Menu
CoddyTech

漂移的小行星

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

岩石需要移动。rock.gd让每块岩石以自己的velocity移动,缓慢旋转,并像飞船一样绕屏幕循环。

岩石很大,所以它会在 -30 和 670 处循环,而不是在 0 和 640 处。这样,它会完全离开屏幕后再回来。跳跃距离是 700,也就是这个更宽区域的宽度:

if position.x > 670:
	position.x -= 700

左边缘以及顶部和底部也采用相同的方式,数值分别是 -30、390,跳跃距离为 420。

challenge icon

挑战

困难

让岩石漂移、旋转并绕回屏幕另一侧。

  1. 在 rock.gd 中添加 _physics_process:移动 velocity * delta,并给 rotation 加上 0.5 * delta。
  2. 如果 x 超出 -30 或 670,就向反方向移动 700。
  3. 如果 y 超出 -30 或 390,就向反方向移动 420。

按下“开始”:岩石会在屏幕上翻滚,然后绕回来。接着点击“检查”。

亲手试试

extends Area2D

var velocity = Vector2.ZERO

# Bursts into dust where the rock is. Call it when the rock breaks.
func burst():
	var dust = CPUParticles2D.new()
	dust.position = position
	dust.one_shot = true
	dust.explosiveness = 1.0
	dust.amount = 20
	dust.lifetime = 0.7
	dust.spread = 180.0
	dust.gravity = Vector2.ZERO
	dust.initial_velocity_min = 30.0
	dust.initial_velocity_max = 120.0 * scale.x
	dust.scale_amount_min = 2.0
	dust.scale_amount_max = 5.0
	dust.color = Color(0.75, 0.82, 0.95)
	dust.finished.connect(dust.queue_free)
	get_parent().add_child(dust)
	dust.emitting = true

项目中的所有课

自己练习: Godot 在线编辑器