Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Processing project - Pacman

Options
  • 22-11-2011 8:07pm
    #1
    Registered Users Posts: 2,589 ✭✭✭


    Ok so if i could get help with this i would greatly appreciate it.

    Basically, I have a figure of Pacman and need to move him around the screen using the Arrow keys, using Processing. When it changes direction, the mouth has to face the appropriate way (ie. if I press up, he moves up and the mouth points up).

    When the arrow key is depressed, the pacman is to stop.

    When it gets to the edge of the screen, I want it to re-appear at the opposite side (if he goes off the screen at the left, he is to come back out on the right hand side.)

    Also, I need to simulate him opening and closing his mouth like in the game.

    Another thing I noticed, is when the keys are not pressed, the pacman disappears off the screen and only shows when a key is pressed.

    Here is the code i have so far, and any help would be great:
    int radius = 10, direction = 1;
    int direction2 = 1;
    float x = 10, speed = 0.5, y = 10;
    void setup()
    {
    size(500,500);
    smooth();
    ellipseMode(RADIUS);

    }
    void draw()

    {
    background(0);

    if((x > width - radius) || (x < radius))
    direction = -direction;

    if((y > height - radius) || (y < radius))
    direction2 = -direction2;

    if(keyPressed)
    {
    if (key == CODED)
    {
    if(keyCode == RIGHT)
    x = x + speed * direction;
    if(keyCode == LEFT)
    x = x - speed * direction;
    if(keyCode == DOWN)
    y = y + speed * direction2;
    if(keyCode == UP)
    y = y - speed*direction2;
    }

    if(keyCode == RIGHT)
    arc(x, y, radius, radius, 0.52, 5.76);
    if(keyCode == LEFT)
    arc(x, y, radius, radius, 3.67, 8.90);
    if(keyCode == UP)
    arc(x, y, radius, radius, 5.15, 10.45);
    if(keyCode == DOWN)
    arc(x, y, radius, radius, 2.25, 7.35);

    }
    }


Comments

  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    The arc calls should be always called, you seem to only call them on a key press. Key presses should only be setting a state, or you read the state every time and react accordingly, but you must always call it regardless of a keypress or not.


  • Registered Users Posts: 2,589 ✭✭✭irish_stevo815


    Giblet wrote: »
    The arc calls should be always called, you seem to only call them on a key press. Key presses should only be setting a state, or you read the state every time and react accordingly, but you must always call it regardless of a keypress or not.

    So this part shouldnt be here: ??
    if(keyCode == RIGHT)
    arc(x, y, radius, radius, 0.52, 5.76);
    if(keyCode == LEFT)
    arc(x, y, radius, radius, 3.67, 8.90);
    if(keyCode == UP)
    arc(x, y, radius, radius, 5.15, 10.45);
    if(keyCode == DOWN)
    arc(x, y, radius, radius, 2.25, 7.35);

    Should I be calling the actual arc shapes in the void draw section then?


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    Well there's tons of stuff you could do to seperate out the logic from the drawing, but in this instance, just to get you moving, try creating two variables. startArc, and stopArc.

    if(keyCode == UP)
    {
    startArc =5.15;
    stopArc = 10.45;
    }

    continuing for all keycodes.
    Then just before the end of the draw() method, call
    arc(x,y,radius,radius, startArc, stopArc);


  • Registered Users Posts: 2,589 ✭✭✭irish_stevo815


    Giblet wrote: »
    Well there's tons of stuff you could do to seperate out the logic from the drawing, but in this instance, just to get you moving, try creating two variables. startArc, and stopArc.

    if(keyCode == UP)
    {
    startArc =5.15;
    stopArc = 10.45;
    }

    continuing for all keycodes.
    Then just before the end of the draw() method, call
    arc(x,y,radius,radius, startArc, stopArc);

    Thank you so much, this helped a lot.

    Now I just have to figure out how to make his mouth open & close :)


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    Try something simple like a counter,
    int count = 0;
    bool open = false;
    void draw()
    {
        if(count % 60 ==0)
        {
            open = !open;
        }
        if(open){
        //Draw arc positions for startArc and stopArc
        }
        else 
        {
        //draw full arc 
        }
        count++;
    }
    


  • Advertisement
  • Registered Users Posts: 2,589 ✭✭✭irish_stevo815


    Giblet wrote: »
    Try something simple like a counter,
    int count = 0;
    bool open = false;
    void draw()
    {
        if(count % 60 ==0)
        {
            open = !open;
        }
        if(open){
        //Draw arc positions for startArc and stopArc
        }
        else 
        {
        //draw full arc 
        }
        count++;
    }
    

    Got this working perfectly, cheers.

    Just a quick one, what do the terms:
    if(count % 60 ==0) and count++;
    actually mean, in simple english terms ;)


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    If count divided by 60 has no remainder, increment count by 1

    edit: % means modulus


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    It's quick and dirty logic to get an update every 60 frames, so 1 a second @ 60fps
    If I'm on a count that is evenly divisible by 60, toggle the open state. The counter is just updated every frame by one (count++ is the same as count = count + 1)


Advertisement