Liam
Liam

Reputation: 31

my pygame code is freezing when i run my code

whenever i run the code the entire thing freezes and i'm not sure why, it should just display everything (which it does) but then when i click on my mouse it should print "yeet"

i don't know if it has to do with the while loop or not

i removed it but then it does not update the game

here is the whole code

import pygame
import time

pygame.init()
screen = pygame.display.set_mode((1200, 800))
done = False
pygame.font.get_fonts()
fourty = pygame.font.SysFont('Comic Sans MS', 40)
ten = pygame.font.SysFont('Comic Sans MS', 10)
twenty = pygame.font.SysFont('Comic Sans MS', 20)
thirty = pygame.font.SysFont('Comic Sans MS', 30)
sixty = pygame.font.SysFont('Comic Sans MS', 60)
fifty = pygame.font.SysFont('Comic Sans MS', 50)
clock = pygame.time.Clock()
WHITE = pygame.Color("#ffffff")
BLACK = pygame.Color("#000000")
RED = pygame.Color("#e6000d")
BLUE = pygame.Color("#0b5dff")
YELLOW = pygame.Color("#d8fb06")
GREEN = pygame.Color("#00e600")
screen.fill(WHITE)
play=bool()
play = False


def refresh():
        pygame.display.update()
        pygame.display.flip()
        clock.tick(60)

def main():
        global play
        hangman = fourty.render("WELCOME TO HANGMAN", True, (BLACK))
        screen.blit(hangman, [360, 200])                
        go = thirty.render("CLICK ANYWHERE TO START", True, (BLACK))
        screen.blit(go, [551,445])
        clock.tick(100)
        while True:   
                refresh()
                if play == True:
                        play()        


def play():
        print ("yeet")



main()




while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
                elif event.type == pygame.MOUSEBUTTONDOWN:
                        if play == False:
                                play == True
                                clock.tick(60)


        pygame.display.flip()

Upvotes: 3

Views: 41

Answers (1)

Rabbid76
Rabbid76

Reputation: 211116

There is an endless loop, without any event handling in main. You don't need that loop at all. Use the main application loop. Furthermore, play is the name of a function, so the name of the variable which states the game state should have a different name (e.g. playgame):

def refresh():
    pygame.display.flip()
    clock.tick(60)

def main():
    hangman = fourty.render("WELCOME TO HANGMAN", True, (BLACK))
    screen.blit(hangman, [360, 200])                
    go = thirty.render("CLICK ANYWHERE TO START", True, (BLACK))
    screen.blit(go, [551,445])

def play():
    print ("yeet")

playgame = False
done = False
while not done:

    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if playgame == False:
                playgame = True

    # clear display
    screen.fill(WHITE)

    # draw scene dependent on game state `playgame` 
    if playgame:
        play()
    else:
        main()

    # update dispaly
    refresh()

Upvotes: 1

Related Questions