Menu
CoddyTech

打砖块生命值

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

如果挡板没接住球,球就会从底部掉出去。给玩家三条命。

在 ball.gd 中,如果球低于 y = 380,就表示没接住。发出 missed 信号,并在挡板上方重新发球:

if position.y > 380:
	missed.emit()
	position = Vector2(320, 290)
	velocity = Vector2(110, -280)

在 main.gd 中,像监听 brick_broken 那样监听这个信号。它调用的函数会扣除一条命并显示新的命数,就像计分函数会更新分数一样。

challenge icon

挑战

困难

给玩家 3 条命。

  1. 在 ball.gd 中添加 signal missed。在 y = 380 下方发出信号,并像上面那样重新发球。
  2. 在 main.gd 中添加 var lives = 3,并在 $HUD/Lives 中显示 Lives: 3。
  3. 将 $Ball.missed 连接到 _on_missed,该函数会扣除一条命并显示剩余命数。

按下“开始”,然后故意让球落空:标签会显示 Lives: 2,并出现一个新球。然后按下“检查”。

亲手试试

extends Node2D

const BRICK = preload("res://brick.tscn")
const COLORS = [Color(1, 0.42, 0.42), Color(1, 0.78, 0.34), Color(0.49, 0.95, 0.75), Color(0.369, 0.635, 1), Color(0.75, 0.82, 0.95)]

var score = 0

func _ready():
	for row in 5:
		for column in 8:
			var brick = BRICK.instantiate()
			brick.position = Vector2(68 + column * 72, 60 + row * 26)
			brick.color = COLORS[row]
			add_child(brick)
	$Ball.brick_broken.connect(_on_brick_broken)
	$HUD/Score.text = "Score: 0"

func _on_brick_broken():
	score += 10
	$HUD/Score.text = "Score: %d" % score

项目中的所有课

自己练习: Godot 在线编辑器