Skip to content

Commit

Permalink
Add Pong demo
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
manuq committed Jun 19, 2024
1 parent 29f250a commit 76bda4b
Show file tree
Hide file tree
Showing 24 changed files with 680 additions and 1 deletion.
39 changes: 39 additions & 0 deletions pong_game/ball.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@tool
class_name Ball
extends RigidBody2D

const _INITIAL_RADIUS: int = 64

## This is how fast your ball moves.
@export_range(0.0, 10000.0, 0.5, "suffix:px/s") var initial_speed: float = 500.0

## This is the initial angle of the ball.
@export_range(-180, 180, 0.5, "radians_as_degrees") var initial_direction: float = PI / 4

## How big is this ball?
@export_range(0.1, 5.0, 0.1) var size: float = 1.0:
set = _set_size

@onready var _shape: CollisionShape2D = %CollisionShape2D
@onready var _sprite: Sprite2D = %Sprite2D


func _set_size(new_size: float):
size = new_size
if not is_node_ready():
await ready
_shape.shape.radius = _INITIAL_RADIUS * size
_sprite.scale = Vector2(size, size)


func _ready():
if Engine.is_editor_hint():
set_process(false)
set_physics_process(false)
reset()


func reset():
linear_velocity = Vector2.from_angle(initial_direction) * initial_speed
angular_velocity = 0.0
_set_size(size)
Binary file added pong_game/ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions pong_game/ball.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bcgr5amsq3jfl"
path="res://.godot/imported/ball.png-207b87d36613f4458c4a15d56cdcf75a.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://pong_game/ball.png"
dest_files=["res://.godot/imported/ball.png-207b87d36613f4458c4a15d56cdcf75a.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
39 changes: 39 additions & 0 deletions pong_game/ball.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[gd_scene load_steps=7 format=3 uid="uid://c7l70grmkauij"]

[ext_resource type="Script" path="res://pong_game/ball.gd" id="1_vdrab"]
[ext_resource type="Texture2D" uid="uid://bcgr5amsq3jfl" path="res://pong_game/ball.png" id="2_xkrmm"]
[ext_resource type="AudioStream" uid="uid://bc7jd3d8m5spt" path="res://pong_game/wall_hit.ogg" id="3_mjbsd"]
[ext_resource type="AudioStream" uid="uid://jk0oapxjw354" path="res://pong_game/paddle_hit.ogg" id="4_skr8k"]

[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_c3m63"]
friction = 0.0
bounce = 1.0

[sub_resource type="CircleShape2D" id="CircleShape2D_sntrn"]
radius = 64.0

[node name="Ball" type="RigidBody2D" groups=["balls"]]
collision_layer = 2
collision_mask = 15
physics_material_override = SubResource("PhysicsMaterial_c3m63")
continuous_cd = 1
max_contacts_reported = 1
contact_monitor = true
linear_velocity = Vector2(353.553, 353.553)
script = ExtResource("1_vdrab")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
unique_name_in_owner = true
shape = SubResource("CircleShape2D_sntrn")

[node name="Sprite2D" type="Sprite2D" parent="."]
unique_name_in_owner = true
texture = ExtResource("2_xkrmm")

[node name="WallAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = ExtResource("3_mjbsd")

[node name="PaddleAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = ExtResource("4_skr8k")

[connection signal="body_entered" from="." to="." method="_on_body_entered"]
19 changes: 19 additions & 0 deletions pong_game/hud.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends CanvasLayer

@onready var _score_labels = {
"left": %PlayerLeftScore,
"right": %PlayerRightScore,
}


## Sets the score for one player.
func set_player_score(player: String, score: int):
var text = _score_labels[player].text
if str(score) != text:
_score_labels[player].text = str(score)


## Sets the score for each player.
func set_players_scores(left_score: int, right_score: int):
set_player_score("left", left_score)
set_player_score("right", right_score)
57 changes: 57 additions & 0 deletions pong_game/hud.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[gd_scene load_steps=3 format=3 uid="uid://bis7afjjuwypq"]

[ext_resource type="Script" path="res://pong_game/hud.gd" id="1_yyb01"]
[ext_resource type="Texture2D" uid="uid://dijemw7iilr2m" path="res://pong_game/line.png" id="2_jon51"]

[node name="HUD" type="CanvasLayer" groups=["hud"]]
script = ExtResource("1_yyb01")

[node name="Control" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="HBoxContainer" type="HBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="Lines" type="Sprite2D" parent="Control/HBoxContainer"]
unique_name_in_owner = true
texture_repeat = 2
position = Vector2(960, 536)
texture = ExtResource("2_jon51")
region_enabled = true
region_rect = Rect2(0, 0, 20, 1100)

[node name="PlayerLeftScore" type="Label" parent="Control"]
unique_name_in_owner = true
layout_mode = 2
offset_left = 240.0
offset_right = 717.0
offset_bottom = 1080.0
pivot_offset = Vector2(240, 176)
size_flags_horizontal = 3
size_flags_vertical = 1
theme_override_font_sizes/font_size = 200
text = "0"
horizontal_alignment = 1

[node name="PlayerRightScore" type="Label" parent="Control"]
unique_name_in_owner = true
layout_mode = 2
offset_left = 1200.0
offset_right = 1677.0
offset_bottom = 1080.0
pivot_offset = Vector2(240, 176)
size_flags_horizontal = 3
size_flags_vertical = 1
theme_override_font_sizes/font_size = 200
text = "0"
horizontal_alignment = 1
Binary file added pong_game/line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions pong_game/line.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://dijemw7iilr2m"
path="res://.godot/imported/line.png-452af046907249a8ef12695e48447ab1.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://pong_game/line.png"
dest_files=["res://.godot/imported/line.png-452af046907249a8ef12695e48447ab1.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
33 changes: 33 additions & 0 deletions pong_game/paddle.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@tool
class_name Paddle
extends CharacterBody2D


func get_custom_class():
return "Paddle"


static func get_exposed_properties() -> Array[String]:
return ["position"]


static func get_custom_blocks() -> Array[BlockCategory]:
var b: Block

# Movement
var movement_list: Array[Block] = []
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}"
b.statement = 'velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")*{speed}\n' + "move_and_slide()"
movement_list.append(b)

b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}"
b.statement = 'velocity = Input.get_vector("player_2_left", "player_2_right", "player_2_up", "player_2_down")*{speed}\n' + "move_and_slide()"
movement_list.append(b)

var movement_category: BlockCategory = BlockCategory.new("Movement", movement_list, Color("4a86d5"))

return [movement_category]
Binary file added pong_game/paddle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions pong_game/paddle.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://tplpgtnfeda0"
path="res://.godot/imported/paddle.png-2fbcaaf37bbbddd482a4ad5b20dde0a2.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://pong_game/paddle.png"
dest_files=["res://.godot/imported/paddle.png-2fbcaaf37bbbddd482a4ad5b20dde0a2.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
19 changes: 19 additions & 0 deletions pong_game/paddle.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[gd_scene load_steps=4 format=3 uid="uid://s7enbp56f256"]

[ext_resource type="Script" path="res://pong_game/paddle.gd" id="1_74lee"]
[ext_resource type="Texture2D" uid="uid://tplpgtnfeda0" path="res://pong_game/paddle.png" id="1_eucti"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_bbbjr"]
size = Vector2(128, 384)

[node name="Paddle" type="CharacterBody2D" groups=["paddles"]]
collision_mask = 68
motion_mode = 1
script = ExtResource("1_74lee")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_bbbjr")

[node name="Sprite2D" type="Sprite2D" parent="."]
unique_name_in_owner = true
texture = ExtResource("1_eucti")
Binary file added pong_game/paddle_hit.ogg
Binary file not shown.
19 changes: 19 additions & 0 deletions pong_game/paddle_hit.ogg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[remap]

importer="oggvorbisstr"
type="AudioStreamOggVorbis"
uid="uid://jk0oapxjw354"
path="res://.godot/imported/paddle_hit.ogg-78978cea7a35465f97568c50bd227ec1.oggvorbisstr"

[deps]

source_file="res://pong_game/paddle_hit.ogg"
dest_files=["res://.godot/imported/paddle_hit.ogg-78978cea7a35465f97568c50bd227ec1.oggvorbisstr"]

[params]

loop=false
loop_offset=0
bpm=0
beat_count=0
bar_beats=4
29 changes: 29 additions & 0 deletions pong_game/pong_game.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@tool
class_name Pong
extends Node2D


func get_custom_class():
return "Pong"


static func get_custom_blocks() -> Array[BlockCategory]:
var b: Block

# TODO: Only for testing. Move these blocks where they belong.
var score_list: Array[Block] = []
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Change player 1 score by {score: INT}"
b.statement = 'get_tree().call_group("hud", "set_player_score", "right", {score})'
score_list.append(b)

b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Change player 2 score by {score: INT}"
b.statement = 'get_tree().call_group("hud", "set_player_score", "left", {score})'
score_list.append(b)

var score_category: BlockCategory = BlockCategory.new("Scoring", score_list, Color("4a86d5"))

return [score_category]
Loading

0 comments on commit 76bda4b

Please sign in to comment.