Menu
CoddyTech

通过 10 个管道获胜

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

玩家现在可以输,但还不能赢。当分数达到 10 时,显示 You win! 并暂停。在分数增加的地方测试:

if score == 10:
	$HUD/Message.text = "You win!"
	get_tree().paused = true

最后,让玩家可以再次游玩。游戏暂停时,按 Space 重新加载场景。Main 的模式是 Always,所以它的 _process 仍会运行:

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

Space 也是拍打翅膀的按键。飞行器的模式是 Pausable,所以游戏暂停时,它会忽略按键操作。

challenge icon

挑战

中等

通过 10 个管道后获胜,然后再玩一次。

  1. 分数达到 10 时,在 $HUD/Message 中显示 You win!,并暂停游戏。
  2. 暂停时,按空格键(jump)可取消暂停并重新加载场景。

按“开始”,通过 10 个管道:屏幕会显示 You win!。按空格键重新开始。然后按“检查”。

亲手试试

extends Node2D

const PIPE = preload("res://pipe.tscn")

var score = 0

func _ready():
	$PipeTimer.timeout.connect(_on_pipe_timer_timeout)
	$Flyer.crashed.connect(_on_crash)
	$HUD/Score.text = "0"

func _on_pipe_timer_timeout():
	var pipe = PIPE.instantiate()
	pipe.position = Vector2(700, randf_range(100, 250))
	pipe.hit.connect(_on_crash)
	pipe.scored.connect(_on_scored)
	$Pipes.add_child(pipe)

func _on_crash():
	$HUD/Message.text = "Game Over"
	get_tree().paused = true

func _on_scored():
	score += 1
	$HUD/Score.text = str(score)
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

项目中的所有课

自己练习: Godot 在线编辑器