Skip to content

Commit 98f8ba0

Browse files
committed
Add Pong demo
And make it the main scene. The paddles have 2 custom blocks for movement, borrowing from the SimpleCharacter example. But the speed parameter is a Vector2 so the movement can be limited to the Y axis. The HUD has methods to change the scoring labels. For now a BlockCode node is added to the root node for setting arbitrary score as example. All components have been copied from Moddable Pong and removing the logic. The ball properties were left for now. The project window viewport is adjusted to match Moddable Pong. Also the gravity setting was set to zero.
1 parent 29f250a commit 98f8ba0

24 files changed

+680
-1
lines changed

pong_game/ball.gd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@tool
2+
class_name Ball
3+
extends RigidBody2D
4+
5+
const _INITIAL_RADIUS: int = 64
6+
7+
## This is how fast your ball moves.
8+
@export_range(0.0, 10000.0, 0.5, "suffix:px/s") var initial_speed: float = 500.0
9+
10+
## This is the initial angle of the ball.
11+
@export_range(-180, 180, 0.5, "radians_as_degrees") var initial_direction: float = PI / 4
12+
13+
## How big is this ball?
14+
@export_range(0.1, 5.0, 0.1) var size: float = 1.0:
15+
set = _set_size
16+
17+
@onready var _shape: CollisionShape2D = %CollisionShape2D
18+
@onready var _sprite: Sprite2D = %Sprite2D
19+
20+
21+
func _set_size(new_size: float):
22+
size = new_size
23+
if not is_node_ready():
24+
await ready
25+
_shape.shape.radius = _INITIAL_RADIUS * size
26+
_sprite.scale = Vector2(size, size)
27+
28+
29+
func _ready():
30+
if Engine.is_editor_hint():
31+
set_process(false)
32+
set_physics_process(false)
33+
reset()
34+
35+
36+
func reset():
37+
linear_velocity = Vector2.from_angle(initial_direction) * initial_speed
38+
angular_velocity = 0.0
39+
_set_size(size)

pong_game/ball.png

6.74 KB
Loading

