Fly Off the Edge
Part of the Projects section of Coddy's Godot journey. Lesson 13 of 36.
Thrust for a while and the ship drifts off the screen. In this game the screen wraps around. Fly off the right edge and you come back on the left. Fly off the bottom and you come back at the top.
Write one if per edge. Add or subtract the screen's width, so the ship keeps the distance it went past the edge:
if position.x < 0:
position.x += 640
if position.x > 640:
position.x -= 640Do the same for y, with the height, 360.
Challenge
MediumWrap the ship around the screen.
- After the ship moves, past the left or right edge (
0and640), bring it back on the other side. - Do the same for the top and bottom (
0and360).
Press Play and fly off the right edge: the ship comes back on the left. Then Check.
Try it yourself
extends CharacterBody2D
const TURN_SPEED = 4.0
const THRUST = 300
func _physics_process(delta):
rotation += Input.get_axis("move_left", "move_right") * TURN_SPEED * delta
if Input.is_action_pressed("move_up"):
velocity += Vector2.UP.rotated(rotation) * THRUST * delta
$Flame.visible = Input.is_action_pressed("move_up")
position += velocity * delta
All lessons in Projects
2Project: Asteroid Shooter
Turn the ShipFire the EnginePractice on your own: Godot playground