You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame五子棋程序运行后窗口立即闪退问题求助

Pygame五子棋窗口闪退问题修复

问题根源

  1. 未调用主函数:代码定义了main()函数,但程序末尾没有执行调用,导致程序执行完函数定义就直接终止,窗口瞬间关闭。
  2. 游戏循环缺少必要的绘制与更新:即使进入循环,也没有调用绘制函数生成画面,也没有刷新显示的操作,同时缺少帧率控制,会导致程序运行异常。
  3. 代码结构不完整:main()函数中的while循环未闭合,逻辑代码不完整。

修复后的完整代码

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")
clock = pygame.time.Clock()  # 控制帧率

# Define the function to draw the game board
def draw_board():
    screen.fill((200, 200, 200))  # 填充背景避免画面重叠
    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:
        clock.tick(30)  # 限制帧率为30FPS,避免资源占用过高
        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
        
        # 绘制棋盘和棋子并刷新显示
        draw_board()
        draw_pieces()
        pygame.display.update()
    
    pygame.quit()  # 退出时清理Pygame资源

# 启动主函数
if __name__ == "__main__":
    main()

关键修复点

  • 添加if __name__ == "__main__": main()调用主函数,启动游戏逻辑
  • 加入clock.tick(30)控制帧率,稳定程序运行
  • 在循环内调用绘制函数并刷新显示,生成可见的游戏画面
  • 补充pygame.quit()清理资源,规范程序退出流程
  • 增加背景填充,解决画面重叠问题

内容的提问来源于stack exchange,提问作者alvaius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 14:43:12