Pac Man beginner

edited January 2017 in Questions about Code

I need to make this ellipse respond to my arrow keys and eat small orbs(pac man points) This is what ive done so far

float ang = 0;
float speed = 10;
int posx = 50;
int posy = 300;
int i = 1;

void setup(){
  background (128);
  size(800, 600);
  frameRate(20);
  ellipseMode(RADIUS);
  fill(#FAF312);
  noStroke();
}

void draw() {
  background (128);
  ang = atan2(mouseY - posy, mouseX - posx);
  posx += speed * cos(ang);
  posy += speed * sin(ang);
  translate(posx, posy);
  rotate(ang);
  //arc(50,50,75,75,.51,5.02);

  if (i == 1){
    arc(50, 50, 50, 50, .51, 5.9);
    i = -i;
  } else {
    ellipse(50, 50, 50, 50);
    i = -i;
  }
}
Tagged:

Answers

  • edited January 2017

    you need to add direction and direction2 to posx and posy (no ang / cos etc. !!!)

         posx=posx+direction ;
         posy=posy+direction2 ;
         translate(posx,posy);
         if(i==1){arc(50,50,50,50,.51,5.9); i=-i;} else{ellipse(50,50,50,50); i=-i;} }
    

    then use this function:

        void keyPressed() {
          if (key == CODED) {
            if (keyCode == LEFT) {
              x = x - 10;
              direction = -1;
              direction2 = 0;
            }
            else if (keyCode == RIGHT) {  
              x = x + 10;
              direction = 1;
              direction2 = 0;
            }
            else if (keyCode == UP) {
              y = y - 10;
              direction = 0;
              direction2 = -1;
            }
            else if (keyCode == DOWN) { 
              y = y + 10;
              direction = 0;
              direction2 = 1;
            }
          }
        }
    
  • It keeps telling me unexpected token for posx Can you like the complete code you used to make it work?

  • No. I am not at home

    You need to declare everything and debug it

    Also click on the tag #pacman below your first post

  • float ang = 0;
    float speed = 10;
    int posx = 50;
    int posy = 300;
    int i = 1;
    
    int direction, direction2;
    
    void setup() {
      background (128);
      size(800, 600);
      //  frameRate(20);
      ellipseMode(RADIUS);
      fill(#FAF312);
      noStroke();
    }
    
    void draw() {
      background (128);
    
      posx=posx+direction ;
      posy=posy+direction2 ;
      translate(posx, posy);
    
      if (frameCount%20==0)
        i=-i;
    
    
      if (i==1) {
        arc(50, 50, 50, 50, .51, 5.9);
      } else {
        arc(50, 50, 50, 50, .2, 6.2);
        //ellipse(50, 50, 50, 50);
      }
    }
    
    void keyPressed() {
      if (key == CODED) {
        if (keyCode == LEFT) {
    
          direction = -1;
          direction2 = 0;
        } else if (keyCode == RIGHT) {  
    
          direction = 1;
          direction2 = 0;
        } else if (keyCode == UP) {
    
          direction = 0;
          direction2 = -1;
        } else if (keyCode == DOWN) { 
    
          direction = 0;
          direction2 = 1;
        }
      }
    }
    
Sign In or Register to comment.