In the previous article From the problem to the solution: a video game to raise awareness about energy waste you started a journey to discover how to make a video game, aiming to raise awareness among players on the issues of energy sustainability and responsible consumption.

You started from an idea and came to a solution, analyzing a series of small problems.

Now it's time to write the code that implements this solution and you'll do it with Scratch: here you'll find the complete project.

Let's examine the steps one at a time.

Problem 1: a person must appear in one of the three rooms and turn on the light in that room and only in that.

figure 1
Solution: the light and the darkness of a room are costumes of the Light panel sprite. When the Light panel sprite touches the Person sprite (figure 1) the Light panel sprite switches to the Yellow costume (the room lights up).

Problem 2: people showing in the house must stay within the perimeter of the house without materializing themselves over the walls - it is not a ghost video game!

figure 2
Solution: when the Person sprite is cloned she remains hidden and positioned in a random position inside the boundary of the house. If the causal position is close to or over a wall (the sprite touches the Black color of the walls) the clone is deleted immediately (figure 2).

Problem 3: when the player touches the switch (the Light bulb sprite), the room light turns off.

figure 3
Solution: when the player touches the Light bulb sprite, it sends a Switch off message to the corresponding Light panel (figure 3).
figure 4

When Light panel sprite receives the message Switch off it switches to  Black costume (figure 4) and the light turns off.

Please note that the code actually involves switching first to the Gray costume and then to the Black one. This trick became necessary because, when the Person sprite is cloned, for a fraction of a second it touches the Light panel sprite with the Black costume and, when this happens, the game ends with Game over (see Problem 4 and figure 5): the algorithm interprets this event (Person sprite touches Black color) in the same way that it interprets the gesture of the player who turns off the light while the person is still inside the room. If you want to reproduce this unwanted behavior, remove the transition to the Gray costume from the code in Figure 4 and go directly to the Black costume.

It was an unexpected event which, after the test has been performed, required the identification of an alternative solution: you can't say that the alternative solution chosen is the only one or the best one, but it has certainly solved the problem.

Problem 4: if the player touches the Light bulb before the person has left the room, the game ends.

figure 5

Problem 5: when the light is switched on, the consumption counter starts.

figure 6
Solution: the energy counter is activated by setting the Switch on variable to 1 (figure 6).

Problem 6: when the player turns the light off the consumption counter stops.

figure 7
Solution: the energy counter is stopped by setting the Switch on variable to zero (figure 7).

Problem 7: when the stopwatch reaches zero the game ends and the consumption counter shows the total consumption expressed by KWh.

figure 8
Solution: the hours counter is decremented by 1 unit every second. When it reaches zero seconds, it sends the message You win to the sprite You win and the relative message appears on the screen (figure 8).

Final test

The game is over: you can run a final test and fix the last details. Do not forget to indicate in the "credits" section any resources borrowed from other authors.

What are you waiting for now?!? Share the video game and play with your friends !!!

Next steps

Try now to imagine how the video game can be further improved and made more attractive.

For example, you could insert background music and sounds linked to certain events that occur during the game: for example, when the light is turned on or when the game ends, due to a player's error.

You could insert a life counter or health bar system for the player. For example, the game ends after the third error (the player turned off the light while the person was still in the room) instead of the first occurrence.

To make players more aware, you could insert a screen at the end of the game that indicates the consumption of the individual bulbs.

Or you could give the player the possibility to choose the type of light bulb (filament or led) before starting the game, so that she can realize the different consumption.

Add some electronics

Arduino Uno

Do you know Arduino and Makey Makey? Why don't you try to use them to transfer your project from a digital world to the real world?

With Arduino and Scratch you could, for example, build the model of the house with the 3 rooms, 3 light bulbs and 3 switches and play your video game in the real world!

With Makey Makey and Scratch you could build a console with 3 keys made of modeling clay having the shape of a light bulb and use them as switches.

Think about it, I'm sure you'll find other fantastic starting points!

Don't want to use Scratch

Do you already know Scratch or you don't like it (seriously?!?!) and you want to explore another programming language? Below are some suggestions.

I propose a breakdown by age, but feel free to explore all the tools at any age.

ages 7 to 12:

mBlock
microbit

ages 13 and up:

AppInventor
Thunkable
Arduino con Wiring
Python con Pygame