xxxxxxxxxx
225
//For this assignmnet I used my previous assignment and class example
//a global variable to control the narrative flow
var storyStage = "initial Stage";
var X1 = 100;
var Y1 = 100;
var X2 = 100;
var Y2 = 330;
var X3 = 430;
var Y3 = 70;
var X4 = 430;
var Y4 = 330;
i = 1.85 // mouth radian for upper
x = 0.2 // mouth radian for lower
speed = 0.003 // speed of mouth for opening and closing
function setup() {
createCanvas(500, 400);
textSize(20);
textFont('georgia');
}
function draw() {
background(232, 238, 242);
//Draws the canvas based on the current story's stage
if (storyStage == "initial Stage") {
//just displays the welcome pac-pac
} else if (storyStage == "Stage 1") {
//displays the message and draws the character
updateMessage("I'm the pacman.. ");
drawPacman(70, 150);
} else if (storyStage == "Stage 2") {
//displays the message and draws the character
updateMessage("I am here to find the ghosts..where are they?");
drawPacman(250, 200);
} else if (storyStage == "Stage 3") {
//displays the message and draws the character
updateMessage("We so dont want to get eaten by pacman today..");
//ghosts having a conversation together
drawGhost1(100, 200);
drawGhost2(100, 350);
drawGhost3(200, 300);
} else if (storyStage == "Stage 4") {
//displays the message and draws the character
updateMessage("Oh no he is right there..");
//ghosts and Pac-pac having a conversation together
drawPacman(100, 200);
drawGhost2(100, 350);
drawGhost3(200, 300);
}else if (storyStage == "Final Stage") {
//displays the final message
updateMessage("Yum Yum... ok Bye!");
//ghosts having a conversation together
drawPacman(100, 300);
} else {
updateMessage("The key you pressed (" + key + ") is not a valid input... :(");
}
//Prints Instructions at the bottom of the screen
//It uses push and pop to not interfere in the other texts displayed in the canvas
push();
textSize(15);
text("Pac-Pac. Type 0, 1, 2, 3, 4 or 5...", 150, height - 10);
pop();
//Provides some debugging information on the console
print("last input: " + key);
}
/*CUSTOM FUNCTIONS */
function updateMessage(msg) {
//function used to update the messages to the user
//creates a dialog box
rect(0, 0, width, 40, 10,10);
//Display the message passed as a parameter
text(msg, 15, 30);
}
function drawPacman(X1, Y1)
{
//Function used to draw the Pacman the position passed by the parameters X1 and Y1
//Pacman
//body
push();
fill(255, 230, 20);
ellipse(X1, Y1, 100, 100);
//Pacman mouth
fill(232, 238, 242);
/* if the upper mouth radian reaches 2*PI the
direction of the mouth reverses*/
if (i >= 2 || i <= 1.8) {
speed = speed * (-1)
}
noStroke();
arc(X1, Y1, 100, 100, (i += speed) * PI, (x -= speed) * PI, PIE);
//Pacman eye
fill(0);
ellipse(X1 + 7, Y1 - 30, 10, 10);
pop();
noStroke();
}
function drawGhost1(X2, Y2)
{
//Function used to draw the Ghost1 the position passed by the parameters X2 and Y2
//Ghost1-Bottom left
//body
push();
fill(0, random(255), random(255));
ellipse(X2, Y2, 55, 55);
rect(X2 - 25, Y2 + 6, 50, 30, 10);
//Eyes
fill(255);
noStroke();
ellipse(X2 - 8, Y2 - 5, 15, 15);
fill(0);
ellipse(X2 - 6, Y2 - 5, 10, 10);
fill(255);
noStroke();
ellipse(X2 + 7, Y2 - 5, 15, 15);
fill(0);
ellipse(X2 + 4, Y2 - 5, 10, 10);
pop();
noStroke();
}
function drawGhost2(X3, Y3)
{
//Function used to draw the Ghost2 the position passed by the parameters X3 and Y3
//Ghost 2 Upper Right
//body
push();
fill(255, random(255), 0);
rect(X3 - 25, Y3 + 6, 50, 30, 10);
ellipse(X3, Y3, 55, 55);
//Eyes
fill(255);
noStroke();
ellipse(X3 - 8, Y3 - 5, 15, 15);
fill(0);
ellipse(X3 - 6, Y3 - 5, 10, 10);
fill(255);
noStroke();
ellipse(X3 + 7, Y3 - 5, 15, 15);
fill(0);
ellipse(X3 + 4, Y3 - 5, 10, 10);
noStroke();
pop();
noStroke();
}
function drawGhost3(X4,Y4)
{
//Function used to draw the Ghost 3 the position passed by the parameters X4 and Y4
//Ghost 3 Bottom Right
//body
push();
fill(random(255), random(0), random(255));
ellipse(X4, Y4, 55, 55);
rect(X4 - 25, Y4 + 6, 50, 30, 10);
//Eyes
fill(255);
noStroke();
ellipse(X4 - 8, Y4 - 5, 15, 15);
fill(0);
ellipse(X4 - 6, Y4 - 5, 10, 10);
fill(255);
noStroke();
ellipse(X4 + 7, Y4 - 5, 15, 15);
fill(0);
ellipse(X4 + 4, Y4 - 5, 10, 10);
pop();
noStroke();
}
/*SYSTEM FUNCTIONS */
function keyPressed() {
//This system function is being used to change the global variable "storyStage". this was the reference from the class example
//the next if statements test the system variable "key"
if (key == "0") {
storyStage = "initial Stage";
} else if (key == "1") {
storyStage = "Stage 1";
} else if (key == "2") {
storyStage = "Stage 2";
} else if (key == "3") {
storyStage = "Stage 3";
} else if (key == "4") {
storyStage = "Stage 4";
} else if (key == "5"){
storyStage = "Final Stage";
} else {
storyStage = "Unknown";
}
}