Level 03 Lesson 01 [Intro to Variables]

Lets start learning how you can use variables in your programs.

Resources You Will Use
It’s Level 03 now, so remember to look for sprites, sounds, and other resources in the Lv03 resource folder.

Part A – Creating and Changing Instance Variables
To save time, a pre-made game project has been made that will help us learn about variables. Go the resources projects folder and open the game project called L03-01-StartHere.gmk .

Watch the video:  03-01-A
Done File: L03-01-A-Done.gmk


Challenge 1– Add the Following
Here are few programming tasks to add.

a) Give the player a new variable called money. When the player collides with a coin, add one to the player’s money and destroy the coin. Use a show message to confirm it is working!

b) Since the player can gain points, lets make a way for the player to lose points. Add the code so that when an arrow hits a wall (misses a ghost) the player loses 2 points. Use a show message to confirm it is working.

Challenge 1 Solution
Solution video: 03-01-X1Solution
Solution file: L03-01-X1-Done.gmk


Part B – Drawing Variables
Continue with the file you are working on.
Watch the video: 03-01-B
Done File: L03-01-B-Done.gmk

Challenge 2– Add the Following
This challenge will have a few more tasks to code so that you can get confident with the basic use of instance variables.

a) When a player picks up gold, add 10 to their money. Remember to destroy the gold. Play sound pickup.

b) Give the player a variable called life. It should start at 100. Use the draw object to draw this variable to the screen.
When the player touches the poison object they should lose 1 life but don’t destroy the poison! Watch how fast life goes down when they player touches the poison.

c) Eating apples will give the player 25 more life.

d) Touching the blue potion will bring you to 100 life instantly.

e) Code this in the WALL object. When the wall collides with the player, lower the player life by one.

Challenge 2 Solution
Solution video: 03-01-X2Solution
Solution file: L03-01-X2-Done.gmk