image Tutorial on Integrating with Variable Attributes

How would you describe a rabbit moving in the shape of a square? Specifically, how would you tell a rabbit to first move right, then down, then left, and then up? Since characters follow the same set of action rules in every frame of the game, you cannot tell the rabbit to go right, down, left and up all at once. You need a way to describe these different actions in a single instant of time. You also need a way to track how far the rabbit has moved in every frame. A solution to both of these problems is to have attributes that use the frames themselves to keep count. These special attributes are called variable attributes. They are sometimes also referred to as 'integer attributes', if the values they take are only of the integer type. Unlike Boolean attributes, they are not limited to two values.

To use variable attributes, use the format "[attribute] of object". For example, in the sentence, "The state of the rabbit is initially 0," the variable attribute is "state" and starts count at 0. Take a look at how variable attributes are used in the following game plan to make the rabbit move in the shape of a square:

There are two rabbits.
The state of the rabbit is initially 0.
The num_steps of the rabbit is initially 0.
When the state of the rabbit is equal to 0, it moves right.
When the state of the rabbit is equal to 1, it moves down.
When the state of the rabbit is equal to 2, it moves left.
When the state of the rabbit is equal to 3, it moves up.
The num_steps of the rabbit increases every frame.
When the num_steps of the rabbit is greater than or equal to 50, the state of the rabbit increments and the num_steps of the rabbit becomes 0.
When the state of the rabbit is greater than 3, the state of the rabbit becomes 0.
There are two variable attributes in this game plan: "state" and "num_steps". The attribute num_steps count the frames. After 50 frames, the num_steps counter resets, and the state counter goes up by 1. The rabbit changes direction with every new state number. Since the default speed for GameChangineer character is 1 pixel per frame, the rabbit will make a 50x50 pixel square.

We can use variable attributes for many other things as well. For example, suppose we want to design a game where the rabbit eats carrots. But you win when half of the carrots are eaten. We can do so in the following:

There are 20 carrots and one rabbit.
You control the rabbit.
When an arrow key is pressed, the rabbit moves in the same direction.
When the rabbit touches a carrot, the carrot is eaten.
When a carrot is eaten, the num_carrots_eaten of the rabbit increases.
When the num_carrots_eaten of the rabbit is 10, you win.


Now, suppose there is also a piglet in the game. We would like to check the difference between the number of the carrots eaten between the rabbit and the piglet. We can do so as follows:

...
When the rabbit touches a carrot, the carrot is eaten and the num_carrots_eaten of the rabbit increases.
When the piglet touches a carrot, the carrot is eaten and the num_carrots_eaten of the piglet increases.
When the rabbit is not dead, the x_diff of the rabbit becomes the num_carrots_eaten of the rabbit.
When the rabbit is not dead, the x_diff of the rabbit subtracts the num_carrots_eaten of the piglet.
When the x_diff of the rabbit is greater than 5, you win.


There are also built-in variable attributes, such as 'position_x', 'position_y' that hold the current position coordinates of the character. There are also 'size' and 'angle' that indicates the size and angle of the object. Note that currently, only comparisons of attributes is supported. That is, you cannot have arithmetic expressions involving attributes in the conditional.
Try it yourself: Try the following paragraph and see what happens.

Example 1:
There is a rabbit.
The time_left of the rabbit is initially 50.
The time_left of the rabbit decreases every frame.
When the time_left of the rabbit equals 0, game is over.

Example 2:
There are a fox and a rabbit.
When the position_x of the rabbit is greater than the position_x of the fox, the rabbit moves left.
When the position_x of the rabbit is less than the position_x of the fox, the rabbit moves right.

Example 3:
There are 5 carrots and a rabbit.
You control the rabbit with the mouse.
When the the rabbit touches a carrot, the size of the rabbit increases.


Complete Sample Games for Advanced Variable Attributes

// Game #1. Get All Spinstars.

You control the kitten.
When an arrow key is pressed, the kitten moves in the same direction.

The topaz is invisible. The diamond is invisible. Rocks are invisible.
When the kitten reaches the border, the kitten stops.
When the kitten reaches with the wall, the kitten reverses direction.
The elephant moves up.
When the elephant reaches the topaz, the elephant becomes notified.
When the elephant reaches the diamond, the elephant is not notified.
When the elephant is notified, the elephant moves down.
The tigers move around.
When the tiger collides with the wall, the tiger reverses direction.
When the tiger collides with the rock, the tiger reverses direction.
When the kitten collides with the spinstar, the spinstar disappears.
When the kitten collides with the spinstar, the score increases by 1.
The state of the cobra is initially 0.
The num_steps of the cobra is initially 0.
When the state of the cobra is equal to 0, it moves right.
When the state of the cobra is equal to 1, it moves down.
When the state of the cobra is equal to 2, it moves left.
When the state of the cobra is equal to 3, it moves up.
The num_steps of the cobra increases every frame.
When the num_steps of the cobra is greater than or equal to 56, the state of the cobra increments and the num_steps of the cobra becomes 0.
When the state of the cobra is greater than 3, the state of the cobra becomes 0.
When the cobra collides with the wall, the cobra stops.

