Making Pacman move around

mapcoordsmapcoordsmapcoords

Now it’s finally time to get Pacman to move around the screen in response to our commands. We can do this by looking out for keypress events in Processing.

if (keyPressed && (key == CODED))    //if it's a coded key
    {
      if (keyCode == UP){ //and if the up arrow key was pressed
        if( centreY< 0 ){ //if pacman moves up off the screen
          centreY = height ;} //move pacman down again
          centreY -= (3*speed); //otherwise move pacman upwards
        }
      if (keyCode == DOWN){    //if the down arrow key was pressed
          centreY = (centreY+(3*speed)) %height ; //move pacman down, but not off the screen
      } 
      if (keyCode == LEFT){ //if left arrow was pressed
         direction="r2l";
         if( centreX  < 0 ){ //if pacman moves left off the screen
          centreX =width ;  //move pacman all the way right againn
         
        }      
         centreX = centreX-(3*speed);//otherwise move pacman left
       }  
      if (keyCode == RIGHT){ //if the right arrow was pressed 
       direction="l2r"; 
       centreX = (centreX+(3*speed)) %width; //move pacman down, but not off the screen
    } 
    }

Here is the code that we are using. For this sketch to display Pacman’s name you will need to have a folder labelled ‘data’ where you have saved the font file. Download the font file here and save it in the ‘data’ folder.

pacman_link

You can see a demo of the Pacman code  here, published with Processing.js.

You can also change or edit the code here.

PFont font;
int speed =3;
float centreX=0;
float centreY=0; 
int pacSize = 85; 
String label = "Pacman";
String direction2 = "down";
String direction = "r2l";
color white =color(255);
color yellow =color(255,255,0);
color blue =color(0,0,255);
color red =color( 255,0,0,200);
color green= color(155,252,140);
color purple= color(229,41,255,100);
color skyblue=color(41,250,255);

void setup() {
  frameRate(12);
  size(200,200);//draw window
  centreX = height/2;
  centreY = width/2;
  font = loadFont("AndaleMono-24.vlw");
  textFont(font);
}

void draw() {
  smooth();
  stroke(0);
  background(0);
  if (keyPressed && (key == CODED))    //if it's a coded key
    {
      if (keyCode == UP){ //and if the up arrow key was pressed
        if( centreY< 0 ){ //if pacman moves up off the screen
          centreY = height ;} //move pacman down again
          centreY -= (3*speed); //otherwise move pacman upwards
        }
      if (keyCode == DOWN){    //if the down arrow key was pressed
          centreY = (centreY+(3*speed)) %height ; //move pacman down, but not off the screen
      } 
      if (keyCode == LEFT){ //if left arrow was pressed
         direction="r2l";
         if( centreX  < 0 ){ //if pacman moves left off the screen
          centreX =width ;  //move pacman all the way right againn

        }      
         centreX = centreX-(3*speed);//otherwise move pacman left
       }  
      if (keyCode == RIGHT){ //if the right arrow was pressed 
       direction="l2r"; 
       centreX = (centreX+(3*speed)) %width; //move pacman down, but not off the screen
    } 
    }
  drawPacman(centreX,centreY,pacSize, yellow, label); //use a function to draw pacman

  if (mousePressed) {
    if (mouseButton == LEFT) {
       save("mapcoords.png");
    }
  }

}

void drawPacman(float tempX, float tempY, int tempSize, color tempColor, String tempText){
    //draw pacman
    fill(tempColor);
    noStroke();
    if (direction=="l2r"){
    arc(tempX, tempY, tempSize, tempSize, radians(45), radians(315));
    }
    if (direction=="r2l")
    {
    arc(tempX, tempY, tempSize, tempSize, radians(225), radians(360+135));
    }
    fill(0);
    ellipse(tempX,tempY-tempSize/3,tempSize/6,tempSize/6);
    //draw label
    fill(white);
    textAlign(CENTER);
    textSize(20);
    text(tempText,tempX,tempY-tempSize/2);
}
Advertisements

3 thoughts on “Making Pacman move around

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