Skip to content

Commit 6899d44

Browse files
authored
Merge pull request #394 from aaronfranke/pong
Update and improve all Pong demos
2 parents 691f1d9 + 4f5f0ec commit 6899d44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+504
-466
lines changed

2d/pong/ball.gd

Lines changed: 0 additions & 16 deletions
This file was deleted.

2d/pong/ceiling_floor.gd

Lines changed: 0 additions & 7 deletions
This file was deleted.

2d/pong/paddle.gd

Lines changed: 0 additions & 19 deletions
This file was deleted.

2d/pong/pong.tscn

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[gd_scene load_steps=13 format=2]
22

3-
[ext_resource path="res://paddle.gd" type="Script" id=1]
3+
[ext_resource path="res://scripts/paddle.gd" type="Script" id=1]
44
[ext_resource path="res://left_pallete.png" type="Texture" id=2]
55
[ext_resource path="res://right_pallete.png" type="Texture" id=3]
6-
[ext_resource path="res://ball.gd" type="Script" id=4]
6+
[ext_resource path="res://scripts/ball.gd" type="Script" id=4]
77
[ext_resource path="res://ball.png" type="Texture" id=5]
88
[ext_resource path="res://separator.png" type="Texture" id=6]
9-
[ext_resource path="res://wall.gd" type="Script" id=7]
10-
[ext_resource path="res://ceiling_floor.gd" type="Script" id=8]
9+
[ext_resource path="res://scripts/wall.gd" type="Script" id=7]
10+
[ext_resource path="res://scripts/ceiling_floor.gd" type="Script" id=8]
1111

1212
[sub_resource type="RectangleShape2D" id=1]
1313
extents = Vector2( 4, 16 )
@@ -21,86 +21,85 @@ extents = Vector2( 10, 200 )
2121
[sub_resource type="RectangleShape2D" id=4]
2222
extents = Vector2( 320, 10 )
2323

24-
[node name="game" type="Node2D"]
24+
[node name="Pong" type="Node2D"]
2525

26-
[node name="ColorRect" type="ColorRect" parent="."]
26+
[node name="Background" type="ColorRect" parent="."]
2727
margin_right = 640.0
2828
margin_bottom = 400.0
2929
color = Color( 0.141176, 0.152941, 0.164706, 1 )
3030
__meta__ = {
3131
"_edit_use_anchors_": false
3232
}
3333

34-
[node name="left" type="Area2D" parent="."]
34+
[node name="Left" type="Area2D" parent="."]
3535
position = Vector2( 67.6285, 192.594 )
3636
script = ExtResource( 1 )
3737

38-
[node name="sprite" type="Sprite" parent="left"]
38+
[node name="Sprite" type="Sprite" parent="Left"]
3939
texture = ExtResource( 2 )
4040

41-
[node name="collision" type="CollisionShape2D" parent="left"]
41+
[node name="Collision" type="CollisionShape2D" parent="Left"]
4242
shape = SubResource( 1 )
4343

44-
[node name="right" type="Area2D" parent="."]
44+
[node name="Right" type="Area2D" parent="."]
4545
position = Vector2( 563.815, 188.919 )
4646
script = ExtResource( 1 )
47-
ball_dir = -1
4847

49-
[node name="sprite" type="Sprite" parent="right"]
48+
[node name="Sprite" type="Sprite" parent="Right"]
5049
texture = ExtResource( 3 )
5150

52-
[node name="collision" type="CollisionShape2D" parent="right"]
51+
[node name="Collision" type="CollisionShape2D" parent="Right"]
5352
shape = SubResource( 1 )
5453

55-
[node name="ball" type="Area2D" parent="."]
54+
[node name="Ball" type="Area2D" parent="."]
5655
position = Vector2( 320.5, 191.124 )
5756
script = ExtResource( 4 )
5857

59-
[node name="sprite" type="Sprite" parent="ball"]
58+
[node name="Sprite" type="Sprite" parent="Ball"]
6059
texture = ExtResource( 5 )
6160

62-
[node name="collision" type="CollisionShape2D" parent="ball"]
61+
[node name="Collision" type="CollisionShape2D" parent="Ball"]
6362
shape = SubResource( 2 )
6463

65-
[node name="separator" type="Sprite" parent="."]
64+
[node name="Separator" type="Sprite" parent="."]
6665
position = Vector2( 320, 200 )
6766
texture = ExtResource( 6 )
6867

69-
[node name="left_wall" type="Area2D" parent="."]
68+
[node name="LeftWall" type="Area2D" parent="."]
7069
position = Vector2( -10, 200 )
7170
script = ExtResource( 7 )
7271

73-
[node name="collision" type="CollisionShape2D" parent="left_wall"]
72+
[node name="Collision" type="CollisionShape2D" parent="LeftWall"]
7473
shape = SubResource( 3 )
7574

76-
[node name="right_wall" type="Area2D" parent="."]
75+
[node name="RightWall" type="Area2D" parent="."]
7776
position = Vector2( 650, 200 )
7877
script = ExtResource( 7 )
7978

80-
[node name="collision" type="CollisionShape2D" parent="right_wall"]
79+
[node name="Collision" type="CollisionShape2D" parent="RightWall"]
8180
shape = SubResource( 3 )
8281

83-
[node name="ceiling" type="Area2D" parent="."]
82+
[node name="Ceiling" type="Area2D" parent="."]
8483
position = Vector2( 320, -10 )
8584
script = ExtResource( 8 )
8685

87-
[node name="collision" type="CollisionShape2D" parent="ceiling"]
86+
[node name="Collision" type="CollisionShape2D" parent="Ceiling"]
8887
shape = SubResource( 4 )
8988

