Pac-Man - keep pacman moving in a direction once one key is pressed and released

make your own pac-man game
how to make a pacman game
create pacman game
pac man sprites for game maker
game maker 8.1 tutorials pdf

So, I want to code pac-man in python3 with the pygame module. However I do not know how to keep pac-man moving once a single key is pressed. The aim is to press for example the "up" key and pac-man will continue to move in that direction until he hits a boundary, even if I release the key. As of right now I have not coded these boundaries as I am prioritising the continuous movement of pac-man. I attempted to nest a while loop which should've worked but that obviously gave me a phat run-time error. Any ideas on how I can do this?

if keys[pygame.K_UP] and (pac.y - pac.radius) > 0:
        pac.y -= pac.speed
        pac.direction = "UP"
    elif keys[pygame.K_DOWN] and (pac.y + 2*(pac.radius) + pac.speed) < height:
        pac.y += pac.speed
        pac.direction = "DOWN"
    elif keys[pygame.K_LEFT] and (pac.x - pac.radius) > 0:
        pac.x -= pac.speed
        pac.direction = "LEFT"
        left = True
    elif keys[pygame.K_RIGHT] and (pac.x + 2*pac.radius + pac.speed) < width:
        pac.x += pac.speed
        pac.direction = "RIGHT"
        right = True

The expected result is to input a single direction and pac man will move in that direction by himself without needing to hold the key down. In reality what happens is I end up having to hold the key down so that pac man will move in the wanted direction.

Moving your pacman directly when the key is pressed is not going to work for your program. You should set pacman's speed or something here and apply that each frame in another function.

def update():
    pac.y += pac.yspeed
    pac.x += pac.xspeed


while True:
    clock.tick(60)
    if keys[pygame.K_UP] and (pac.y - pac.radius) > 0:
        pac.yspeed = 1
    elif keys[pygame.K_DOWN] and (pac.y + 2*(pac.radius) + pac.speed) < height:
        pac.yspeed = -1
    elif keys[pygame.K_LEFT] and (pac.x - pac.radius) > 0:
        pac.xspeed = -1
        left = True
    elif keys[pygame.K_RIGHT] and (pac.x + 2*pac.radius + pac.speed) < width:
        pac.xspeed = 1
        right = True
    if keys[pygame.K_ESC]:
        break
    update()
    # Do all your other stuff

GMS 2, What is the best way to handle pacman movement? The originial works like: one key stroke sends you in a direction for an In pacman if you have a long straight away to the right and you press up, he'll keep going right until he's able to go up. Yes, the key is though that once that up movement becomes� Make Pacman rotate and move in a direction when you hit the arrow keys, only if there is a path for Pacman to go on. So, if I hit the "left" key, Pacman checks to the left and, if there is a path there, he changes to look like he's going left and moves in that direction. Behaviors are what make our characters act a certain way.

I'd recommend creating an object within your Pac-Man class that keeps track of the direction Pac-Man should be moving (basically your pac.direction you're already setting). For each movement iteration, instead of checking your keys map, instead check pac.direction and perform the operation it says. Basically have it look similar to this if you want to keep your code mostly the way it is:

if pac.direction == "UP" and (pac.y - pac.radius) > 0:
        pac.y -= pac.speed
elif pac.direction == "DOWN" and (pac.y + 2*(pac.radius) + pac.speed) < height:
        pac.y += pac.speed
elif pac.direction == "LEFT" and (pac.x - pac.radius) > 0:
        pac.x -= pac.speed
        left = True
elif pac.direction == "RIGHT" and (pac.x + 2*pac.radius + pac.speed) < width:
        pac.x += pac.speed
        right = True

Then you just need to set pac.direction to the correct value when a key is pressed before you get to the code. I must admit, I'm unfamiliar with PyGame, so I'll leave that up to the reader.

Alternatively, you could compact this down quite a bit by making the direction of the form pac.direction = (xSpeed, ySpeed). Then your code compacts to (without bounds checking which can be done before the lines):

pac.x += pac.direction[0]  # Could be pac.direction.x if you use an object instead of a tuple
pac.y += pac.direction[1]  # Same as above with pac.direction.y
left = pac.direction[0] > 0  # I'm unsure why you're doing left and right separately, so I'll just put left

Character Movement, Designing and Scoring a Pac-Man Style Maze Game with Flash Type the following code right after the break; statement: direction based on the arrow key being pressed, and beginning the walking animation so that the character moves when the user presses one of the arrow keys. Save your work. Pac-Man is a maze arcade game developed and released by Namco in 1980. The original Japanese title of Puck Man was changed to Pac-Man for international releases as a preventative measure against defacement of the arcade machines by changing the P to an F.

