Python Processing 3; Pacman Menu

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:

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)
1 Like

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

thank you.


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:

    ...
    #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:

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 :smiling_imp: :+1:
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.

3 Likes