PyGame window crashes without running code

Viewed 125

Every time I try running my code in pygame, it loads the window and then freezes and not responding. I've been told that the code doesn't run past the display update loop. I can't figure out what is wrong and it has been very frustrating trying to code without being able to test it. This is for a simple PyGame platformer I'm trying to make for a project in school. This is just the main menu and does include some functions that aren't in this clip of code. I was using VS Code to write the program and it is running on an extension called "Pygame Snippets". The code is here:

import pygame
from pygame.locals import *

run = 1
loop = 1

pygame.init()
Screen = pygame.display.set_mode((1280,720))

White = (255,255,255)
DGray = (128,128,128)
LGray = (170,170,170)

DFont = pygame.font.SysFont("Arial",40)
BFont = pygame.font.SysFont("Arial", 60)

while loop == 1:
    pygame.time.delay(100)
    pygame.display.update()

while run == 1:
    def main_menu():

            Menu1 = 1
            hover_controls = 0
            hover_levels = 0
            hover_quit = 0

            T1 = DFont.render("Level Select", True , White)
            T2 = DFont.render("Controls", True , White)
            T3 = DFont.render("Quit", True , White)
            Title = BFont.render("Maths Platformer", True, White)


            while Menu1 == 1:

                mouse = pygame.mouse.get_pos()
            
                if 220 <= mouse[0] <= 420 and 310<= mouse[1] <= 410:
                    hover_levels == 1

                if 860 <= mouse[0] <= 1060 and 310<= mouse[1] <= 410:
                    hover_controls == 1

                if 540 <= mouse[0] <= 740 and 550 <= mouse[1] <= 650:
                    hover_quit == 1



                for ev in pygame.event.get():
                    if ev.type == pygame.QUIT:
                        pygame.QUIT()

                    else:

                        if ev.type == pygame.MOUSEBUTTONDOWN:

                            if hover_levels == 1:
                                levels_menu()

                            if hover_controls == 1:
                                controls_menu()

                            if hover_quit == 1:
                                pygame.quit

            screen.fill((117, 196, 255))

            if hover_levels == 1:
                pygame.draw.rect(screen,LGray,[220,310,200,100])

            else:
                pygame.draw.rect(screen,DGray,[220,310,200,100])


            if hover_controls == 1:
                pygame.draw.rect(screen,LGray,[860,310,200,100])

            else:
                pygame.draw.rect(screen,DGray,[860,310,200,100])


            if hover_quit == 1:
                pygame.draw.rect(screen,LGray,[540,550,200,100])

            else:
                pygame.draw.rect(screen,DGray,[540,550,200,100])


            screen.blit(T1, (270, 360))

            screen.blit(T2, (910, 360))

            screen.blit(T3, (590, 600))

            screen.blit(Title, (570, 180))


        
                
    main_menu() 
2 Answers

The loop while loop == 1: never terminates, because loop is never changed. Reomve this code completely.
== is the comparison operator. You have to use the assignment operator when specifying a variable (e.g.: hover_levels = 1). Python is case sensitive. Hence, you need to make sure that you are using screen everywhere (instead of Screen).
You must draw the menus in the application loop. Respect the Indentation.
pygame.QUIT is an event type. The name of the "quit" function is pygame.quit().

import pygame
from pygame.locals import *

run = 1
#loop = 1

pygame.init()
screen = pygame.display.set_mode((1280,720))

White = (255,255,255)
DGray = (128,128,128)
LGray = (170,170,170)

DFont = pygame.font.SysFont("Arial",40)
BFont = pygame.font.SysFont("Arial", 60)

#while loop == 1:
#    pygame.time.delay(100)
#    pygame.display.update()

def main_menu():
    global run

    Menu1 = 1
    
    T1 = DFont.render("Level Select", True , White)
    T2 = DFont.render("Controls", True , White)
    T3 = DFont.render("Quit", True , White)
    Title = BFont.render("Maths Platformer", True, White)

    while Menu1 == 1:

        mouse = pygame.mouse.get_pos()
        hover_controls = 0
        hover_levels = 0
        hover_quit = 0
        if 220 <= mouse[0] <= 420 and 310<= mouse[1] <= 410:
            hover_levels = 1
        if 860 <= mouse[0] <= 1060 and 310<= mouse[1] <= 410:
            hover_controls = 1
        if 540 <= mouse[0] <= 740 and 550 <= mouse[1] <= 650:
            hover_quit = 1

        for ev in pygame.event.get():
            if ev.type == pygame.QUIT:
                run = False
                Menu1 = False
            else:

                if ev.type == pygame.MOUSEBUTTONDOWN:
                    if hover_levels == 1:
                        levels_menu()
                    if hover_controls == 1:
                        controls_menu()
                    if hover_quit == 1:
                        run = False
                        Menu1 = False

    # Indentation
    #-->|

        screen.fill((117, 196, 255))

        if hover_levels == 1:
            pygame.draw.rect(screen,LGray,[220,310,200,100])
        else:
            pygame.draw.rect(screen,DGray,[220,310,200,100])

        if hover_controls == 1:
            pygame.draw.rect(screen,LGray,[860,310,200,100])
        else:
            pygame.draw.rect(screen,DGray,[860,310,200,100])

        if hover_quit == 1:
            pygame.draw.rect(screen,LGray,[540,550,200,100])
        else:
            pygame.draw.rect(screen,DGray,[540,550,200,100])

        screen.blit(T1, (270, 360))
        screen.blit(T2, (910, 360))
        screen.blit(T3, (590, 600))
        screen.blit(Title, (570, 180))
        pygame.display.update()

while run == 1:             
    main_menu() 

pygame.quit()
exit()

This loop:

while loop == 1:
    pygame.time.delay(100)
    pygame.display.update()

always runs and then runs constantly. Remove it and put display.update at the end of the menu loop.

Related