[Tạo 2D Platformer Game với Godot] Part 24: Creating a Pause Menu – LLODO


In this part, I will guide you how to create a pause menu while playing a game and press pause then the game stops and you can continue playing, playing again, exiting.

And create a menu when the game ends, it includes: Next Level, Play again, exit the menu.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 133

I add CanvasLayer as the root node and rename it as PauseMenu

[Tạo 2D Platformer Game với Godot]  Part 24: Creating Pause Menu 134

Since it’s quite small, I adjusted the min size up.
[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 135 . Pause Menu

It got bigger when the min size was adjusted and in the text box I added 2 signs | |

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 136

After that, I added a Custom Font to make it beautiful.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 137

Color id: a77322

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 138

Then add custom Styles to make it pleasing to the eye.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 139

And I have the above result[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 140 . Pause Menu [Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 141

I added a new Control node and renamed it PauseMenu and added a Panel to make the Background then renamed it again.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 142

Then align to the center
[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 143

I create a new Custom styles.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 144 . Pause Menu

Color: a89d24[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 145 . Pause Menu

Above are the parameters.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 146 . Pause Menu [Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 147

You add the MarginConTainer node below the Background.

I use margincontainer to align it beautifully.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 148

Select Full rect

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 149

Then Theme Overrides -> Constants you fill in about 10 and customize according to your game.[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 150 . Pause Menu

Add a Vboxcontainer

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 151

After adding you will see the Vboxcontainer is indented 10mm from the margincontainer.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 152

Add the Label rename as TieuDe

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 153

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 154

Adjust align to Center to center

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 155

Add a custom font to make it beautiful[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 156

Result as above.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 157

I added 3 more buttons like the picture and 3 Ninepatchrect as spaces.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 158

Then you add custom fonts and custom styles to the button.

You can add or not add.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 159

You save the scene

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 160 . Pause Menu [Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 161

Add a script in the PauseMenu node

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 162

Then connect 4 signals of 4 buttons.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 163

Then hide the pause menu because I only show it when I press the show button.

func _on_NutTiepTuc_pressed():
    get_tree().paused = false
    $PauseMenu.hide()

Code in continue button.

get_tree().paused = false : I will tell the tree to continue working.

$PauseMenu.hide() : I will hide the PauseMenu because I want to continue playing the game.

func _on_NutChoiLai_pressed():
	get_tree().paused = false
	get_tree().reload_current_scene()
	pass # Replace with function body.


get_tree().reload_current_scene() : I will tell the tree to replay the scene.

func _on_NutVeMenu_pressed():
    get_tree().paused = false
    get_tree().change_scene("res://Scences/Map/LevelMap.tscn")
    pass # Replace with function body.

get_tree().change_scene("res://Scences/Map/LevelMap.tscn"): I will move to the Level Map scene.

func _on_HienPauseMenu_pressed():
    $PauseMenu.show()
    get_tree().paused = true
    pass # Replace with function body.

$PauseMenu.show() : is that I will show the PauseMenu because it is hidden.

get_tree().paused = true : that I will call the current tree and tell it to stop. When stopped, all nodes on the tree will stop working.

extends CanvasLayer



func _on_NutTiepTuc_pressed():
    get_tree().paused = false
    $PauseMenu.hide()



func _on_NutChoiLai_pressed():
    get_tree().paused = false
    get_tree().reload_current_scene()
    pass # Replace with function body.


func _on_NutVeMenu_pressed():
    get_tree().paused = false
    get_tree().change_scene("res://Scences/Map/LevelMap.tscn")
    pass # Replace with function body.


func _on_HienPauseMenu_pressed():
    $PauseMenu.show()
    get_tree().paused = true
    pass # Replace with function body.

So I will have the full code like this.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 164 . Pause Menu

And in the Inspector’s PauseMenu, you need to change the pause mode to Process so that when the tree pauses, but the PauseMenu still works.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 165 . Pause Menu

Then Instance to Map.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 166

Here are my results

Level Map

Oh, I forgot in the previous parts to add a back button in the Level map scene to return to the Main Menu.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 167 . Pause Menu

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a 168 . Pause Menu [Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 169

You add the Button node and then rename it. Then add custom fonts, custom styles.

[Tạo 2D Platformer Game với Godot]  Part 24: Create a 170 . Pause Menu [Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 171

Create your own script at Level Map.

Then connect the signal in the QuayVe node.

extends Node2D


func _on_QuayVe_pressed():
    get_tree().change_scene("res://Scences/UI/Menu.tscn")
    pass # Replace with function body.

About the code I have as above.

[Tạo 2D Platformer Game với Godot]  Part 24: Creating a Pause Menu 172

And I have the above result

Summary

So in this part, I showed you how to create a pause menu for the game.



Link Hoc va de thi 2021

Chuyển đến thanh công cụ