Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
Battle Pong
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Frederic Aust
Battle Pong
Commits
487ffaac
Commit
487ffaac
authored
Dec 14, 2020
by
Frederic Aust
Browse files
Options
Downloads
Patches
Plain Diff
=Es ist spielbar!
parent
60874719
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
Battle Pong/Ball.gd
+35
-4
35 additions, 4 deletions
Battle Pong/Ball.gd
Battle Pong/Main.gd
+119
-0
119 additions, 0 deletions
Battle Pong/Main.gd
Battle Pong/Main.tscn
+53
-4
53 additions, 4 deletions
Battle Pong/Main.tscn
Battle Pong/Player.gd
+4
-3
4 additions, 3 deletions
Battle Pong/Player.gd
with
211 additions
and
11 deletions
Battle Pong/Ball.gd
+
35
−
4
View file @
487ffaac
...
@@ -2,17 +2,48 @@ extends RigidBody2D
...
@@ -2,17 +2,48 @@ extends RigidBody2D
# Declare member variables here.
# Declare member variables here.
export
var
min_speed
=
150
# Minimum speed range.
export
var
speed
=
150
export
var
max_speed
=
250
# Maximum speed range.
var
playing
=
false
var
dx
=
100
var
dy
=
0
var
y_range
=
100
# Called when the node enters the scene tree for the first time.
# Called when the node enters the scene tree for the first time.
func
_ready
():
func
_ready
():
pass
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#func _process(delta):
# pass
# pass
func
_on_VisibilityNotifier2D_screen_exited
():
queue_free
()
func
_physics_process
(
delta
):
if
playing
:
change_dy_on_wall_hit
()
self
.
rotation
=
0
self
.
linear_velocity
=
Vector2
(
dx
,
dy
)
*
delta
*
speed
func
ball_hit_paddle
():
dx
*=
-
1
dy
=
rand_range
(
-
y_range
,
y_range
)
if
speed
<
300
:
speed
+=
5
if
y_range
<
200
:
y_range
+=
5
func
change_dy_on_wall_hit
():
if
self
.
position
.
y
<=
0
:
dy
=
rand_range
(
0
,
y_range
)
if
self
.
position
.
y
>=
600
:
dy
=
rand_range
(
-
y_range
,
0
)
func
set_playing
(
_playing
):
playing
=
_playing
func
start
(
pos
):
position
=
pos
show
()
$
CollisionShape2D
.
disabled
=
false
This diff is collapsed.
Click to expand it.
Battle Pong/Main.gd
0 → 100644
+
119
−
0
View file @
487ffaac
extends
Node
var
Ball
=
preload
(
"Ball.tscn"
)
var
ball
=
Ball
.
instance
()
var
score_player_one
=
0
var
score_player_two
=
0
var
playing
=
false
var
score_event
=
false
var
game_done
=
false
const
SPACE_TO_PLAY
=
"Press SPACE to Play!"
const
P1_WIN
=
"Player 1 won!"
const
P2_WIN
=
"Player 2 won!"
var
message
=
SPACE_TO_PLAY
func
_ready
():
set_ball
()
$
PlayerOne
.
start
(
$
StartPositionPlayerOne
.
position
)
$
PlayerTwo
.
start
(
$
StartPositionPlayerTwo
.
position
)
display_message
()
update_score
()
func
_input
(
_event
):
if
Input
.
is_key_pressed
(
KEY_SPACE
):
play
()
func
play
():
if
game_done
:
# if game was done, reset states to start a fresh game
game_done
=
false
score_player_one
=
0
score_player_two
=
0
message
=
SPACE_TO_PLAY
update_score
()
playing
=
true
ball
.
set_playing
(
playing
)
$
DisplayMessage
.
visible
=
false
func
_process
(
delta
):
check_point_scored
()
handle_score_event
()
handle_game_end
()
func
check_point_scored
():
if
ball
.
position
.
x
<=
0
:
score_event
=
true
score_player_two
+=
1
if
ball
.
position
.
x
>=
1024
:
score_event
=
true
score_player_one
+=
1
update_score
()
if
score_player_one
==
5
or
score_player_two
==
5
:
game_done
=
true
func
handle_score_event
():
if
score_event
:
remove_ball
()
set_ball
()
reset_paddle_positions
()
display_message
()
playing
=
false
score_event
=
false
func
update_score
():
$
Player1Score
.
text
=
str
(
score_player_one
)
$
Player2Score
.
text
=
str
(
score_player_two
)
func
display_message
():
$
DisplayMessage
.
text
=
message
$
DisplayMessage
.
visible
=
true
func
game_over
():
$
ScoreTimer
.
stop
()
$
MobTimer
.
stop
()
func
new_game
():
score_player_one
=
0
score_player_two
=
0
$
PlayerOne
.
start
(
$
StartPositionPlayerOne
.
position
)
$
PlayerTwo
.
start
(
$
StartPositionPlayerTwo
.
position
)
$
StartTimer
.
start
()
func
remove_ball
():
remove_child
(
ball
)
func
set_ball
():
ball
=
Ball
.
instance
()
add_child
(
ball
)
ball
.
start
(
$
StartPositionBall
.
position
)
func
reset_paddle_positions
():
$
PlayerOne
.
start
(
$
StartPositionPlayerOne
.
position
)
$
PlayerTwo
.
start
(
$
StartPositionPlayerTwo
.
position
)
func
handle_game_end
():
if
game_done
:
if
score_player_one
==
5
:
message
=
P1_WIN
else
:
message
=
P2_WIN
display_message
()
func
_on_PlayerTwo_hit
():
ball
.
ball_hit_paddle
()
pass
# Replace with function body.
func
_on_PlayerOne_hit
():
ball
.
ball_hit_paddle
()
pass
# Replace with function body.
This diff is collapsed.
Click to expand it.
Battle Pong/Main.tscn
+
53
−
4
View file @
487ffaac
[gd_scene load_steps=
2
format=2]
[gd_scene load_steps=
3
format=2]
[ext_resource path="res://Player.tscn" type="PackedScene" id=1]
[ext_resource path="res://Player.tscn" type="PackedScene" id=1]
[ext_resource path="res://Main.gd" type="Script" id=2]
[node name="Main" type="Node"]
[node name="Node2D" type="Node2D"]
script = ExtResource( 2 )
[node name="PlayerTwo" parent="." instance=ExtResource( 1 )]
[node name="Player2Score" type="Label" parent="."]
margin_left = 700.0
margin_top = 50.0
margin_right = 750.0
margin_bottom = 110.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Player1Score" type="Label" parent="."]
margin_left = 200.0
margin_top = 50.0
margin_right = 250.0
margin_bottom = 110.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="DisplayMessage" type="Label" parent="."]
margin_right = 40.0
margin_bottom = 14.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="ColorRect" type="ColorRect" parent="."]
margin_left = 510.0
margin_right = 514.0
margin_bottom = 600.0
color = Color( 0.0784314, 1, 0, 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="StartTimer" type="Timer" parent="."]
wait_time = 2.0
one_shot = true
[node name="StartPositionPlayerTwo" type="Position2D" parent="."]
position = Vector2( 968, 300 )
position = Vector2( 968, 300 )
[node name="StartPositionBall" type="Position2D" parent="."]
position = Vector2( 512, 300 )
[node name="StartPositionPlayerOne" type="Position2D" parent="."]
position = Vector2( 52, 300 )
[node name="PlayerTwo" parent="." instance=ExtResource( 1 )]
is_player_one = false
is_player_one = false
[node name="PlayerOne" parent="." instance=ExtResource( 1 )]
[node name="PlayerOne" parent="." instance=ExtResource( 1 )]
position = Vector2( 52, 300 )
[connection signal="hit" from="PlayerTwo" to="." method="_on_PlayerTwo_hit"]
[connection signal="hit" from="PlayerOne" to="." method="_on_PlayerOne_hit"]
This diff is collapsed.
Click to expand it.
Battle Pong/Player.gd
+
4
−
3
View file @
487ffaac
...
@@ -5,10 +5,11 @@ signal hit
...
@@ -5,10 +5,11 @@ signal hit
export
var
speed
=
400
# (pixel/sec)
export
var
speed
=
400
# (pixel/sec)
export
var
is_player_one
=
true
# Select player one or two
export
var
is_player_one
=
true
# Select player one or two
var
screen_size
var
screen_size
var
player_size
# Called when the node enters the scene tree for the first time.
# Called when the node enters the scene tree for the first time.
func
_ready
():
func
_ready
():
screen_size
=
get_viewport_rect
()
.
size
screen_size
=
get_viewport_rect
()
.
size
player_size
=$
CollisionShape2D
.
shape
.
extents
#hide()
#hide()
pass
# Replace with function body.
pass
# Replace with function body.
...
@@ -36,13 +37,13 @@ func _process(delta):
...
@@ -36,13 +37,13 @@ func _process(delta):
position
+=
velocity
*
delta
position
+=
velocity
*
delta
#position.x = clamp(position.x, 0, screen_size.x) # ändert sich nicht
#position.x = clamp(position.x, 0, screen_size.x) # ändert sich nicht
position
.
y
=
clamp
(
position
.
y
,
0
,
screen
_size
.
y
)
position
.
y
=
clamp
(
position
.
y
,
0
+
player_size
.
y
,
screen_size
.
y
-
player
_size
.
y
)
func
_on_Player_body_entered
(
body
):
func
_on_Player_body_entered
(
body
):
emit_signal
(
"hit"
)
emit_signal
(
"hit"
)
$
CollisionShape2D
.
set_deferred
(
"disabled"
,
true
)
# TODO das muss aufgerufen werden, wenn der ball das spielfeld auf der eigenen Seite verlässt
#
$CollisionShape2D.set_deferred("disabled", true) # TODO das muss aufgerufen werden, wenn der ball das spielfeld auf der eigenen Seite verlässt
func
start
(
pos
):
func
start
(
pos
):
position
=
pos
position
=
pos
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment