Drifting Rocks
Part of the Projects section of Coddy's Godot journey. Lesson 16 of 36.
The rocks need to move. rock.gd drifts each rock at its velocity, spins it slowly and wraps it like the ship.
A rock is big, so it wraps at -30 and 670 instead of 0 and 640. That way it leaves the screen fully before it comes back. The jump is 700, the width of that wider strip:
if position.x > 670:
position.x -= 700The left edge and the top and bottom work the same way, with -30, 390 and a jump of 420.
Challenge
HardMake the rocks drift, spin and wrap.
- In
rock.gd, add_physics_process: move byvelocity * deltaand add0.5 * deltatorotation. - Past
-30or670on x, move it700the other way. - Past
-30or390on y, move it420the other way.
Press Play: the rocks tumble across the screen and come back. Then Check.
Try it yourself
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
All lessons in Projects
2Project: Asteroid Shooter
Turn the ShipFire the EnginePractice on your own: Godot playground