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

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=3 format=3 uid="uid://dd0cxfceqwxca"]
[ext_resource type="Script" uid="uid://d0dti6pba80ci" path="res://Paddle.gd" id="1_q73f6"]
[ext_resource type="Script" uid="uid://d0dti6pba80ci" path="res://scripts/Paddle.gd" id="1_q73f6"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_e2o6t"]
size = Vector2(33, 105)

View file

@ -11,7 +11,7 @@ config_version=5
[application]
config/name="2025-08-23 Learning"
run/main_scene="uid://ct7v3x7imstlk"
run/main_scene="uid://d04c3lqwj6a5h"
config/features=PackedStringArray("4.5", "Forward Plus")
config/icon="res://icon.svg"
@ -37,3 +37,8 @@ down-1={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":75,"key_label":0,"unicode":107,"location":0,"echo":false,"script":null)
]
}
key_exit={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}

101
resources/pong_theme.tres Normal file
View file

@ -0,0 +1,101 @@
[gd_resource type="Theme" load_steps=7 format=3 uid="uid://cga6aww0tbilq"]
[sub_resource type="SystemFont" id="SystemFont_svo66"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_svo66"]
content_margin_left = 4.0
content_margin_top = 4.0
content_margin_right = 4.0
content_margin_bottom = 4.0
bg_color = Color(0.35417196, 0.23284352, 0.33606362, 1)
corner_radius_top_left = 3
corner_radius_top_right = 3
corner_radius_bottom_right = 3
corner_radius_bottom_left = 3
corner_detail = 5
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_u7j1g"]
content_margin_left = 4.0
content_margin_top = 4.0
content_margin_right = 4.0
content_margin_bottom = 4.0
bg_color = Color(0.35417196, 0.23284352, 0.33606362, 1)
draw_center = false
border_width_left = 2
border_width_top = 2
border_width_right = 2
border_width_bottom = 2
corner_radius_top_left = 3
corner_radius_top_right = 3
corner_radius_bottom_right = 3
corner_radius_bottom_left = 3
corner_detail = 5
expand_margin_left = 2.0
expand_margin_top = 2.0
expand_margin_right = 2.0
expand_margin_bottom = 2.0
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_gwd0w"]
content_margin_left = 136.0
content_margin_right = 136.0
bg_color = Color(0.42745098, 0.59607846, 0.7411765, 1)
border_width_left = 7
border_width_top = 7
border_width_right = 7
border_width_bottom = 7
expand_margin_left = 52.0
expand_margin_top = 52.0
expand_margin_right = 52.0
expand_margin_bottom = 52.0
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_gq8b7"]
content_margin_left = 136.0
content_margin_right = 136.0
bg_color = Color(0.20784314, 0.23921569, 0.2901961, 1)
border_width_left = 7
border_width_top = 7
border_width_right = 7
border_width_bottom = 7
expand_margin_left = 52.0
expand_margin_top = 52.0
expand_margin_right = 52.0
expand_margin_bottom = 52.0
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_5f4su"]
content_margin_left = 4.0
content_margin_top = 4.0
content_margin_right = 4.0
content_margin_bottom = 4.0
bg_color = Color(0.3529412, 0.23137255, 0.3372549, 1)
corner_radius_top_left = 3
corner_radius_top_right = 3
corner_radius_bottom_right = 3
corner_radius_bottom_left = 3
corner_detail = 5
[resource]
Button/colors/font_color = Color(0.875, 0.875, 0.875, 1)
Button/colors/font_disabled_color = Color(0.875, 0.875, 0.875, 0.5)
Button/colors/font_focus_color = Color(0.95, 0.95, 0.95, 1)
Button/colors/font_hover_color = Color(0.95, 0.95, 0.95, 1)
Button/colors/font_hover_pressed_color = Color(1, 1, 1, 1)
Button/colors/font_outline_color = Color(0, 0, 0, 1)
Button/colors/font_pressed_color = Color(1, 1, 1, 1)
Button/colors/icon_disabled_color = Color(1, 1, 1, 0.4)
Button/colors/icon_focus_color = Color(1, 1, 1, 1)
Button/colors/icon_hover_color = Color(1, 1, 1, 1)
Button/colors/icon_hover_pressed_color = Color(1, 1, 1, 1)
Button/colors/icon_normal_color = Color(1, 1, 1, 1)
Button/colors/icon_pressed_color = Color(1, 1, 1, 1)
Button/constants/align_to_largest_stylebox = 0
Button/constants/h_separation = 10
Button/constants/icon_max_width = 0
Button/constants/line_spacing = 6
Button/constants/outline_size = 4
Button/font_sizes/font_size = 72
Button/fonts/font = SubResource("SystemFont_svo66")
Button/styles/disabled = SubResource("StyleBoxFlat_svo66")
Button/styles/focus = SubResource("StyleBoxFlat_u7j1g")
Button/styles/hover = SubResource("StyleBoxFlat_gwd0w")
Button/styles/normal = SubResource("StyleBoxFlat_gq8b7")
Button/styles/pressed = SubResource("StyleBoxFlat_5f4su")

13
scenes/Main.tscn Normal file
View file

@ -0,0 +1,13 @@
[gd_scene load_steps=4 format=3 uid="uid://dy4s4as4b07s8"]
[ext_resource type="Script" uid="uid://bi1hbvswrw6yr" path="res://scripts/main.gd" id="1_0bbpv"]
[ext_resource type="PackedScene" uid="uid://d04c3lqwj6a5h" path="res://scenes/Start.tscn" id="2_rarhs"]
[ext_resource type="PackedScene" uid="uid://ct7v3x7imstlk" path="res://scenes/gameplay.tscn" id="3_vcsgt"]
[node name="Main" type="Node2D"]
script = ExtResource("1_0bbpv")
[node name="Start" parent="." instance=ExtResource("2_rarhs")]
z_index = 5
[node name="Gameplay" parent="." instance=ExtResource("3_vcsgt")]

55
scenes/Start.tscn Normal file
View file

@ -0,0 +1,55 @@
[gd_scene load_steps=4 format=3 uid="uid://d04c3lqwj6a5h"]
[ext_resource type="Script" uid="uid://d3rocjpvkb3te" path="res://scripts/start.gd" id="1_r305v"]
[ext_resource type="Theme" uid="uid://cga6aww0tbilq" path="res://resources/pong_theme.tres" id="3_gx2gx"]
[sub_resource type="GDScript" id="GDScript_fx3ca"]
script/source = "extends VBoxContainer
"
[node name="Start" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_r305v")
[node name="ColorRect" type="ColorRect" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0.29000002, 0.402, 0.5, 1)
[node name="CenterContainer" type="CenterContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"]
layout_mode = 2
theme_override_constants/separation = 175
script = SubResource("GDScript_fx3ca")
[node name="Button_Play" type="Button" parent="CenterContainer/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
theme = ExtResource("3_gx2gx")
theme_override_colors/font_color = Color(0.87, 0.87, 0.87, 1)
theme_override_constants/outline_size = 2
text = "Play"
[node name="Button_Quit" type="Button" parent="CenterContainer/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
theme = ExtResource("3_gx2gx")
theme_override_colors/font_color = Color(0.87, 0.87, 0.87, 1)
theme_override_constants/outline_size = 2
text = "Quit"

View file

@ -1,20 +1,22 @@
[gd_scene load_steps=5 format=3 uid="uid://ct7v3x7imstlk"]
[gd_scene load_steps=6 format=3 uid="uid://ct7v3x7imstlk"]
[ext_resource type="PackedScene" uid="uid://dd0cxfceqwxca" path="res://prefabs/paddle.tscn" id="1_80nbo"]
[ext_resource type="Script" uid="uid://wrequpaalk7f" path="res://ball.gd" id="2_e2o6t"]
[ext_resource type="Script" uid="uid://8lo33im7wrya" path="res://scripts/gameplay.gd" id="1_oe8ih"]
[ext_resource type="Script" uid="uid://wrequpaalk7f" path="res://scripts/ball.gd" id="2_e2o6t"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_mwb40"]
size = Vector2(1200, 20)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_feb5d"]
[node name="Root" type="Control"]
[node name="Gameplay" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_oe8ih")
[node name="Background" type="ColorRect" parent="."]
layout_mode = 1
@ -88,7 +90,7 @@ layout_mode = 1
anchors_preset = 10
anchor_right = 1.0
offset_top = 20.0
offset_bottom = 43.0
offset_bottom = 75.0
grow_horizontal = 2
[node name="Score-0" type="Label" parent="HBoxContainer"]

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