欢迎访问宙启技术站
智能推送

python实现简单五子棋游戏

发布时间:2023-05-18 19:45:49

五子棋是中国古代的一种传统棋类游戏,它是围棋之外一种非常受欢迎的棋类游戏,离线版本的五子棋游戏可以在家庭聚会、休闲时使用,但随着计算机的发展,网络上也出现了无数的五子棋游戏。在这里我们将使用Python实现一款简单五子棋游戏。

首先我们需要导入pygame模块,使用pygame来绘制游戏界面。然后定义一个棋盘类, 并实现棋盘的绘制和落子功能。棋子的颜色用黑色和白色表示,每个棋子的大小和棋盘上每个格子的大小一致。

import pygame

class Board:

    def __init__(self, screen):

        self.screen = screen

        self.width = 640

        self.height = 640

        self.grid_size = 40

        self.grid_num = 15

        self.bg_color = (249, 214, 91)

        self.grid_color = (0, 0, 0)

        self.players = ['black', 'white']

        self.current_player = 0

        self.board = [[-1] * self.grid_num for _ in range(self.grid_num)]

        

    def draw_board(self):

        self.screen.fill(self.bg_color)

        for i in range(self.grid_num):

            pygame.draw.line(self.screen, self.grid_color, (i*self.grid_size + self.grid_size//2, self.grid_size//2), (i*self.grid_size + self.grid_size//2, self.height - self.grid_size//2), 2)

            pygame.draw.line(self.screen, self.grid_color, (self.grid_size//2, i*self.grid_size + self.grid_size//2), (self.width - self.grid_size//2, i*self.grid_size + self.grid_size//2), 2)

    

    def get_x_y(self, mouse_pos):

        x = mouse_pos[0] // self.grid_size

        y = mouse_pos[1] // self.grid_size

        return x, y

    def put_chess(self, pos):

        if self.board[pos[1]][pos[0]] == -1:

            self.board[pos[1]][pos[0]] = self.current_player

            self.draw_chess(pos, self.current_player)

            self.current_player = 1 - self.current_player

    def draw_chess(self, pos, player):

        x = pos[0]*self.grid_size + self.grid_size//2

        y = pos[1]*self.grid_size + self.grid_size//2

        if player == 0:

            pygame.draw.circle(self.screen, (0, 0, 0), (x, y), self.grid_size//2-2, 2)

        else:

            pygame.draw.circle(self.screen, (255, 255, 255), (x, y), self.grid_size//2-2, 2)

我们还需要实现游戏的主循环,这个循环用于监测用户的输入和更新游戏状态。在主循环中,我们需要监测用户的鼠标点击事件, 并将鼠标点击事件转换为落子位置,然后使用put_chess()方法来更新棋盘状态。我们还需要检查游戏是否结束,如果结束,我们将显示游戏结束的消息。

def run_game():

    pygame.init()

    screen = pygame.display.set_mode((640, 640))

    pygame.display.set_caption('五子棋')

    board = Board(screen)

    game_over = False

    font = pygame.font.SysFont('SimHei', 48)

    while not game_over:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                game_over = True

            elif event.type == pygame.MOUSEBUTTONDOWN:

                pos = board.get_x_y(pygame.mouse.get_pos())

                board.put_chess(pos)

        board.draw_board()

        status = check_status(board.board)

        if status == 0:

            text = font.render('黑棋胜利', True, (0, 0, 0))

            screen.blit(text, (200, 300))

            game_over = True

        elif status == 1:

            text = font.render('白棋胜利', True, (255, 255, 255))

            screen.blit(text, (200, 300))

            game_over = True

        elif status == 2:

            text = font.render('平局', True, (0, 0, 0))

            screen.blit(text, (270, 300))

            game_over = True

        pygame.display.flip()

    pygame.quit()

在check_status()方法中,我们需要检查是否有五子连珠,如果有,则游戏结束并返回胜利方;如果达到最大回合数但仍未分出胜负,则游戏结束并宣布平局。

def check_status(board):

    for i in range(len(board)):

        for j in range(len(board[0])):

            if board[i][j] == -1:

                continue

            if j+4 < len(board[0]) and board[i][j] == board[i][j+1] == board[i][j+2] == board[i][j+3] == board[i][j+4]:

                return board[i][j]

            if i+4 < len(board) and board[i][j] == board[i+1][j] == board[i+2][j] == board[i+3][j] == board[i+4][j]:

                return board[i][j]

            if i+4 < len(board) and j+4 < len(board[0]) and board[i][j] == board[i+1][j+1] == board[i+2][j+2] == board[i+3][j+3] == board[i+4][j+4]:

                return board[i][j]

            if i+4 < len(board) and j-4 >= 0 and board[i][j] == board[i+1][j-1] == board[i+2][j-2] == board[i+3][j-3] == board[i+4][j-4]:

                return board[i][j]

    for row in board:

        for col in row:

            if col == -1:

                return -1

    return 2

最后,我们只需要调用run_game()方法就可以开始游戏了。

run_game()

综上所述,这就是用Python实现简单五子棋游戏的完整代码。希望对初学者们有所帮助,也希望大家可以通过不断学习和实践,编写出更加优秀的游戏作品。