Zeperox
Zeperox

Reputation: 206

Game freezing when switching between functions pygame

I was trying to make a character gender choice in my game once you press new game, but whenever I try to call the function, the game freezes without an error. I tried to see what's happening using the breakpoint, but everything was ok, the game didn't freeze.

Here is the code I think is causing the issue:

class Button:
    def __init__(self, color, x, y, width, height, text=""):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self, window, outline = None):
        if outline:
            pygame.draw.rect(window, outline, (self.x-2, self.y-2, self.width+4, self.height+4), 0)
        pygame.draw.rect(window, self.color, (self.x, self.y, self.width, self.height), 0)

        if self.text != "":
            button_font = pygame.font.SysFont("comicsans", int(self.height/2))
            button_label = button_font.render(self.text, 1, (255,255,255))
            win.blit(button_label, (self.x + (self.width/2 - button_label.get_width()/2), self.y + (self.height/2 - button_label.get_height()/2)))

    def isOver(self, pos):
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False

def title():
    run = True
    button_newgame = Button((0,0,0), (Width/2)-(350/2), 300, 350, 75, "New Game")
    def redraw():
        win.fill((0,0,0))
        button_newgame.draw(win, (255,255,255))
        pygame.display.update()
    while run:
        clock.tick(FPS)
        redraw()

        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
            if event.type == QUIT:
                quit()

            if event.type == MOUSEBUTTONDOWN:
                if button_newgame.isOver(pos):
                    characterGender()

def characterGender():
    run = True
    Text_font = pygame.font.SysFont("comicsans", 100)
    button_girl = Button(None, 400+(342/2), 200-(360/2), 342, 360) # 342x360
    button_boy = Button(None, 200+(342/2), 200-(360/2), 342, 360) # 342x360
    win.fill((0,0,0))
    def redraw():
        Text_label = Text_font.render("Please select your gender (does not affect gameplay)", 1, (255,255,255))
        win.blit(Text_label, ((Width/2)-(Text_label.get_width()/2),(Height/2)-(Text_label.get_height()/2)))
    while run:
        clock.tick(FPS)
        redraw()

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

title()

Upvotes: 2

Views: 86

Answers (1)

sloth
sloth

Reputation: 101122

You never call pygame.display.update() or pygame.display.flip() in your second game loop. That's why your screen is never updated.

I suggest using only one game loop and refactor your code using different scenes. You can find some examples here:

Pygame level/menu states
How can I make my Sprite launch an object towards the mouse position?
How to fade the screen out and back in using PyGame?

Upvotes: 1

Related Questions