Screensaver: New Colors
Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 28 of 28.
The last step gives the logo the next color on every bounce. All four edges do the same thing, so put it in one function, next_color(), and call it from each bounce.
The function moves color_index on by 1, then shows colors[color_index]. With three colors the index goes 1, 2, then 3, where there is no color. So when it equals colors.size(), the number of items, set it back to 0:
if color_index == colors.size():
color_index = 0== asks whether two values are equal. A single = sets a value.
Challenge
EasyGive the logo the next color on every bounce.
- Write
func next_color():. It adds 1 tocolor_index, sets it back to0when it equalscolors.size(), then setsmodulate = colors[color_index]. - Call
next_color()in bothifs, under the flip.
Press Play: each bounce shows the next color from your list. Then Check.
Try it yourself
extends Node2D
var velocity = Vector2(200, 90)
var colors = [Color(0.49, 0.95, 0.75), Color(1, 0.78, 0.34), Color(1, 0.42, 0.42)]
var color_index = 0
func _ready():
modulate = colors[0]
func _process(delta):
position += velocity * delta
if position.x > 570 or position.x < 70:
velocity.x = -velocity.x
if position.y > 330 or position.y < 30:
velocity.y = -velocity.y
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