Python Processing 3

My code isn’t working and I don’t know why.
My start button and my control button works fine, but the arrow button, to go back to the menu, at my control button wouldn’t work.
Please help!
Here is my code:

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

def draw():
    image(menu, 0, 0, 750, 750)

#startbutton
def mouseReleased(s):
    if s.x > 136 and s.x < 136 + 477 and s.y > 263 and s.y < 263 + 159:
        image(whatever, 0, 0, 750, 750)
        noLoop()

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

        #arrowbutton
        def mouseClicked(a):
            if a.x > 0 and a.x < 0 + 61 and a.y > 672 and a.y < 672 + 77:
                image(menu, 0, 0, 750, 750)
                noLoop()

You need to out-dent your def mouseClicked(a) code, so that it sits against the left margin. Currently, it’s nested within the mousePressed(c) block.

I’ve spotted another issue, though: your #startbutton and #controlbutton code are always active. In other words, when your “whatever” and “controls” images are showing, the startbutton and controlbutton ‘zones’ remain clickable.

how would you make the buttons unactive?

You need a structure for managing different levels/states in your game. Something to manage the switch between the different menus, between your menu and level 1 (with it’s own control scheme), the end-game screen, etc.

There are many approaches you can take … the ideal solution depends on how complex your game is and how well you know Python/Processing. Here’s the simplest example I can whip up:

state = 'menu'

def setup():
    size(750, 750)

def draw():
    background(0)

    if state == 'menu':
        text('CLICK TO GO TO WHATEVER', 100, 100)
        
    elif state == 'whatever':
        text('CLICK TO GO TO MENU', 100, 100)
        
    elif state == 'level1':
        ...


def mouseClicked(e):
    global state
    
    if state == 'menu':
        # mouse events for menu
        if e.x > 80 and e.x < 290 and e.y > 70 and e.y < 120:
            state = 'whatever'
            print(114235)

    elif state == 'whatever':
        # mouse events for controls
        if e.x > 80 and e.x < 290 and e.y > 70 and e.y < 120:
            state = 'menu'
            
    elif state == 'level1':
        ...
        

Hope this helps.

If you’re doing advanced menu stuff, you may want to look at a GUI library.

2 Likes

Beginner here. I was struggling over the same problem for a very long time and your answer just saved my life. Thank you so much!!!

1 Like