Menu
CoddyTech

心数耗尽

本课属于 Coddy Godot 学习旅程的“项目”单元。第 35 课,共 36 课。

最后一颗心消失后,游戏就结束了。隐藏心形后,立即检查心形数量是否为 0:

if health == 0:
	$HUD/Message.text = "Game Over"
	get_tree().paused = true

英雄和守卫属于 Pausable,因此会冻结。Main 属于 Always,因此它的 _process 会继续运行。玩家按下 Enter 时,它会开始新游戏:

func _process(delta):
	if get_tree().paused and Input.is_action_just_pressed("action"):
		get_tree().paused = false
		get_tree().reload_current_scene()

重新加载会重置所有内容:三颗心、地上的钥匙,以及上锁的门。

challenge icon

挑战

中等

生命值耗尽时结束游戏。

  1. 当 health 变为 0 时,在 $HUD/Message 中显示 Game Over,并暂停游戏。
  2. 暂停时,按 Enter(action)可取消暂停并重新加载场景。

按“开始”,失去 3 颗心后会显示 Game Over。按 Enter 重新开始。然后按“检查”。

亲手试试

extends Node2D

var has_key = false
var health = 3

func _ready():
	$Key.body_entered.connect(_on_key_body_entered)
	$DoorZone.body_entered.connect(_on_door_zone_body_entered)
	$Enemy.body_entered.connect(_on_enemy_body_entered)

func _on_key_body_entered(body):
	has_key = true
	$Key.queue_free()
	$HUD/KeyIcon.visible = true

func _on_door_zone_body_entered(body):
	if has_key:
		$Door.queue_free()
		$DoorZone.queue_free()

func _on_enemy_body_entered(body):
	health -= 1
	$HUD/Hearts.get_child(health).visible = false
	$Hero.flash()

项目中的所有课

自己练习: Godot 在线编辑器