Screensaver: Side Walls
Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 25 of 28.
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.
- In
_process(), after the logo moves, add anif:position.xis more than570or less than70. - Inside it, flip
velocity.x.
Press Play: the logo bounces off the sides and still drifts off the bottom. Then Check.
Try it yourself
extends Node2D
var velocity = Vector2(200, 90)
func _process(delta):
position += velocity * delta
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