0
votes

So I created a Python game Tetris based on Youtube tutorial: https://www.youtube.com/watch?v=zfvxp7PgQ6c&t=2075s

But the pygame.error: display Surface quit occurs.

I have tried to add "break", "sys.exit()", "QUIT" after the pygame.quit but does not work.

Does anyone know how to solve it? Here is the code: (You can skip to the def main_menu)

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    current_piece.x -= 1
                    if not (valid_space(current_piece, grid)):
                        current_piece.x += 1
                if event.key == pygame.K_RIGHT:
                    current_piece.x += 1
                    if not (valid_space(current_piece, grid)):
                        current_piece.x -= 1
                if event.key == pygame.K_DOWN:
                    current_piece.y += 1
                    if not (valid_space(current_piece, grid)):
                        current_piece.y -= 1
                    if event.key == pygame.K_UP:
                        current_piece.rotation += current_piece.rotation + 1 % len(current_piece.shape)
                    if not (valid_space(current_piece, grid)):
                        current_piece.rotation -= 1

                    shape_pos = convert_shape_format(current_piece)

                    for i in range(len(shape_pos)):
                        x, y = shape_pos[i]
                    if y > -1:
                        grid[y][x] = current_piece.color

                    if change_piece:
                        for pos in shape_pos:
                         p = (pos[0], pos[1])
                    locked_positions[p] = current_piece.color
                    current_piece = next_piece
                    next_piece = get_shape()
                    change_piece = False
                    score += clear_rows(grid, locked_positions) * 10

                    draw_window(win, grid, score, last_score)
                    draw_next_shape(next_piece, win)
                    pygame.display.update()

                    if check_lost(locked_positions):
                        draw_text_middle(win, "You Lost!", 80, (255,255,255))
                        pygame.display.update()
                        pygame.time.delay(1500)
                        run = False
                        update_score(score)
def main_menu(win):
    run = True
    while run:
        win.fill((0,0,0))
        draw_text_middle(win, 'Press any key to play', 60, (255,255,255))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.display.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                main(win)

    pygame.display.QUIT()

win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)

Updated code:

def main_menu(win):
    run = True
    while run:
        win.fill((0,0,0))
        draw_text_middle(win, 'Press any key to play', 60, (255,255,255))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            pygame.quit()
            quit()

            if event.type == pygame.KEYDOWN:
                main(win)

    pygame.quit()

win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)

1
What @Rabbid76 said. Did you mean pygame.quit() ? - Kingsley
Do pygame.quit() after the main application loop. pygame.display.quit() and pygame.display.QUIT() do not exist. - Rabbid76
Thanks. I'll Try. - hlan19
@Kingsley Yes, I just follow the instruction so can be wrong - hlan19
now in my code there is no error but the program just pop up for a few seconds and it quit, I just update the new code in my post @Rabbid76, - hlan19

1 Answers

0
votes
  1. In your main_menu loop you are telling it to loop while local boolean run == True. This is okay, but you should as people mentioned in the comments do a pygame.quit() and optionally quit() (closes the window) instead of the pygame.display.quit() and sys.exit() that you have right now.

  2. The second problem occurs if you start the game by going into the main loop. I assume that the main loop runs your events function shown at the top?

    Depending on how you have written the code, the boolean run in the event function is local. This means that it will not change the value of the run you are using in your main loop (nor change it in the main_menu loop). I would suggest to transfer into OOP and create a self.run boolean instead, or else you need to make the boolean run global.

    And you should in the event function write this instead of what you have now at the top:

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

Hope this helps!