Understanding variables

A variable is a container for values that we can change or store in our programs. Let’s think about the different ways in which you can adapt or customise a simple Pacman character. You might want to:

• Draw Pacman in different places in the sketch window.
• Change the colour of Pacman.
• Make Pacman face left or right
• Turn Pacman into Ms Pacman by adding a little bow.
• Make Pacman bigger or smaller by increasing or decreasing the radius used to draw the arc (part of a circle).

We will use variables to create all these different versions of Pacman.

Integers store numbers

An integer is a whole number or zero. An integer can be negative, positive or zero. If we want to change the position where we draw Pacman we need integer variables.We need two integers because we want to be able to place Pacman on both the x and y axes.

int pacX=50;
int pacY=height/2;

Here we are using two integer variables to store the position of our pacman character. In the first case we specify a particular value (pacX), and in the second case (pacY) we calculate a value – height/2. There ‘height’ is a also a variable – a special one used to store the height of the sketch. Notice that in each case the variable consists of three components:

• The type of variable.
• The name of the variable – imagine this as a kind of box or container.
• The value of the variable – imagine this as something which is stored in the box.

We also use an integer to store the radius of Pacman. This radius controls the size of the circle (arc) that we draw.

int radius = 30;

Booleans store true or false

Sometimes we need a special kind of variable, one which can store only two values – true or false. This kind of variable is known as a Boolean. Here we use it to decide whether Pacman will be male or female.

//boolean to set the sex of the pacman characters
Boolean female = true;

Sometimes we need a special kind of variable, one which can store only two values – true or false. This kind of variable is known as a Boolean.

//boolean to set the sex of the pacman characters
Boolean female = true;

Color variables

We can also use a special kind of variable (known as an object) to change colours. These are called ‘color’  – notice the U.S. spelling! Colors are a bit different to simple integers, because we use one color to store three or four integers. These work together to create a colour. As we have discussed in a previous lesson, each colour is created by combining values for

• red
• green
• blue
• (optionally) alpha or transparency
color pink = color(255,134,241,125);

Checking the value of variables

If statements are used to check the value of a variable and then execute a different set of commands depending on the value of the variable. For example, if we set a Boolean variable “direction” whenever someone presses an arrow key, then we can check that variable before we draw the Pacman.

if (direction==0) //this checks the value of the variable 'direction', and whether it's true that it equals zero
{
//if it is true, then draw pacman arc facing right
}
else //if the condition is not true,
{
//if it is not true, then draw pacman arc facing left
}

Color variables

You can play around with the code for this sketch in this sketchpad.

int pacX=50;
int pacY=height/2;
color pink = color(255,134,241,125);
//set the radius variable for the pacman characters
int radius = 30;
//boolean to set the sex of the pacman characters
Boolean female = true;
int direction=0;
PFont font;

background(0);
size(100,100);
smooth();
noStroke();
fill(pink);
if (direction==0)
{
//draw pacman facing right
}
else
{
//draw pacman facing left
}
//draw eyes
fill(0);
if (female == true)
{
//draw ribbon for ms pacman
fill(255, 0, 0, 200);

}

fill(0, 102, 153);

//move pacman around the screen
if (keyPressed && (key == CODED)) { // If it's a coded key
if (keyCode == LEFT) {            // If it's the left arrow
pacX-=5;
direction =0;
}
else if (keyCode == RIGHT) {      // If it's the right arrow
pacX+=5;
direction=1;
}
if (keyCode == UP) {            // If it's the left arrow
pacY-=5;
}
else if (keyCode == DOWN) {      // If it's the right arrow
pacY+=5;
}
}// end if keypressed