Today’s post is going to be pretty short. The goal today was to add a start screen and a victory/defeat condition.
For the start screen, into our main Scene tree I added in a new label, with very similar settings to the first. From code I can easily turn this label on and off. As a child node to the main label I added another label with smaller text that says “Press R to Start” to tell the player how to start the game. Now when the game boots up this is what you see.
From here I added a bit of code to the game controller and to the PlayerPaddle that essentially makes it so nothing happens when the game is not in an “active state”
Player Paddle.gd has a similar change with a new variable, is_active that we check in _process before obeying any input.
In that first block you’ll likely notice a check to a new action as well. In our input map I also added an action to start the game by pressing ‘r’. In the future I might just instead have it also happen on spacebar, but for now this works.
Here I’ve added in a max_score, set to 3 for now for testing purposes, as well as a center_text variable. This is where I can easily set the text for that main label, like you see in the above screengrab.
Finally, at the bottom of GameController.gd there is some logic that handles checking for victory/loss.
And with that it’s now possible to win (or lose).
Sorry for the condensed nature of today’s post, but I didn’t have a lot of time for today’s changes. Hopefully future posts won’t be as rushed.
Here is now the current version, in full, of the GameController script.
extends Node2D var ballscene = load("res://Ball.tscn") onready var comPaddle = get_node("Com Paddle") onready var playerPadd = get_node("Player Paddle") var current_ball var is_active var center_text export (int) var max_score = 3 var playerScore = 0 var comScore = 0 func _init(): is_active = false center_text = "PONGODOT" func _process(_delta): if (!is_active): $CenterLabel.visible = true $CenterLabel.text = center_text if Input.is_action_just_pressed("start"): _reset() if (is_active): $CenterLabel.visible = false $PlayerScoreLabel.text = str(playerScore) $ComScoreLabel.text = str(comScore) if Input.is_action_just_pressed("launch"): if current_ball != null: return current_ball = ballscene.instance() current_ball.position.x = playerPadd.position.x + 20 current_ball.position.y = playerPadd.position.y add_child(current_ball) comPaddle.ball = current_ball func _end_game(): is_active = false playerPadd.is_active = false func _reset(): playerScore = 0 comScore = 0 is_active = true playerPadd.is_active = true func _delete_ball(): if current_ball != null: current_ball.queue_free() current_ball = null comPaddle.ball = null func _on_PlayerGoal_body_entered(body): _delete_ball() comScore += 1 _check_victory() func _on_ComGoal_body_entered(body): _delete_ball() playerScore += 1 _check_victory() func _check_victory(): if playerScore >= max_score: center_text = "You Win!" _end_game() elif comScore >= max_score: center_text = "Game Over" _end_game()
See you tomorrow