Pac-Man

Working a making a pac- man move back and forth on the screen. this is all ive got. i want it to keep bouncing back and forth.

float x =0; float xmovel =150/2; int dirl =1; float speed = 1;

void setup(){ size(500,500); frameRate(60); translate(250,250); }

void draw(){ print(mouseX," : "); println(mouseY);

background(255);

//facing right if (dirl == 1){ noStroke(); fill(254,255,0); arc(0+xmovel,250,150,150,.5-x,5.8+x); x = x + .050; if (x > .6){ x = 0; } fill(0); ellipse(41+xmovel,212,15,15); }

if (dirl == -1){ // facing left noStroke(); fill(254,255,0); arc(0+xmovel,250,150,150,radians(-152)-x,radians(152)+x); x = x + .050; if (x > .6){ x = 0; } fill(0); ellipse(-40+xmovel,212,15,15); }

if (xmovel > 420){ dirl = -dirl; xmovel = 400;

} xmovel = dirl*speed+xmovel; println(xmovel);

}

void keyPressed(){ if (key == 'u'){ speed++; } if (key == 'd'){ speed--; } }

Answers

  • Answer ✓
    float x = 0; 
    float xmovel = 150/2; 
    int dirl = 1; 
    float speed = 1;
    
    void setup() { 
      size(500, 500); 
      frameRate(60); 
      translate(250, 250);
    }
    
    void draw() { 
      background(255);
      //facing right 
      if (dirl == 1) { 
        noStroke(); 
        fill(254, 255, 0); 
        arc(0+xmovel, 250, 150, 150, .5-x, 5.8+x); 
        x = x + .050; 
        if (x > .6) { 
          x = 0;
        } 
        fill(0); 
        ellipse(41+xmovel, 212, 15, 15);
      }
      if (dirl == -1) { // facing left 
        noStroke(); 
        fill(254, 255, 0); 
        arc(0+xmovel, 250, 150, 150, radians(-152)-x, radians(152)+x); 
        x = x + .050; 
        if (x > .6) { 
          x = 0;
        } 
        fill(0);
        ellipse(-40+xmovel, 212, 15, 15);
      }
    
      if (xmovel > 420) { 
        dirl = -dirl; 
        xmovel = 400;
      }
    
      if (xmovel < 80) { 
        dirl = -dirl; 
        xmovel = 100;
      }
    
    
    
      xmovel = dirl * speed + xmovel; 
    }
    
    void keyPressed() { 
      if (key == 'u') { 
        speed++;
      } 
      if (key == 'd') { 
        speed--;
        if( speed < 0 ){
          speed = 0;
        }
      }
    }
    

    You didn't have a condition that would check if he bounced off the left wall, so I added one for you. You also probably don't want him to have a negative speed, so I added a check for that too.

  • Wow thx alot

Sign In or Register to comment.