Menu
CoddyTech

Screensaver: Top and Bottom

Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 26 of 28.

The top and bottom edges work like the sides, but on y. A bounce there flips velocity.y.

The plate is 60 pixels tall, so its middle is 30 pixels from its top and its bottom. The logo touches the bottom when its middle is at 360 - 30 = 330, and the top when its middle is at 30.

Write a second if under the first. Each if is checked on its own, so in a corner the logo bounces off a side and the bottom on the same frame.

challenge icon

Challenge

Easy

Make the logo bounce off the top and bottom too.

  1. Under the first if, add a second one: position.y is more than 330 or less than 30.
  2. Inside it, flip velocity.y.

Press Play: the logo bounces around the screen and never leaves it. Then Check.

Try it yourself

extends Node2D

var velocity = Vector2(200, 90)

func _process(delta):
	position += velocity * delta
	if position.x > 570 or position.x < 70:
		velocity.x = -velocity.x

All lessons in Nodes and Scenes

Practice on your own: Godot playground