Menu
CoddyTech

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 = 0

All 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 icon

Challenge

Easy
  • Add a node variable colors: a list of at least three colors (the course's mint, amber, coral and blue are Color(0.49, 0.95, 0.75), Color(1, 0.78, 0.34), Color(1, 0.42, 0.42) and Color(0.369, 0.635, 1)), and color_index = 0.
  • Write next_color(): set modulate to the color at color_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
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