# Python Processing; Pacman

**URL:** https://discourse.processing.org/t/python-processing-pacman/17058
**Category:** Processing.py
**Created:** [January 13, 2020, 2:17am UTC](https://discourse.processing.org/t/python-processing-pacman/17058 "2020-01-13T02:17:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![heyitsme](https://avatars.discourse-cdn.com/v4/letter/h/7cd45c/32.png) [@heyitsme](https://discourse.processing.org/u/heyitsme)
#### Post date: [January 13, 2020, 2:17am UTC](https://discourse.processing.org/t/python-processing-pacman/17058/1 "2020-01-13T02:17:23Z")

</div>

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:

```auto
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)

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 13, 2020, 2:37am UTC](https://discourse.processing.org/t/python-processing-pacman/17058/2 "2020-01-13T02:37:18Z")

</div>

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

```auto
</> 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 )

* * *

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [January 13, 2020, 5:45am UTC](https://discourse.processing.org/t/python-processing-pacman/17058/3 "2020-01-13T05:45:52Z")

</div>

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:

```auto
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.