When you collide with a tiger, it is gameover.
When you collide with an elephant, it is gameover.
When you collide with a cobra, it is game over.
When the score is equal to 27, you win.

Map:

sFwwwwwwwwwwwwwww-=-
w---ooow----ooowwFsw
w-ww---wB--w---ws-ww
w-wwI--w-s-w---ww-sw
w-ww---w---wI--ws-ww
w-ww---wB-sw---ww-sw
w-ww--Iws--w---ws-ww
w-sw---w---w---ww-sw
w-ww---wBs-w---ws-ww
w-ww---w---w---ww-sw
w-ww---ws--w---ws-ww
w-ww---wB--wI--ww-sw
w-ww---w-s-w---ws-ww
s-ww-I-w---w---ww-sw
w-ww---wB-sw---ws-ww
w-ww---w---w---ww-sw
w-wwI--w---w---ws-ww
wjwwoooww--w--Iww-sw
wdww-------wooowwjww
wiwwwwwwwwww-----dww

// Game #2. Get All Spinstars 2.

Wide canvas with 2 frames.
The size of kitten is 20.
The size of coyotes is 20.
The size of dino is 20.
The size of cobra is 20.
The size of koala is 20.
The speed of kitten is 4.
The speed of coyote is 1.5.
The speed of dino is 3.

//Player Control.
You control the kitten.
When the arrow is pressed, the kitten moves same direction.

//Collision.
When the kitten collides with a topaz, it reverses direction.
When the kitten collides with a spinstar, the spinstar explodes and score increases by 1 point.
When the kitten collides with a dino or a coyote, it explodes.
When the kitten collides with a cobra or a koala, it explodes.

//Coyote Patrol.
The state of the coyote is initially 0.
The num_steps of the coyote is initially 0.
When the state of the coyote is equal to 0, it moves right.
When the state of the coyote is equal to 1, it moves down.
When the state of the coyote is equal to 2, it moves left.
When the state of the coyote is equal to 3, it moves up.
The num_steps of the coyote increases every frame.
When the num_steps of the coyote is greater than or equal to 40, the state of the coyote increments and the num_steps of the coyote becomes 0.
When the state of the coyote is greater than 3, the state of the coyote becomes 0.
//Dino Patrol.
The state of the dino is initially 0.
The num_steps of the dino is initially 0.
When the state of the dino is equal to 0, it moves right.
When the state of the dino is equal to 1, it moves down.
When the state of the dino is equal to 2, it moves left.
When the state of the dino is equal to 3, it moves up.
The num_steps of the dino increases every frame.
When the num_steps of the dino is greater than or equal to 40, the state of the dino increments and the num_steps of the dino becomes 0.
When the state of the dino is greater than 3, the state of the dino becomes 0.

//Cobra patrol.
The cobras start by moving right.
When a cobra collides with a topaz, it reverses direction.

//Koala patrol.
The koalas start by moving left.
When a koala collides with a topaz, it reverses direction.

//Game over.
When the kitten is gone, you lose.
When score is 12, you win.

Map:

FFFFFFFFFFFFFFFFFFFFFFFF
Fi-D----D----D----D----F
FFF-Q----Q----Q----Q---F
-=F--F----F----F----F--F
--F--------------------F
--Fs----s----s----s----F
--FFFFFFFFFFFFFFFFFFFF-F
--FD----D----D----D----F
--FsQ---sQ---sQ---sQ---F
--F--F----F----F----F--F
--F--------------------F
--F--------------------F
--FsFFFFFFFFFFFFFFFFFFFF
--FB-FFFFB--------sFFFFF
--F-AFFFF---------BFFFFF
--FB-FFFFB-FFFFFFB-FFFFF
--F-AFFFF-AFFBsFF-AFFFFF
--FB-------FF-AFFB-----F
--F-------BFF--------BsF
--FFFFFFFFFFFFFFFFFFFFFF


Programming Concepts (optional material):
We have learned about attributes and how they can help us program our games. So far, the custom attributes are all of Boolean type. That is, the values of an attribute can only be either true or false. It is possible for an object to have non-Boolean attributes as well. One example would be to have a counter attribute that counts how long the object has traveled, such as the 'num_steps' attributes that we used in the lesson above. These variable attributes help us remember how many of some object we have eaten, how far we have traveled, etc. And they can be very useful to designing more complex games.

Next page

Tutorials Home