Python Forum
[PyGame] Return won't stop a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Return won't stop a function
#1
I'm currently making a menu for my game in python. And I encountered a problem with code, that switches between different menu pages. When it needed to switch, it just switched for a little less than a second and then returned to the main page. I searched the web and found out, that I need to use return and so I did. However it did nothing. Then I figured I should add break, since loop is maybe still running. And I did that as well, however still nothing changed. I tried to search the web further, but no one seemed to have problem with return not working, no matter how widely I searched. So I figured I should ask on this forum.
Here's the code in question:
def menu():
    screen.blit(ozadje, (0, 0))
    screen.blit(naslov_menija, (440, 210))
    screen.blit(avtor, (450, 270))
    choice10 = (440, 305, 210, 30)
    choice11 = (440, 300, 280, 30)
    choice20 = (440, 335, 140, 30)
    choice21 = (440, 335, 173, 30)
    choice30 = (440, 365, 70, 30)
    choice31 = (440, 365, 90, 30)
    mouse = pygame.mouse.get_pos()
    if lang == 0:
        screen.blit(singleplayer, (440, 300))
        if mouse[0] > choice10[0] and mouse[0] < choice10[0] + choice10[2]:
            if mouse[1] > choice10[1] and mouse[1] < choice10[1] + choice10[3]:
                pygame.draw.rect(screen, red, choice10, 3)
    elif lang == 1:
        screen.blit(enoigralski_nacin, (440, 300))
        if mouse[0] > choice11[0] and mouse[0] < choice11[0] + choice11[2]:
            if mouse[1] > choice11[1] and mouse[1] < choice11[1] + choice11[3]:
                pygame.draw.rect(screen, red, choice11, 3) 
    if lang == 0:
        screen.blit(settings, (440, 330))
        if mouse[0] > choice20[0] and mouse[0] < choice20[0] + choice20[2]:
            if mouse[1] > choice20[1] and mouse[1] < choice20[1] + choice20[3]:
                pygame.draw.rect(screen, red, choice20, 3)
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                       settings()
                        break
                        return
    elif lang == 1:
        screen.blit(nastavitve, (440, 330))
        if mouse[0] > choice21[0] and mouse[0] < choice21[0] + choice21[2]:
            if mouse[1] > choice21[1] and mouse[1] < choice21[1] + choice21[3]:
                pygame.draw.rect(screen, red, choice21, 3)
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        settings()
                        break
                        return
    if lang == 0:
        screen.blit(izhod1, (440, 360))
        if mouse[0] > choice30[0] and mouse[0] < choice[0] + choice30[2]:
            if mouse[1] > choice30[1] and mouse[1] < choice30[1] + choice30[3]:
                pygame.draw.rect(screen, red, choice30, 3)
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        pygame.quit()
    elif lang == 1:
        screen.blit(izhod2, (440, 360))
        if mouse[0] > choice31[0] and mouse[0] < choice31[0] + choice31[2]:
            if mouse[1] > choice31[1] and mouse[1] < choice31[1] + choice31[3]:
                pygame.draw.rect(screen, red, choice31, 3)
                for event in pygame.event.get(): 
                    if event.type == pygame.MOUSEBUTTONDOWN :
                        run = False
    pygame.display.update()
def settings():
    title0 = font1.render('SETTINGS', 1, black)
    title1 = font1.render('NASTAVITVE', 1, black)
    screen.blit(background , (0, 0))
    if lang == 0:
        screen.blit(title0, (440, 250))
    elif lang == 1:
        screen.blit(title1, (440, 250))
    pygame.display.update()
Can anyone help me with my problem?
Reply
#2
break will break out of the current loop it is under, or if under multiple loops, the one that it is directly under. return will exit the function with data if provided. In your code here, you've added a break before a return so the return is never run. If you remove the break, then it will run settings function and then exit menu function. I don't know what happens after that since this isn't the full code. Also, on line 23, you passed settings into screen.blit, but it's a function. One thing I'd like to note though, is that your code is really messy, so I'd like to give you a few tips:
  • Use pygame.Rect instead of tuples, this way you can do something such as choice10.collidepoint(mouse) which will return a bool value of whether or not the point is inside the rectangle.
  • You should not be using pygame.event.get() more than one time in one loop.
  • Those if statements can easily be combined into two if statements or you can even use no if statements at all.

Just a quick example of how it could be simplified:
def menu():
    screen.blit(ozadje, (0, 0))
    screen.blit(naslov_menija, (440, 210))
    screen.blit(avtor, (450, 270))
    
    choice10 = pygame.Rect(440, 305, 210, 30)
    choice11 = pygame.Rect(440, 300, 280, 30)
    choice20 = pygame.Rect(440, 335, 140, 30)
    choice21 = pygame.Rect(440, 335, 173, 30)
    choice30 = pygame.Rect(440, 365, 70, 30)
    choice31 = pygame.Rect(440, 365, 90, 30)
    
    mouse = pygame.mouse.get_pos()
    
    langs = [
        ((singleplayer, settings, izhod1), (choice10, choice20, choice30)),
        ((enoigralski_nacin, nastavitve, izhod2), (choice11, choice21, choice31))
    ]

    for x in langs[lang][0]:
        screen.blit(x, (440, 300))
    for x in langs[lang][1]:
        if x.collidepoint(mouse):
            pygame.draw.rect(screen, red, x, 3)

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            settings()
            return
Also I'd recommend reading through metulburr's tutorial on creating a state machine which is really good practice for things like pygame.
Reply
#3
Thank you for the tips I will try to implement them and use them (When I understand them, because I'm a beginner Big Grin ). However return didn't work even when I didn't put break in my code. Return somehow just doesn't stop the menu function for some reason.
Reply
#4
(Apr-25-2021, 01:50 PM)jonagold Wrote: Thank you for the tips I will try to implement them and use them (When I understand them, because I'm a beginner Big Grin ). However return didn't work even when I didn't put break in my code. Return somehow just doesn't stop the menu function for some reason.
No problem, I actually had an error in there which I just fixed, but I can explain how it works. So there's a list langs which contains the info for each lang. The code indexes the list with the lang variable and uses the values listed in the tuples to know what to blit and draw. Also, one of the best ways to debug code is to put print statements. So try placing some print statements around your code to see what's happening.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020