使用pygame制作双人五子棋小游戏

源码如下:

import pygame
import time

pygame.init()


class Chess:
    screen = pygame.display.set_mode((690, 690))
    clock = pygame.time.Clock()

    def __init__(self):
        # 未下棋0,黑棋1,白棋-1
        self.chess_panel = [
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        ]

        self.chess_draw = [
            [
                (45, 45), (85, 45), (125, 45), (165, 45), (205, 45),
                (245, 45), (285, 45), (325, 45), (365, 45), (405, 45),
                (445, 45), (485, 45), (525, 45), (565, 45), (605, 45),
                (645, 45)
            ], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []
        ]

        pygame.display.set_caption("单机双人五子棋    version:1.0")

        black = (0, 0, 0)
        bg_color = (237, 182, 102)
        # bg_color = (167, 123, 25)

        Chess.screen.fill(bg_color)

        # 横线
        a, b = 45, 645
        for i in range(0, 16):
            pygame.draw.aaline(Chess.screen, black, (a + i*40, a), (a + i*40, b), 1)

        # 竖线
        for i in range(0, 16):
            pygame.draw.aaline(Chess.screen, black, (a, a + i*40), (b, a + i*40), 1)

        self.image_hei = pygame.image.load("./images/hei.png")
        self.image_bai = pygame.image.load("./images/bai.png")

        # 点位计算
        for i in range(1, 16):
            for j in range(0, 16):
                self.chess_draw[i].insert(j, (self.chess_draw[0][j][0], self.chess_draw[i - 1][j][1] + 40))
                # print(chess_draw[i][j], end="  ")
            # print()

    # 在当前点位下棋,黑棋1,白棋-1
    def chess(self, tuple1, index_chess):
        index_i, index_j = tuple1[0], tuple1[1]
        pos_x, pos_y = self.chess_draw[index_i][index_j]
        tuple2 = pos_x - 17, pos_y - 17
        if index_chess == 1:
            Chess.screen.blit(self.image_hei, tuple2)
            self.chess_panel[index_i][index_j] = 1
        else:
            Chess.screen.blit(self.image_bai, tuple2)
            self.chess_panel[index_i][index_j] = -1

    # 判断当前下标位能不能下棋
    def judgment_index(self, tuple1):
        if tuple1:
            index_i, index_j = tuple1[0], tuple1[1]
            return True if self.chess_panel[index_i][index_j] == 0 else False
        else:
            return False

    # return 当前鼠标点击位置转换为的点位下标位置
    def change(self, tuple1):
        x, y = tuple1[0], tuple1[1]
        for ii in range(0, 16):
            for jj in range(0, 16):
                left = self.chess_draw[ii][jj][0] - 10
                right = self.chess_draw[ii][jj][0] + 10
                top = self.chess_draw[ii][jj][1] - 10
                bottom = self.chess_draw[ii][jj][1] + 10
                if left <= x <= right and top <= y <= bottom:
                    return ii, jj

    # 判断胜负
    def win(self):
        # 横
        for ii in range(0, 16):
            for jj in range(0, 11):
                list_slice = self.chess_panel[ii][jj:jj + 5]
                if list_slice[0] != 0:
                    if list_slice.count(1) == 5:
                        # 黑胜
                        return 1
                    else:
                        if list_slice.count(-1) == 5:
                            # 白胜
                            return -1
        # 竖
        for ii in range(0, 11):
            for jj in range(0, 16):
                list_slice = self.chess_panel[ii][jj], self.chess_panel[ii + 1][jj], \
                             self.chess_panel[ii + 2][jj], self.chess_panel[ii + 3][jj], self.chess_panel[ii + 4][jj]
                if list_slice[0] != 0:
                    if list_slice.count(1) == 5:
                        # 黑胜
                        return 1
                    else:
                        if list_slice.count(-1) == 5:
                            # 白胜
                            return -1
        # 斜
        # 右下斜
        for ii in range(0, 12):
            for jj in range(0, 12):
                list_slice = self.chess_panel[ii][jj], self.chess_panel[ii + 1][jj + 1], \
                             self.chess_panel[ii + 2][jj + 2], self.chess_panel[ii + 3][jj + 3], \
                             self.chess_panel[ii + 4][jj + 4]
                if list_slice[0] != 0:
                    if list_slice.count(1) == 5:
                        # 黑胜
                        return 1
                    else:
                        if list_slice.count(-1) == 5:
                            # 白胜
                            return -1
        # 左下斜
        for ii in range(4, 16):
            for jj in range(0, 12):
                list_slice = self.chess_panel[ii][jj], self.chess_panel[ii - 1][jj + 1], \
                             self.chess_panel[ii - 2][jj + 2], self.chess_panel[ii - 3][jj + 3],\
                             self.chess_panel[ii - 4][jj + 4]
                if list_slice[0] != 0:
                    if list_slice.count(1) == 5:
                        # 黑胜
                        return 1
                    else:
                        if list_slice.count(-1) == 5:
                            # 白胜
                            return -1

    # 打印胜负情况
    @staticmethod
    def print_win(win_index):
        if win_index is not None:
            text = pygame.font.SysFont("宋体", 70)
            if win_index == 1:
                text_fmt = text.render("Black win", 1, (208, 2, 27))
            else:
                text_fmt = text.render("White win", 1, (208, 2, 27))
            Chess.screen.blit(text_fmt, (240, 320))
            return True
        else:
            return False


if __name__ == '__main__':
    chess_obj = Chess()
    flag = True
    while True:
        chess_obj.clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONUP and flag:
                tuple_point = chess_obj.change(pygame.mouse.get_pos())
                if chess_obj.judgment_index(tuple_point):
                    chess_obj.chess(tuple_point, 1)
                    flag = False
            else:
                if event.type == pygame.MOUSEBUTTONUP:
                    tuple_point = chess_obj.change(pygame.mouse.get_pos())
                    if chess_obj.judgment_index(tuple_point):
                        chess_obj.chess(tuple_point, -1)
                        flag = True
            if Chess.print_win(chess_obj.win()):
                pygame.display.update()
                time.sleep(3)
                chess_obj = Chess()
                flag = True
            else:
                pygame.display.update()

图片素材:
下载棋子1
下载棋子2
如果图片无法下载,请更换自己的图片素材

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值