运行后立即打开和关闭 Pygame [复制]

Pygame opening and closing immediately after running [duplicate]

提问人:alvaius 提问时间:4/7/2023 更新时间:4/7/2023 访问量:64

问:

我正在用 pygame 编写一个简单的 gomoku 游戏,但是当我运行该程序时,pygame 窗口在运行后立即打开和退出。我知道程序本身有问题,因为我用在线 pygame 程序测试了 pygame,它运行良好,但我不确定哪个部分本身是错误的。没有错误,程序只是关闭。程序如下:

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

我尝试添加额外的行,因为我认为我没有正确编写循环,导致程序在运行后立即退出,但这也没有用。

python pygame gomoku

评论

0赞 drum 4/7/2023
你是如何运行的?你在哪里打电话?main()
0赞 Ake 4/7/2023
你在哪里打电话 , , ?你如何使用这个程序?draw_board()check_win()draw_pieces()

答:

-1赞 Ake 4/7/2023 #1

你没有调用 、 ,也没有在主循环的屏幕上绘制和更新你的棋盘和棋子。 这就是你应该的样子,你也需要在最后打电话才能开始游戏。screen.fill()draw_board()draw_pieces()pygame.display.flip()main()main

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()

评论

0赞 alvaius 4/7/2023
啊,我没有磕头,我需要在最后打电话给 main 才能启动它。这是我第一次编写游戏,因为它是为初学者 python 类项目编写的。谢谢!
0赞 Ake 4/7/2023
也是为了你的知识。该语句是一种常见的 Python 习语,用于确保其下面的代码块仅在模块直接运行时执行,而不是在将其作为模块导入另一个程序时执行。if __name__ == "__main__":