Python用Pygame实现一个五子棋小游戏

2023-12-19 23:09:52

下面是一个使用Pygame包实现的五子棋小游戏的示例代码:

import pygame
import sys

# 初始化游戏
pygame.init()

# 设置窗口大小
WIDTH = 640
HEIGHT = 480
SIZE = 15
MARGIN = 20
BOARD_SIZE = SIZE * MARGIN + MARGIN
WINDOW_SIZE = (WIDTH, HEIGHT)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)

# 创建游戏窗口
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("五子棋")

# 创建棋盘
board = [[0 for _ in range(SIZE)] for _ in range(SIZE)]


# 绘制棋盘
def draw_board():
    screen.fill(WHITE)
    for row in range(SIZE):
        for col in range(SIZE):
            pygame.draw.rect(screen, BLACK, (MARGIN + col * MARGIN, MARGIN + row * MARGIN, MARGIN, MARGIN), 1)
            if board[row][col] == 1:
                pygame.draw.circle(screen, BLACK, (MARGIN + col * MARGIN, MARGIN + row * MARGIN), MARGIN // 2)
            elif board[row][col] == 2:
                pygame.draw.circle(screen, BLUE, (MARGIN + col * MARGIN, MARGIN + row * MARGIN), MARGIN // 2)


# 判断胜利条件
def check_win(row, col, player):
    # 判断水平方向
    count = 1
    i = 1
    while col + i < SIZE and board[row][col + i] == player:
        count += 1
        i += 1
    i = 1
    while col - i >= 0 and board[row][col - i] == player:
        count += 1
        i += 1
    if count >= 5:
        return True
    
    # 判断垂直方向
    count = 1
    i = 1
    while row + i < SIZE and board[row + i][col] == player:
        count += 1
        i += 1
    i = 1
    while row - i >= 0 and board[row - i][col] == player:
        count += 1
        i += 1
    if count >= 5:
        return True
    
    # 判断左斜方向
    count = 1
    i = 1
    while row + i < SIZE and col + i < SIZE and board[row + i][col + i] == player:
        count += 1
        i += 1
    i = 1
    while row - i >= 0 and col - i >= 0 and board[row - i][col - i] == player:
        count += 1
        i += 1
    if count >= 5:
        return True
    
    # 判断右斜方向
    count = 1
    i = 1
    while row + i < SIZE and col - i >= 0 and board[row + i][col - i] == player:
        count += 1
        i += 1
    i = 1
    while row - i >= 0 and col + i < SIZE and board[row - i][col + i] == player:
        count += 1
        i += 1
    if count >= 5:
        return True
    
    return False


# 游戏主循环
def main():
    player = 1  # 玩家1为黑子,玩家2为蓝子
    running = True
    game_over = False

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                sys.exit()
            
            if not game_over and event.type == pygame.MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed()[0]:
                    x, y = event.pos
                    col = (x - MARGIN) // MARGIN
                    row = (y - MARGIN) // MARGIN
                    if row >= 0 and row < SIZE and col >= 0 and col < SIZE and board[row][col] == 0:
                        board[row][col] = player
                        draw_board()
                        pygame.display.update()
                        if check_win(row, col, player):
                            msg = "玩家{}赢了!".format(player)
                            font = pygame.font.Font(None, 36)
                            text = font.render(msg, True, BLACK)
                            screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2))
                            pygame.display.update()
                            game_over = True
                        else:
                            player = 2 if player == 1 else 1

        if not game_over:
            draw_board()
            pygame.display.update()

    pygame.quit()


if __name__ == "__main__":
    main()

这个代码创建了一个基于Pygame的五子棋游戏窗口,并通过鼠标点击来下棋。玩家1使用黑子,玩家2使用蓝子。游戏结束时会显示胜利信息。你可以根据需要对代码进行修改和优化。请确保已安装Pygame包,并将上述代码保存为.py文件运行即可开始游戏。

文章来源:https://blog.csdn.net/xukris/article/details/135089218
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。