Open the ‘main.gd‘ script.
We start by adding constants right after the ‘extends Node2D‘ line. And then we add the ‘func _ready()‘ and ‘_on_viewport_size_changed()‘ functions.
extends Node2D
const GAME_OVER_SCENE := preload("res://game_over.tscn")
@onready var game_root: Node2D = $GameRoot
@onready var board: Node2D = $GameRoot/Board
@onready var hud_left: Control = $GameRoot/HUDLeft
@onready var hud_right: Control = $GameRoot/HUDRight
@onready var ui_root: Control = $GameRoot/UILayer/UIRoot
@onready var label_score_value: Label = $GameRoot/HUDLeft/VBoxContainer/LabelScoreValue
@onready var label_highscore_value: Label = $GameRoot/HUDLeft/VBoxContainer/LabelHighScoreValue
@onready var label_lines_value: Label = $GameRoot/HUDLeft/VBoxContainer/LabelLinesValue
@onready var next_preview: Node2D = $GameRoot/HUDRight/VBoxContainer/NextPreview
func _ready() -> void:
board.connect("lines_cleared", Callable(self, "_on_lines_cleared"))
board.connect("next_piece_changed", Callable(self, "_on_next_piece_changed"))
board.connect("game_over", Callable(self, "_on_game_over"))
# Center once and whenever the window size changes
get_viewport().size_changed.connect(_on_viewport_size_changed)
_on_viewport_size_changed()
func _on_viewport_size_changed() -> void:
var window_size: Vector2 = get_viewport_rect().size
var layout_bounds := _get_layout_bounds()
var layout_center := layout_bounds.position + layout_bounds.size * 0.5
var target_center := window_size * 0.5
# Move the whole game so the *layout* center matches the window center
game_root.position = target_center - layout_centerNow we add a function ‘func _on_lines_cleared()‘ to update over scores.
func _on_lines_cleared(total_lines: int, score: int, highscore: int) -> void:
label_score_value.text = str(score)
label_highscore_value.text = str(highscore)
label_lines_value.text = str(total_lines)We add a function to get layout bounds ‘func _get_layout_bounds()‘.
func _get_layout_bounds() -> Rect2:
# Board rectangle (we know its size from constants)
var board_size := Vector2(
board.BOARD_WIDTH * board.CELL_SIZE,
board.BOARD_HEIGHT * board.CELL_SIZE
)
var bounds := Rect2(board.position, board_size)
# Merge HUDLeft and HUDRight rectangles into the bounds
if hud_left:
bounds = bounds.merge(hud_left.get_rect())
if hud_right:
bounds = bounds.merge(hud_right.get_rect())
return boundsWe add a function to change next_preview – ‘func _on_next_piece_changed()‘.
func _on_next_piece_changed(shape_key: String, shapes: Dictionary, colors: Dictionary) -> void:
next_preview.update_preview(shape_key, shapes, colors)Add a game over handler ‘func _on_game_over()‘.
func _on_game_over(score: int, highscore: int, total_lines: int) -> void:
var game_over_layer := GAME_OVER_SCENE.instantiate()
game_over_layer.final_score = score
game_over_layer.final_highscore = highscore
game_over_layer.total_lines = total_lines
ui_root.add_child(game_over_layer)
# Make sure it fills the screen relative to UIRoot
game_over_layer.set_anchors_preset(Control.PRESET_FULL_RECT)Add a reset game function ‘func reset_game()‘.
func reset_game() -> void:
if board.has_method("reset_game"):
board.reset_game()This concludes the ‘main.gd‘ script. Save it.
Leave a Reply