90-
[node name="floor" type="Area2D" parent="."]
89+
[node name="Floor" type="Area2D" parent="."]
9190
position = Vector2( 320, 410 )
9291
script = ExtResource( 8 )
93-
y_direction = -1
92+
_bounce_direction = -1
9493

95-
[node name="collision" type="CollisionShape2D" parent="floor"]
94+
[node name="Collision" type="CollisionShape2D" parent="Floor"]
9695
shape = SubResource( 4 )
9796

9897
[node name="Camera2D" type="Camera2D" parent="."]
9998
offset = Vector2( 320, 200 )
10099
current = true
101-
[connection signal="area_entered" from="left" to="left" method="_on_area_entered"]
102-
[connection signal="area_entered" from="right" to="right" method="_on_area_entered"]
103-
[connection signal="area_entered" from="left_wall" to="left_wall" method="_on_wall_area_entered"]
104-
[connection signal="area_entered" from="right_wall" to="right_wall" method="_on_wall_area_entered"]
105-
[connection signal="area_entered" from="ceiling" to="ceiling" method="_on_area_entered"]
106-
[connection signal="area_entered" from="floor" to="floor" method="_on_area_entered"]
100+
[connection signal="area_entered" from="Left" to="Left" method="_on_area_entered"]
101+
[connection signal="area_entered" from="Right" to="Right" method="_on_area_entered"]
102+
[connection signal="area_entered" from="LeftWall" to="LeftWall" method="_on_wall_area_entered"]
103+
[connection signal="area_entered" from="RightWall" to="RightWall" method="_on_wall_area_entered"]
104+
[connection signal="area_entered" from="Ceiling" to="Ceiling" method="_on_area_entered"]
105+
[connection signal="area_entered" from="Floor" to="Floor" method="_on_area_entered"]

2d/pong/project.godot

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _global_script_class_icons={
1515

1616
[application]
1717

18-
config/name="Pong"
18+
config/name="Pong with GDScript"
1919
run/main_scene="pong.tscn"
2020
config/icon="res://icon.png"
2121

@@ -36,21 +36,31 @@ singletons=[ ]
3636
left_move_down={
3737
"deadzone": 0.5,
3838
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":90,"unicode":0,"echo":false,"script":null)
39+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
40+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null)
41+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null)
3942
]
4043
}
4144
left_move_up={
4245
"deadzone": 0.5,
4346
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
47+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
48+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null)
49+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
4450
]
4551
}
4652
right_move_down={
4753
"deadzone": 0.5,
4854
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null)
55+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
56+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":1.0,"script":null)
4957
]
5058
}
5159
right_move_up={
5260
"deadzone": 0.5,
5361
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
62+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
63+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":-1.0,"script":null)
5464
]
5565
}
5666

2d/pong/scripts/ball.gd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extends Area2D
2+
3+
const DEFAULT_SPEED = 100
4+
5+
var direction = Vector2.LEFT
6+
7+
onready var _initial_pos = position
8+
onready var _speed = DEFAULT_SPEED
9+
10+
func _process(delta):
11+
_speed += delta * 2
12+
position += _speed * delta * direction
13+
14+
15+
func reset():
16+
direction = Vector2.LEFT
17+
position = _initial_pos
18+
_speed = DEFAULT_SPEED

2d/pong/scripts/ceiling_floor.gd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extends Area2D
2+
3+
export var _bounce_direction = 1
4+
5+
func _on_area_entered( area ):
6+
if area.name == "Ball":
7+
area.direction = (area.direction + Vector2(0, _bounce_direction)).normalized()

2d/pong/scripts/paddle.gd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extends Area2D
2+
3+
const MOVE_SPEED = 100
4+
5+
var _ball_dir
6+
var _up
7+
var _down
8+
9+
onready var _screen_size_y = get_viewport_rect().size.y
10+
11+
func _ready():
12+
var n = name.to_lower()
13+
_up = n + "_move_up"
14+
_down = n + "_move_down"
15+
_ball_dir = 1 if n == "left" else -1
16+
17+
18+
func _process(delta):
19+
# Move up and down based on input.
20+
var input = Input.get_action_strength(_down) - Input.get_action_strength(_up)
21+
position.y = clamp(position.y + input * MOVE_SPEED * delta, 16, _screen_size_y - 16)
22+
23+
24+
func _on_area_entered(area):
25+
if area.name == "Ball":
26+
# Assign new direction.
27+
area.direction = Vector2(_ball_dir, randf() * 2 - 1).normalized()
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
extends Area2D
22

3-
4-
func _on_wall_area_entered( area ):
5-
if area.get_name() == "ball":
3+
func _on_wall_area_entered(area):
4+
if area.name == "Ball":
65
#oops, ball went out of game place, reset
76
area.reset()

mono/pong/Scripts/Ball.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
public class Ball : Area2D
44
{
5-
private const int BallSpeed = 100;
5+
private const int DefaultSpeed = 100;
66

77
public Vector2 direction = Vector2.Left;
88

99
private Vector2 _initialPos;
10-
11-
public void Reset()
12-
{
13-
Position = _initialPos;
14-
direction = Vector2.Left;
15-
}
10+
private float _speed = DefaultSpeed;
1611

1712
public override void _Ready()
1813
{
@@ -21,6 +16,14 @@ public override void _Ready()
2116

2217
public override void _Process(float delta)
2318
{
24-
Position += BallSpeed * delta * direction;
19+
_speed += delta * 2;
20+
Position += _speed * delta * direction;
21+
}
22+
23+
public void Reset()
24+
{
25+
direction = Vector2.Left;
26+
Position = _initialPos;
27+
_speed = DefaultSpeed;
2528
}
2629
}

0 commit comments

Comments
 (0)