Menu
CoddyTech

Fly Off the Edge

Part of the Projects section of Coddy's Godot journey. Lesson 12 of 33.

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 -= 640

Do the same for y, with the height, 360.

challenge icon

Challenge

Medium

After the ship moves, wrap it around the screen:

  • Past the left or right edge (0 and 640), it comes back on the other side.
  • The same for the top and bottom (0 and 360).

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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Projects

Practice on your own: Godot playground