# Python Processing 3; Pacman Menu

**URL:** <https://discourse.processing.org/t/python-processing-3-pacman-menu/16824>\
**Category:** Processing.py\
**Created:** [January 3, 2020, 2:38am UTC](https://discourse.processing.org/t/python-processing-3-pacman-menu/16824 "2020-01-03T02:38:26Z")\
**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 3, 2020, 2:38am UTC](https://discourse.processing.org/t/python-processing-3-pacman-menu/16824/1 "2020-01-03T02:38:27Z")

</div>

Hi!  
I am a beginner at python processing 3. I am currently making a Pacman game and I am having trouble making the menu.  
I would like to have the menu so that when the player clicks on the controls button, it will bring them to the controls page.  
However, the code that I have made allows me to click wherever and will bring me to the controls page.  
Also, the controls page will only last for around one-eighth of a second.  
Please help,  
Here is my code so far:

```auto
def setup():
    global menu, controls, whatever
    size (750, 750)
    menu = loadImage ( "startmainmenu.jpg" )
    controls = loadImage ( "keycontrols.jpg" )
    whatever = loadImage ( "gameover.jpg" )

def draw():
    global menu
    global startbutton, controlbutton
    image(menu, 0, 0, 750, 750)
    startbutton = rect (136, 263, 477, 159)
    controlbutton = rect (136, 484, 477, 159)
    noFill()
    noStroke()

def mousePressed(controlbutton):
    if mousePressed == True:
        image(controls, 0, 0, 750, 750)

```

---

<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 3, 2020, 3:40am UTC](https://discourse.processing.org/t/python-processing-3-pacman-menu/16824/2 "2020-01-03T03:40:49Z")

</div>

please format your code posting 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.

thank you.

* * *

---

<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 3, 2020, 4:27am UTC](https://discourse.processing.org/t/python-processing-3-pacman-menu/16824/3 "2020-01-03T04:27:10Z")

</div>

Welcome, heyitsme!

You’ve asked a two-part question, so I’ve provided a two-part response –

**Part 1**  
_“However, the code that I have made allows me to click wherever and will bring me to the controls page.”_

This is because the `mousePressed()` detects a click anywhere within the display window. You can use an `if` statement to detect if this click is within the area of the button:

```auto
    ...
    #controlbutton = rect (136, 484, 477, 159)
    rect(136, 484, 477, 159)
    ...

#def mousePressed(controlbutton):
def mousePressed(e):
    if e.x > 136 and e.x < 136+477 and e.y > 484 and e.y < 484+159:
        image(controls, 0, 0, 750, 750)

```

I’m not sure why you assigned the `rect()` to a variable? I’ve renamed your `controlbutton` to `e` because it’s shorter; this holds the event details and you can name it whatever you like. The `e.x` and `e.y` hold the click’s x- and y-coordinate, respectively.

**Part 2**

_“Also, the controls page will only last for around one-eighth of a second.”_

This is because the sketch is running at ~60 fps. The image, therefore, displays momentarily. To halt the sketch, you could add a `noLoop()` function:

```auto
def mousePressed(e):
    if e.x > 136 and e.x < 136+477 and e.y > 484 and e.y < 484+159:
        image(controls, 0, 0, 750, 750)
        noLoop()

```

You can use a `loop()` function to resume the normal `draw()` behaviour.

This should get you moving forward. Coding a Pacman clone is pretty challenging 😈 👍  
I suspect the next issue you’ll encounter is changing the button logic for each game state / menu; you could solve this with a few additional `if` statements.
