Python Processing; Pacman

Hello!
I am starting the first step of my Pacman game, but I am having a problem with the Pacman moving. It seems that I’ve done something to my code to keep adding new Pacmans when I press an arrow. I want to remove the old Pacman whenever an arrow clicks.
Here is my code:

def setup():
    global pacmanright, pacmanleft, pacmandown, pacmanup
    global boxone
    global incrx, incry
    global x, y
    size (750, 750)
    pacmanright = loadImage ( "pacmanright.png" )
    pacmanleft = loadImage( "pacmanleft.png" )
    pacmandown = loadImage( "pacmandown.png" )
    pacmanup = loadImage ( "pacmanup.png" )
    x = 0
    y = 0
    image(pacmanright, x, y, 35, 35)  

def draw():
    boxone = rect (136, 362, 477, 159)

def keyPressed():
    global x, y
    if key == CODED:
        
        if keyCode == RIGHT:
            x = x + 35
            image(pacmanright, x, y, 35, 35)
                             
        elif keyCode == LEFT:
                x = x - 35
                image(pacmanleft, x, y, 35, 35)
                
        elif keyCode == DOWN:
            y = y + 35
            image(pacmandown, x, y, 35, 35)
        
        elif keyCode == UP:
            y = y - 35
            image(pacmanup, x, y, 35, 35)
1 Like

sorry, but i asked you already to format your code at your first post,
you did not do, so the admin had to do it for you.


please format your code posting here at forum
( topic editor )
by pasting it into the

</> code button

of the editor header menu ( context name: Preformatted text )
it looks like
```
type or paste code here
```

also can use the ``` manually above and below your code.
or select the already pasted “code” and use [ctrl][shift][c]

thank you.


also we recommend that the code from processing IDE ( PDE )
is formatted there first, like with /Edit/Auto Format/ or [ctrl][t]


now we need you to REPAIR your above CODE POSTING,
not make a new / better post.
( just think about the hundred people opening your topic in the future )


1 Like

You’re seeing the Pacman images from earlier frames. Processing does not clear the drawing space between frames. Add a background('#FFFFFF') function to your draw(). You can change the colour argument from white to whatever you prefer.

You’ll also need to restructure your code, so that the image() function is the last line of the draw() block. For example:

def setup():
    ...
    global pacman
    pacman = pacmanright

def draw():
    background('#FFFFFF')
    image(pacman, x, y, 35, 35)

def keyPressed():
    global x, y, pacman

    if key == CODED:
    
        if keyCode == RIGHT:
            x = x + 35
            pacman = pacmanright
                             
        elif keyCode == LEFT:
            x = x - 35
            pacman = pacmanleft
        ...

* Just an observation: I recall that Pacman’s movement is more like Snake, in that Pacman is always in motion. Of course, you don’t have to replicate the original arcade game.