You could use an OR statement to check if Pac Man has already been given a direction:

if (keys[pygame.K_UP] or pac.direction == "UP") and (pac.y - pac.radius) > 0:

Repeat for each if statement per direction.

This is assuming that no other code is changing the direction.

Pacman game, In Pacman, ghosts are mutable and expose Move and Reset methods that change their properties. IsReturning <- false v else // Other ghosts pick one direction twoards Pacman keyCode keysPressed /// Triggered when key is pressed/released let update (e It keeps a mutable state with current step of Pacman's mouth. The ghosts in Pac-Man always leave the ghost house to the left, however, due their programming, they can immediately change direction as soon as leaving depending on where you are. In some levels of the game there are “Safe Zones” on the maps where you can hide and, due to the ghost’s programming, will never be caught.

Why not use a variable to store the most recent direction

while True:
    if keys[pygame.K_UP] and (pac.y - pac.radius) > 0:
        recent_direction = 'UP'
    elif keys[pygame.K_DOWN] and (pac.y + 2*(pac.radius) + pac.speed) < height:
        recent_direction = 'DOWN'
    elif keys[pygame.K_LEFT] and (pac.x - pac.radius) > 0:
        recent_direction = 'LEFT'
    elif keys[pygame.K_RIGHT] and (pac.x + 2*pac.radius + pac.speed) < width:
        recent_direction = 'RIGHT'
while True:
    if recent_direction == 'UP':
        pac.y -= pac.speed
    elif recent_direction == 'DOWN':
        pac.y += pac.speed
    if recent_direction == 'LEFT':
        pac.x -= pac.speed
    if recent_direction == 'RIGHT':
        pac.x += pac.speed

Hopefully this works for you

[PDF] Pacman, direction of movement with the keyboard (see Controlling Pacman square, one ghost outside of the ghost pen, and the rest of the ghosts You must also keep track of Pacman's score and remaining lives. You may notice that in the demo, if a directional key is pressed when Pacman cannot move in. 1. I used a sprite sheet for the maze, and I drew PAC-MAN pixel by pixel. It looks pretty accurate. 2. I'm not very good at clones. 3. The keys are used for unlocking the doors, the super pellets (green pellets) turn PAC-MAN into Super PAC-MAN, they make PAC-MAN bigger and invulnerable, thus squashing the ghosts.

Unity 2D Pac-Man Tutorial, The original game was released in October 1980 and soon became the most Our Pac-Man Tutorial will be developed with Unity 2018.4 (also known as 2018 LTS). We will name it PacMan, save it in a location such as C:\GameDev, select the Arrow-Keys, Pac-Man should move exactly 1 unit into the desired direction. How to Build a Simple Pac-Man Game For today's program, we are going to build a simple Pac-Man game. Pac-Man was one of the very first arcade games developed around 1980. For our version of Pac-Man we are going to focus on the following programming and problem solving techniques: a. Using Scratch to develop computer objects (also known as Object

Pacman java code, This is the original pacman game that you once stood around and played at the local Game Description: Play online the popular pac man online game very easily on Player 1 Controls: Arrow keys PacMan by Justin Wei A fork of {{sketch . will while going around corners in case that there is user input for this direction. Then we have to check if any of the keys are pressed, and lastly, assign the correct speed and direction, and change is_moving to true. For the left arrow key, the code would be: The last thing we haven’t gone through is the timer, move_timer. The logic will be that when move_timer is greater than 0, the character will move each frame.

Pacman code python, The editor I am using is atom and I highly Code Pac-Man in Python: so much to do. some keys pressed and allow him to go only in certain directions, where there Hint:--If Pacman moves too slowly for you, try the option frameTime 0. one Pac-Man is a maze arcade game developed and released by Namco in 1980. During the game, the player could move Pac-Man (represented by a yellow light) by tapping flipper buttons for direction/movement to avoid the ghost (represented by a red light). Five months later came Baby Pac-Man, which combined a Pac-Man video game with a pinball table, in a video-game-sized cabinet.

Comments
  • Could you please add some more of you code around the if statement.
  • You are currently moving pacman (e.g. pac.x += pac.speed) only when a key is pressed (e.g. elif keys[pygame.K_RIGHT]...). You should store the direction information when a key is pressed and put the moving code someplace which is periodically executed (e.g. use a timer).
  • Exactly. But since you've gone this far, the answer would be complete if it showed how to call update in each time step.
  • This worked really nicely, I had to make some minor adjustments to ensure that pac-man only moved in the x direction or the y direction at a time so I used some if statements. But this was absolutely perfect, thank you