Menu
CoddyTech

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 icon

Challenge

Easy

Give the logo the next color on every bounce.

  1. Write func next_color():. It adds 1 to color_index, sets it back to 0 when it equals colors.size(), then sets modulate = colors[color_index].
  2. Call next_color() in both ifs, 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Nodes and Scenes

Practice on your own: Godot playground