Menu
CoddyTech

Screensaver: Top and Bottom

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

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 edges too. When its middle passes y = 330 or y = 30, flip velocity.y. The logo then stays on the screen.

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

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

All lessons in Nodes and Scenes

Practice on your own: Godot playground