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
EasyMake the logo bounce off the top and bottom too.
- Under the first
if, add a second one:position.yis more than330or less than30. - 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
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