Add menu system and scene navigation

The changes include: - Moving script files to scripts/ directory -
Creating Start menu scene with Play/Quit buttons - Adding scene
switching between menu and gameplay - Creating theme for menu buttons -
Adding ESC key to return to menu
This commit is contained in:
Henry 2025-10-12 13:20:30 +01:00
parent 83ff6ef14d
commit 49988ae674
14 changed files with 202 additions and 6 deletions

25
scripts/Paddle.gd Normal file
View file

@ -0,0 +1,25 @@
extends CharacterBody2D
const SPEED: float = 350.0
@export var player_id: int
@export var score_label: Label
var score: int = 0
func _input(event: InputEvent) -> void:
if event.is_action_pressed('up-%d' % player_id):
velocity = Vector2(0, -SPEED)
if event.is_action_pressed( 'down-%d' % player_id):
velocity = Vector2(0, SPEED)
if event.is_action_released('up-%d' % player_id) or \
event.is_action_released('down-%d' % player_id):
velocity = Vector2.ZERO
func _physics_process(_delta: float) -> void:
move_and_slide()
func increment_score():
# score one point
score += 1
# update the UI label
score_label.text = '%02d' % score

1
scripts/Paddle.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://d0dti6pba80ci

31
scripts/ball.gd Normal file
View file

@ -0,0 +1,31 @@
extends CharacterBody2D
const SPEED : float = 310.0
@export var paddle_0 : Node2D
@export var paddle_1 : Node2D
func _ready() -> void:
initialize()
func _physics_process(_delta: float) -> void:
var collision = move_and_collide(velocity * _delta)
if collision:
velocity = velocity.bounce(collision.get_normal())
func initialize():
var extra_offset = 0.0 if randf() < 0.5 else PI
var angle = extra_offset + randf_range(-PI/3.0, PI/3.0)
velocity = Vector2(cos(angle), sin(angle)).normalized() * SPEED
position = get_viewport_rect().size / 2.0
func _on_screen_exited() -> void:
#Update Score
if position.x <0.0:
paddle_1.increment_score()
else:
paddle_0.increment_score()
#Reint After 1 second
await get_tree().create_timer(1.0).timeout
initialize()

1
scripts/ball.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://wrequpaalk7f

8
scripts/gameplay.gd Normal file
View file

@ -0,0 +1,8 @@
extends Control
func _ready():
set_process(true)
func _process(_delta):
if Input.is_action_pressed("key_exit"):
get_tree().change_scene_to_file("res://scenes/Start.tscn")

1
scripts/gameplay.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://8lo33im7wrya

10
scripts/start.gd Normal file
View file

@ -0,0 +1,10 @@
extends Control
func _ready():
%Button_Play.pressed.connect(play)
%Button_Quit.pressed.connect(quit_game)
func play():
get_tree().change_scene_to_file('res://scenes/gameplay.tscn')
func quit_game():
get_tree().quit()

1
scripts/start.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://d3rocjpvkb3te