Create Pacman with functions and parameters

Now that we’ve learned about variables and functions, we can start having more fun!

Your task today is to create and save your own version of Pacman, by changing the parameters on a pacman() function in the code below:

Use this code – you can copy it from here

//Can you change the appearance of pacman by changing the parameters on this function? 
float x; //this sets the centre of pacman in the x axis
float y; //this sets the centre of pacman in the y axis
color pink = color(255,134,241); //do you want to draw a pink pacman?
color yellow = color(0,125,125); //or a yellow one?
//set the radius variable for the pacman character
float radius = 55; //decide how big you want your pacman to be
//boolean variable to set the sex of the pacman character
Boolean female = true;
int direction = 0;

void setup()
{
size(100,100);
//set initial values for the pacman coordinates
x=width/2;
//set y position of pacman
y=height/2;
} //this ends the setup() function

//the draw() function runs many times per second - useful for animations
void draw()
{
smooth();
//draw background
background(0);

//change the parameters in this function to draw your own pacman
//see what happens if you don't set one of the parameters
pacman(x, y, pink, true, radius, direction);
//Can you put in a couple of extra pacman() functions here, with different paramenters?
} //This ends the draw() function

//next we need the pacman() function - you can see that all the parameters are defined in the first line
//see what happens if you delete one of the functions
void pacman(float tempX, float tempY, color clr, Boolean tempFemale, float tempRadius, int tempDir)
{
fill(clr);
noStroke();

if (tempDir==0)
{
//draw pacman facing right
arc(tempX, tempY, tempRadius,tempRadius, radians(45), radians(315));
}
else
{
//draw pacman facing left
arc(tempX, tempY, tempRadius,tempRadius, radians(225), radians(360+135));
}
//draw eyes
fill(0);
ellipse(tempX,tempY-tempRadius/3,tempRadius/5,tempRadius/5);
if (tempFemale == true)
{
//draw ribbon for ms pacman
fill(255, 0, 0, 200);
triangle(tempX,tempY-tempRadius/2,tempX+tempRadius/3,tempY-(tempRadius/3)*2,tempX+tempRadius/3,tempY-tempRadius/3);
triangle(tempX-tempRadius/3,tempY-tempRadius/3*2,tempX,tempY-tempRadius/2,tempX-tempRadius/3,tempY-tempRadius/3);

}
}

//this function saves your pacman image
void mousePressed()
{
save("ms_pacman.png");
}
Advertisements

5 thoughts on “Create Pacman with functions and parameters

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s