Home > OS >  pygame display boots up then immediately shuts down
pygame display boots up then immediately shuts down

Time:08-14

I'm trying to display something but it keeps booting up and immediately quitting.

the console comes up with:

 line 77, in main
    if event.type == MOUSEBUTTONDOWN:
NameError: name 'MOUSEBUTTONDOWN' is not defined
Traceback (most recent call last): line 89, in <module>
    main()

but I didn't think any of these problems would create this but I'm not sure.

def main(): #main game loop
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                clicking = True

        draw_window(startbutton, exitbutton,)


    pygame.quit()


if __name__ == "__main__":
    main()

CodePudding user response:

You should use pygame.MOUSEBUTTONDOWN instead of just a MOUSEBUTTONDOWN. Same as in pygame.QUIT.

CodePudding user response:

It is a matter of Indentation. The event object must be checked within the event loop. Also use pygame.MOUSEBUTTONDOWN instead of MOUSEBUTTONDOWN:

ef main(): #main game loop
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        
        # INDENTATION
        #-->|
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    clicking = True

        draw_window(startbutton, exitbutton,)


    pygame.quit()



if __name__ == "__main__":
    main()
  • Related