Menu
CoddyTech

暂停游戏

本课属于 Coddy Godot 学习旅程的“游戏规则”单元。第 13 课,共 29 课。

游戏结束后,玩家仍然可以四处走动。用一行代码暂停整个游戏:

get_tree().paused = true

get_tree() 是场景树,它包含游戏中的所有节点。暂停时,_process 和 _physics_process 会停止运行。计时器会停止计时,所有对象都不会移动或发生碰撞。屏幕会继续显示最后一帧。

get_tree().paused = false 会让一切重新运行。

challenge icon

挑战

简单

失去最后一条命时暂停游戏。

  1. 找到显示“游戏结束”的 if 语句。
  2. 在它里面的那一行下方,将 get_tree().paused 设为 true。

按“开始”并失去 3 条命:游戏结束界面会显示,玩家无法移动。然后按“检查”。

亲手试试

extends Node2D

var lives = 3

func _ready():
	$HUD/Lives.text = "Lives: %d" % lives
	$Player.hurt.connect(_on_player_hurt)

func _on_player_hurt():
	lives -= 1
	$HUD/Lives.text = "Lives: %d" % lives
	if lives == 0:
		$HUD/GameOver.visible = true

游戏规则中的所有课

自己练习: Godot 在线编辑器