Beginner help; how do you stop an image to go through a area?

edited October 2014 in Using Processing

I'm have this code, but i don't know how to stop Pacman from going through the edges of parts of the level but allow it to go through other parts.

PImage bg;
float  x = 335;
float y =148;
float a = 0.5;
float b = 5;
float z = x + 1;
float w = y - 10;
void setup () {
  size (766, 520);
  bg = loadImage ("pacman.png");
}
  void draw() {
    background (bg) ;
     fill (#F7E516);
  arc (x, y, 35, 35, a, b+QUARTER_PI, PIE);   
 fill (0);
  ellipse ( z, w, 3, 3);
 if (y < 38){
  y = 38;
}
if (y > 485){
  y = 485;
}

if (x > 766){
x = 1;
}
if (x < 1){
x = 766;
 }
  }
  void keyPressed () {
  if (keyCode == CODED) {
    if (keyCode == UP) {
   x = x;
  y = y - 5; 
  b = 9.7;
  a = 5.2;
  z = x + 10;
   w = y - 9;
} 
}else if (keyCode == LEFT) {
  x = x-5;
  y =y;
  b = 8.1;
  a = 3.6;
  z = x + 2;
 w = y - 10;

}else if (keyCode == DOWN) {
  x= x;
  y = y + 5;
  b = 6.5;
  a = 2;
  z = x + 9.5;
  w = y + 3;
  }else if (keyCode == UP) {
    x = x;
  y = y - 5; 
  b = 9.7;
  a = 5.2;
  z = x + 10;
  w = y - 3;
  }else if (keyCode == RIGHT) {
  x= x + 5;
  y = y; 
  b = 5;
  a = .5;
  z = x + 1 ;
 w = y - 10;
 }
}

pacman

Answers

  • Check the position of the moving figure, like you do against the bounds of the sketch, but against the bounds of the drawn rectangle.

Sign In or Register to comment.