Pygame opening and closing immediately after running

Question:

I’m writing a simple game of gomoku with pygame, but when I run the program the pygame windows opens and exits immediately after running. I know it’s something wrong with the program itself as I tested pygame with an online pygame program and it worked fine, but I’m not sure which part itself is wrong. There are no errors, the program just closes. Here’s the program:

import pygame

# Set the dimensions of the game board
BOARD_SIZE = 15

# Set the dimensions of the window
WINDOW_SIZE = (600, 600)

# Set the colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)

# Initialize the game board and history list
board = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
history = []

# Initialize the game
pygame.init()
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Gomoku")

# Define the function to draw the game board
def draw_board():
    for i in range(BOARD_SIZE):
        pygame.draw.line(screen, BLACK, (40 + i * 40, 40), (40 + i * 40, 560))
        pygame.draw.line(screen, BLACK, (40, 40 + i * 40), (560, 40 + i * 40))

# Define the function to draw the pieces on the game board
def draw_pieces():
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            if board[i][j] == 1:
                pygame.draw.circle(screen, BLACK, (40 + i * 40, 40 + j * 40), 18)
            elif board[i][j] == 2:
                pygame.draw.circle(screen, WHITE, (40 + i * 40, 40 + j * 40), 18)

# Define the function to check for a win
def check_win(player):
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            if j + 4 < BOARD_SIZE and sum([board[i][k] == player for k in range(j, j+5)]) == 5:
                return True
            if i + 4 < BOARD_SIZE and sum([board[k][j] == player for k in range(i, i+5)]) == 5:
                return True
            if i + 4 < BOARD_SIZE and j + 4 < BOARD_SIZE and sum([board[i+k][j+k] == player for k in range(5)]) == 5:
                return True
            if i + 4 < BOARD_SIZE and j - 4 >= 0 and sum([board[i+k][j-k] == player for k in range(5)]) == 5:
                return True
    return False

def main():
    player = 1
    running = True
    undo_button = pygame.Rect(450, 250, 100, 50)
    redo_button = pygame.Rect(450, 350, 100, 50)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if 40 <= x <= 560 and 40 <= y <= 560:
                    i, j = (x - 40) // 40, (y - 40) // 40
                    if board[i][j] == 0:
                        board[i][j] = player
                        history.append((i, j, player))
                        if check_win(player):
                            print("Player", player, "wins!")
                            running = False
                        player = 3 - player

I tried adding additional lines since I thought I did not write the loops correctly causing the program to exit immediately after it runs, but that didn’t work either.

Asked By: alvaius

||

Answers:

You didn’t call screen.fill(), draw_board(), draw_pieces() and also pygame.display.flip() to draw and update your board and pieces on the screen in the main loop.
This is how your main() should look, also you need to call main at the end in order to start the game.

def main():
    player = 1
    running = True
    undo_button = pygame.Rect(450, 250, 100, 50)
    redo_button = pygame.Rect(450, 350, 100, 50)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if 40 <= x <= 560 and 40 <= y <= 560:
                    i, j = (x - 40) // 40, (y - 40) // 40
                    if board[i][j] == 0:
                        board[i][j] = player
                        history.append((i, j, player))
                        if check_win(player):
                            print("Player", player, "wins!")
                            running = False
                        player = 3 - player
        
        screen.fill(WHITE)
        draw_board()
        draw_pieces()
        pygame.display.flip()



if __name__ == "__main__":
    main()
Answered By: Ake
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.