Python3+pygame中国象棋 代码完整 非常好 有效果演示

本文介绍了一款使用Python3和pygame库编写的中国象棋游戏。作者受抖音上的象棋对局启发,决定自己动手制作。文章展示了游戏的运行效果,并提供了完整的代码供读者参考。
摘要由CSDN通过智能技术生成

这几天看到抖音上有个妹子下象棋超级猛,我的中国象棋也差不到哪去啊,走 做一个。。。。 本中国象棋是用Python3+pygame实现的

一、运行效果

![](

二、代码

下面的代码用到图片素材(images文件夹),下载地​ 址如下:www.itprojects.cn/detail.html… ​

"""
作者:it项目实例网
网址:www.itprojects.cn
"""

import sys

import pygame

# 要显示的窗口的宽、高
WIDTH, HEIGHT = 750, 667


class ClickBox(pygame.sprite.Sprite):
    """
    选中棋子对象
    """
    singleton = None

    def __new__(cls, *args, **kwargs):
        if cls.singleton is None:
            cls.singleton = super().__new__(cls)
        return cls.singleton

    def __init__(self, screen, row, col, team):
        super().__init__()
        self.image = pygame.image.load("images/r_box.png")
        self.rect = self.image.get_rect()
        self.row, self.col = row, col
        self.rect.topleft = (50 + self.col * 57, 50 + self.row * 57)
        self.screen = screen
        self.team = team

    @classmethod
    def show(cls):
        if cls.singleton:
            cls.singleton.screen.blit(cls.singleton.image, cls.singleton.rect)

    @classmethod
    def clean(cls):
        """
        清理上次的对象
        """
        cls.singleton = None


class Dot(pygame.sprite.Sprite):
    """
    可落棋子类
    """
    group = list()

    def __init__(self, screen, position):
        super().__init__()
        self.image = pygame.image.load("images/dot2.png")
        self.rect = self.image.get_rect()
        self.row, self.col = position  # 将元组拆包
        self.rect.topleft = (60 + self.col * 57, 60 + self.row * 57)
        self.group.append(self)
        self.screen = screen

    @classmethod
    def show(cls):
        for dot in cls.group:
            dot.screen.blit(dot.image, dot.rect)

    @classmethod
    def clean_last_postion(cls):
        """
        清除上次落子位置
        """
        cls.group.clear()

    @classmethod
    def click(cls):
        """
        点击棋子
        """
        for dot in cls.group:
            if pygame.mouse.get_pressed()[0] and dot.rect.collidepoint(pygame.mouse.get_pos()):
                print("被点击了「可落子」对象")
                return dot


class Chess(pygame.sprite.Sprite):
    """
    棋子类
    """

    def __init__(self, screen, chess_name, row, col):
        self.screen = screen
        self.image = pygame.image.load("images/" + chess_name + ".png")
        self.rect = self.image.get_rect()
        self.rect.topleft = (50 + col * 57, 50 + row * 57)
        self.team = chess_name[0]  # 队伍(红方 r、黑方b)
        self.name = chess_name[2]  # 名字(炮p、马m等)
        self.row = row
        self.col = col

    def show(self):
        self.screen.blit(self.image, self.rect)

    @staticmethod
    def click(player, chesses):
        """
        点击棋子
        """
        for chess in chesses:
            if pygame.mouse.get_pressed()[0] and chess.rect.collidepoint(pygame.mouse.get_pos()):
                if player == chess.team:
                    print("被点击了")
                    return chess

    def update_postion(self, new_row, new_col):
        """
        更新要显示的图片的坐标
        """
        self.row = new_row
        self.col = new_col
        self.rect.topleft = (50 + new_col * 57, 50 + new_row * 57)


class ChessBoard(object):
    """
    棋盘类
    """

    def __init__(self, screen):
        self.screen = screen
        self.image = pygame.image.load("images/bg.png")
        self.topleft = (50, 50)
        self.__create_default_chess()

    def __create_default_chess(self):
        """
        创建默认棋子
        """
        self.map = [
            ["b_c", "b_m", "b_x", "b_s", "b_j", "b_s", "b_x", "b_m", "b_c"],
            ["", "", "", "", "", "", "", "", ""],
            ["", "b_p", "", "", "", "", "", "b_p", ""],
            ["b_z", "", "b_z", "", "b_z", "", "b_z", "", "b_z"],
            ["", "", "", "", "", "", "", "", ""],
            ["", "", "", "", "", "", "", "", ""],
            ["r_z", "", "r_z", "", "r_z", "", "r_z", "", "r_z"],
            ["", "r_p", "", "", "", "", "", "r_p", ""],
            ["", "", "", "", "", "", "", "", ""],
            ["r_c", "r_m", "r_x", "r_s", "r_j", "r_s", "r_x", "r_m", "r_c"],
        ]
        for row, line in enumerate(self.map):
            for col, chess_name in enumerate(line):
                if chess_name:
                    # 将创建的棋子添加到属性map中
                    self.map[row][col] = Chess(self.screen, chess_name, row, col)
                else:
                    self.map[row][col] = None

    def show(self):
        # 显示棋盘
        self.screen.blit(self.image, self.topleft)
        # 显示棋盘上的所有棋子
        for line_chess in self.map:
            for chess in line_chess:
                if chess:
                    chess.show()

    def get_put_down_postion(self, clicked_chess):
        """
        计算当前棋子可以移动的位置
        """
        # 存储当前棋子可以落子的位置
        all_position = list()
        # 拿到当前棋子的行、列
        row, col = clicked_chess.r
  • 11
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值