Skip to content

Commit 7c4ac6b

Browse files
committed
Pong demo: Add area for spawning balls
In Pong the balls spawn at a random position from the center vertical line. To avoid the maths, one option is to pick a random point from an Area2D with a thin vertical rectangle as collision shape. The area has group "ball_spawn_area" so it can be referenced.
1 parent b129a33 commit 7c4ac6b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

pong_game/ball_spawn_area.tscn

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://1ownc3rikn2k"]
2+
3+
[sub_resource type="RectangleShape2D" id="RectangleShape2D_tpd4f"]
4+
size = Vector2(128, 896)
5+
6+
[node name="BallSpawnArea" type="Area2D"]
7+
8+
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
9+
shape = SubResource("RectangleShape2D_tpd4f")

pong_game/pong_game.tscn

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=31 format=3 uid="uid://tf7b8c64ecc0"]
1+
[gd_scene load_steps=32 format=3 uid="uid://tf7b8c64ecc0"]
22

33
[ext_resource type="PackedScene" uid="uid://s7enbp56f256" path="res://pong_game/paddle.tscn" id="1_1k5k2"]
44
[ext_resource type="Script" path="res://pong_game/pong_game.gd" id="1_bjkc8"]
@@ -10,6 +10,7 @@
1010
[ext_resource type="Script" path="res://addons/block_code/block_script_data/block_script_data.gd" id="7_uuuue"]
1111
[ext_resource type="PackedScene" uid="uid://bis7afjjuwypq" path="res://pong_game/hud.tscn" id="8_yg457"]
1212
[ext_resource type="PackedScene" uid="uid://c7l70grmkauij" path="res://pong_game/ball.tscn" id="9_xrqll"]
13+
[ext_resource type="PackedScene" uid="uid://1ownc3rikn2k" path="res://pong_game/ball_spawn_area.tscn" id="10_5vs1t"]
1314

1415
[sub_resource type="Resource" id="Resource_535fl"]
1516
script = ExtResource("5_wr38c")
@@ -152,25 +153,28 @@ script = ExtResource("1_bjkc8")
152153

153154
[node name="PaddleLeft" parent="." instance=ExtResource("1_1k5k2")]
154155
modulate = Color(0.511, 0.362, 0.972, 1)
155-
position = Vector2(64, 576)
156+
position = Vector2(64, 544)
156157

157158
[node name="BlockCode" type="Node" parent="PaddleLeft"]
158159
script = ExtResource("3_6jaq8")
159160
bsd = SubResource("Resource_t7nl4")
160161

161162
[node name="PaddleRight" parent="." instance=ExtResource("1_1k5k2")]
162163
modulate = Color(0.511, 0.362, 0.972, 1)
163-
position = Vector2(1856, 576)
164+
position = Vector2(1856, 544)
164165

165166
[node name="BlockCode" type="Node" parent="PaddleRight"]
166167
script = ExtResource("3_6jaq8")
167168
bsd = SubResource("Resource_52r02")
168169

169170
[node name="Ball" parent="." instance=ExtResource("9_xrqll")]
170171
modulate = Color(0.511, 0.362, 0.972, 1)
171-
position = Vector2(960, 576)
172+
position = Vector2(960, 544)
172173
linear_velocity = Vector2(353.553, 353.553)
173174

175+
[node name="BallSpawnArea" parent="." groups=["ball_spawn_area"] instance=ExtResource("10_5vs1t")]
176+
position = Vector2(960, 544)
177+
174178
[node name="HUD" parent="." instance=ExtResource("8_yg457")]
175179

176180
[node name="BlockCode" type="Node" parent="."]

0 commit comments

Comments
 (0)