Screensaver: Side Walls
Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 25 of 27.
When the logo hits the right edge, its x speed must point the other way, from 200 to -200. A minus sign in front of a value flips it:
velocity.x = -velocity.xAt the left edge the same line turns -200 back into 200. Only x flips, so the logo keeps drifting down.
The logo's position is its middle, and the plate is 140 pixels wide. Its right side touches the edge when its middle is at 640 - 70 = 570, and its left side when its middle is at 70.
or joins two conditions. The if runs when either one is true:
if position.y > 300 or position.y < 60:Challenge
EasyMake the logo bounce off the left and right edges. When its middle passes x = 570 or x = 70, flip velocity.x. It still drifts off the bottom for now.
Try it yourself
extends Node2D
var velocity = Vector2(200, 90)
func _process(delta):
position += velocity * delta
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