pong_game/ball.png.import

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://bcgr5amsq3jfl"
6+
path="res://.godot/imported/ball.png-207b87d36613f4458c4a15d56cdcf75a.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://pong_game/ball.png"
14+
dest_files=["res://.godot/imported/ball.png-207b87d36613f4458c4a15d56cdcf75a.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

pong_game/ball.tscn

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[gd_scene load_steps=7 format=3 uid="uid://c7l70grmkauij"]
2+
3+
[ext_resource type="Script" path="res://pong_game/ball.gd" id="1_vdrab"]
4+
[ext_resource type="Texture2D" uid="uid://bcgr5amsq3jfl" path="res://pong_game/ball.png" id="2_xkrmm"]
5+
[ext_resource type="AudioStream" uid="uid://bc7jd3d8m5spt" path="res://pong_game/wall_hit.ogg" id="3_mjbsd"]
6+
[ext_resource type="AudioStream" uid="uid://jk0oapxjw354" path="res://pong_game/paddle_hit.ogg" id="4_skr8k"]
7+
8+
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_c3m63"]
9+
friction = 0.0
10+
bounce = 1.0
11+
12+
[sub_resource type="CircleShape2D" id="CircleShape2D_sntrn"]
13+
radius = 64.0
14+
15+
[node name="Ball" type="RigidBody2D" groups=["balls"]]
16+
collision_layer = 2
17+
collision_mask = 15
18+
physics_material_override = SubResource("PhysicsMaterial_c3m63")
19+
continuous_cd = 1
20+
max_contacts_reported = 1
21+
contact_monitor = true
22+
linear_velocity = Vector2(353.553, 353.553)
23+
script = ExtResource("1_vdrab")
24+
25+
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
26+
unique_name_in_owner = true
27+
shape = SubResource("CircleShape2D_sntrn")
28+
29+
[node name="Sprite2D" type="Sprite2D" parent="."]
30+
unique_name_in_owner = true
31+
texture = ExtResource("2_xkrmm")
32+
33+
[node name="WallAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
34+
stream = ExtResource("3_mjbsd")
35+
36+
[node name="PaddleAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
37+
stream = ExtResource("4_skr8k")
38+
39+
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

pong_game/hud.gd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
extends CanvasLayer
2+
3+
@onready var _score_labels = {
4+
"left": %PlayerLeftScore,
5+
"right": %PlayerRightScore,
6+
}
7+
8+
9+
## Sets the score for one player.
10+
func set_player_score(player: String, score: int):
11+
var text = _score_labels[player].text
12+
if str(score) != text:
13+
_score_labels[player].text = str(score)
14+
15+
16+
## Sets the score for each player.
17+
func set_players_scores(left_score: int, right_score: int):
18+
set_player_score("left", left_score)
19+
set_player_score("right", right_score)

pong_game/hud.tscn

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[gd_scene load_steps=3 format=3 uid="uid://bis7afjjuwypq"]
2+
3+
[ext_resource type="Script" path="res://pong_game/hud.gd" id="1_yyb01"]
4+
[ext_resource type="Texture2D" uid="uid://dijemw7iilr2m" path="res://pong_game/line.png" id="2_jon51"]
5+
6+
[node name="HUD" type="CanvasLayer" groups=["hud"]]
7+
script = ExtResource("1_yyb01")
8+
9+
[node name="Control" type="Control" parent="."]
10+
layout_mode = 3
11+
anchors_preset = 15
12+
anchor_right = 1.0
13+
anchor_bottom = 1.0
14+
grow_horizontal = 2
15+
grow_vertical = 2
16+
17+
[node name="HBoxContainer" type="HBoxContainer" parent="Control"]
18+
layout_mode = 1
19+
anchors_preset = 15
20+
anchor_right = 1.0
21+
anchor_bottom = 1.0
22+
grow_horizontal = 2
23+
grow_vertical = 2
24+
25+
[node name="Lines" type="Sprite2D" parent="Control/HBoxContainer"]
26+
unique_name_in_owner = true
27+
texture_repeat = 2
28+
position = Vector2(960, 536)
29+
texture = ExtResource("2_jon51")
30+
region_enabled = true
31+
region_rect = Rect2(0, 0, 20, 1100)
32+
33+
[node name="PlayerLeftScore" type="Label" parent="Control"]
34+
unique_name_in_owner = true
35+
layout_mode = 2
36+
offset_left = 240.0
37+
offset_right = 717.0
38+
offset_bottom = 1080.0
39+
pivot_offset = Vector2(240, 176)
40+
size_flags_horizontal = 3
41+
size_flags_vertical = 1
42+
theme_override_font_sizes/font_size = 200
43+
text = "0"
44+
horizontal_alignment = 1
45+
46+
[node name="PlayerRightScore" type="Label" parent="Control"]
47+
unique_name_in_owner = true
48+
layout_mode = 2
49+
offset_left = 1200.0
50+
offset_right = 1677.0
51+
offset_bottom = 1080.0
52+
pivot_offset = Vector2(240, 176)
53+
size_flags_horizontal = 3
54+
size_flags_vertical = 1
55+
theme_override_font_sizes/font_size = 200
56+
text = "0"
57+
horizontal_alignment = 1

pong_game/line.png

179 Bytes
Loading

pong_game/line.png.import

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://dijemw7iilr2m"
6+
path="res://.godot/imported/line.png-452af046907249a8ef12695e48447ab1.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://pong_game/line.png"
14+
dest_files=["res://.godot/imported/line.png-452af046907249a8ef12695e48447ab1.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

pong_game/paddle.gd

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@tool
2+
class_name Paddle
3+
extends CharacterBody2D
4+
5+
6+
func get_custom_class():
7+
return "Paddle"
8+
9+
10+
static func get_exposed_properties() -> Array[String]:
11+
return ["position"]
12+
13+
14+
static func get_custom_blocks() -> Array[BlockCategory]:
15+
var b: Block
16+
17+
# Movement
18+
var movement_list: Array[Block] = []
19+
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
20+
b.block_type = Types.BlockType.EXECUTE
21+
b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}"
22+
b.statement = 'velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")*{speed}\n' + "move_and_slide()"
23+
movement_list.append(b)
24+
25+
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
26+
b.block_type = Types.BlockType.EXECUTE
27+
b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}"
28+
b.statement = 'velocity = Input.get_vector("player_2_left", "player_2_right", "player_2_up", "player_2_down")*{speed}\n' + "move_and_slide()"
29+
movement_list.append(b)
30+
31+
var movement_category: BlockCategory = BlockCategory.new("Movement", movement_list, Color("4a86d5"))
32+
33+
return [movement_category]

pong_game/paddle.png

6.78 KB
Loading

0 commit comments

Comments
 (0)