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
EasyMake 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Nodes and Scenes
1Your First Godot Game
What Is Godot?Ready Runs OncePrint in GodotThe Scene TreeSet a PositionMove a Node Once2Every Frame
Process Runs Every FrameDeltaPixels per SecondMove DiagonallySpin With RotationGrow With Scale5Project: Screensaver
Screensaver: DriftScreensaver: Side WallsPractice on your own: Godot playground