打砖块生命值
本课属于 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 那样监听这个信号。它调用的函数会扣除一条命并显示新的命数,就像计分函数会更新分数一样。
挑战
困难给玩家 3 条命。
- 在
ball.gd中添加signal missed。在y = 380下方发出信号,并像上面那样重新发球。 - 在
main.gd中添加var lives = 3,并在$HUD/Lives中显示Lives: 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 在线编辑器