Screensaver: New Colors
Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 27 of 27.
The last step gives the logo a new color on every bounce. Keep the colors in a list, written in square brackets. colors[0] is the first color, colors[1] the second. An index says which color comes next:
var colors = [
Color(0.49, 0.95, 0.75),
Color(1, 0.78, 0.34),
]
var color_index = 0All four walls do the same thing, so put it in one function that each bounce calls. It sets modulate to the color at color_index, then adds 1 to the index.
When the index reaches colors.size(), there is no color there. Set it back to 0 and the colors start over.
Challenge
Easy- Add a node variable
colors: a list of at least three colors (the course's mint, amber, coral and blue areColor(0.49, 0.95, 0.75),Color(1, 0.78, 0.34),Color(1, 0.42, 0.42)andColor(0.369, 0.635, 1)), andcolor_index = 0. - Write
next_color(): setmodulateto the color atcolor_index, add 1 to the index, and set it back to 0 at the end of the list. - Call it on every bounce: off the sides, the top and the bottom.
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
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