Python pygame实现中国象棋单机版源码


Posted in Python onJune 20, 2021

Python中国象棋单机版

鼠标点击操作;两天制作,较为粗糙,很多效果还未实现。

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 13 15:41:56 2021

@author: Administrator
"""

import pygame
from pygame.locals import *
import sys
import math

pygame.init()
screen=pygame.display.set_mode((450,550))
pygame.display.set_caption('中国象棋')

img_board=pygame.image.load('F:/images/中国象棋/board.png')
img_redSoldier=pygame.image.load('F:/images/中国象棋/chess_redSoldier.png')
img_redCannon=pygame.image.load('F:/images/中国象棋/chess_redCannon.png')
img_redCar=pygame.image.load('F:/images/中国象棋/chess_redCar.png')
img_redHorse=pygame.image.load('F:/images/中国象棋/chess_redHorse.png')
img_redElephant=pygame.image.load('F:/images/中国象棋/chess_redElephant.png')
img_redAttendant=pygame.image.load('F:/images/中国象棋/chess_redAttendant.png')
img_chief=pygame.image.load('F:/images/中国象棋/chess_chief.png')
img_blackSoldier=pygame.image.load('F:/images/中国象棋/chess_blackSoldier.png')
img_blackCannon=pygame.image.load('F:/images/中国象棋/chess_blackCannon.png')
img_blackCar=pygame.image.load('F:/images/中国象棋/chess_blackCar.png')
img_blackHorse=pygame.image.load('F:/images/中国象棋/chess_blackHorse.png')
img_blackElephant=pygame.image.load('F:/images/中国象棋/chess_blackElephant.png')
img_blackAttendant=pygame.image.load('F:/images/中国象棋/chess_blackAttendant.png')
img_general=pygame.image.load('F:/images/中国象棋/chess_general.png')

screen.blit(img_board,(0,0))
pygame.display.update()

red_chess=[[0,6],[2,6],[4,6],[6,6],[8,6],[1,7],[7,7],[0,9],[1,9],[2,9],[3,9],[4,9],[5,9],[6,9],[7,9],[8,9]]
black_chess=[[0,3],[2,3],[4,3],[6,3],[8,3],[1,2],[7,2],[0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0]]

#画棋子
def draw_chess():
    for i in range(len(red_chess)):
        if 0<=i<=4:
            screen.blit(img_redSoldier,(red_chess[i][0]*50,red_chess[i][1]*50))
        elif 5<=i<=6:
            screen.blit(img_redCannon,(red_chess[i][0]*50,red_chess[i][1]*50))
        elif i==7 or i==15:
            screen.blit(img_redCar,(red_chess[i][0]*50,red_chess[i][1]*50))
        elif i==8 or i==14:
            screen.blit(img_redHorse,(red_chess[i][0]*50,red_chess[i][1]*50))
        elif i==9 or i==13:
            screen.blit(img_redElephant,(red_chess[i][0]*50,red_chess[i][1]*50))
        elif i==10  or i==12:
            screen.blit(img_redAttendant,(red_chess[i][0]*50,red_chess[i][1]*50))
        else:
            screen.blit(img_chief,(red_chess[i][0]*50,red_chess[i][1]*50))
    for i in range(len(black_chess)):
        if 0<=i<=4:
            screen.blit(img_blackSoldier,(black_chess[i][0]*50,black_chess[i][1]*50))
        elif 5<=i<=6:
            screen.blit(img_blackCannon,(black_chess[i][0]*50,black_chess[i][1]*50))
        elif i==7 or i==15:
            screen.blit(img_blackCar,(black_chess[i][0]*50,black_chess[i][1]*50))
        elif i==8 or i==14:
            screen.blit(img_blackHorse,(black_chess[i][0]*50,black_chess[i][1]*50))
        elif i==9 or i==13:
            screen.blit(img_blackElephant,(black_chess[i][0]*50,black_chess[i][1]*50))
        elif i==10  or i==12:
            screen.blit(img_blackAttendant,(black_chess[i][0]*50,black_chess[i][1]*50))
        else:
            screen.blit(img_general,(black_chess[i][0]*50,black_chess[i][1]*50))
    pygame.display.update()

#返回1表示正常移动,返回2表示有子被吃,返回0表示拒绝移动
#兵移动规则,红兵chess1为red_chess,chess2为black_chess
def soldier_rule(chess1,chess2,current_pos,next_pos):
    if chess1==red_chess:
        pos,index=[5,6],1
    elif chess1==black_chess:
        pos,index=[3,4],-1
    if current_pos[1] in pos:
        if current_pos[0]==next_pos[0] and current_pos[1]==next_pos[1]+index and next_pos not in chess1:
            for i in range(len(chess2)):
                if chess2[i]==next_pos:
                    chess2[i]=[-1,-1]
                    current_pos=next_pos
                    return [current_pos,2]
            current_pos=next_pos
            return [current_pos,1]
    else:
        if current_pos[0]==next_pos[0] and current_pos[1]==next_pos[1]+index and next_pos not in chess1:
            for i in range(len(chess2)):
                if chess2[i]==next_pos:
                    chess2[i]=[-1,-1]
                    current_pos=next_pos
                    return [current_pos,2]
            current_pos=next_pos
            return [current_pos,1]
        elif current_pos[1]==next_pos[1] and current_pos[0]+1==next_pos[0] and next_pos not in chess1:
            for i in range(len(chess2)):
                if chess2[i]==next_pos:
                    chess2[i]=[-1,-1]
                    current_pos=next_pos
                    return [current_pos,2]
            current_pos=next_pos
            return [current_pos,1]
        elif current_pos[1]==next_pos[1] and current_pos[0]-1==next_pos[0] and next_pos not in chess1:
            for i in range(len(chess2)):
                if chess2[i]==next_pos:
                    chess2[i]=[-1,-1]
                    current_pos=next_pos
                    return [current_pos,2]
            current_pos=next_pos
            return [current_pos,1]

#车移动规则,红车前两个参数为red_chess、black_chess,黑车前两个参数为black_chess、red_chess
def car_rule(chess1,chess2,current_pos,next_pos):
    if next_pos not in chess1 and current_pos[0]==next_pos[0]:
        a,b=current_pos,next_pos
        if a[1]>b[1]:
            a,b=b,a
        for i in range(a[1]+1,b[1]):
            if [a[0],i] in black_chess+red_chess:
                return 0
        for i in range(len(chess2)):
            if chess2[i]==next_pos:
                chess2[i]=[-1,-1]
                current_pos=next_pos
                return [current_pos,2]
        current_pos=next_pos
        return [current_pos,1]
    elif next_pos not in chess1 and current_pos[1]==next_pos[1]:
        a,b=current_pos,next_pos
        if a[0]>b[0]:
            a,b=b,a
        for i in range(a[0]+1,b[0]):
            if [i,a[1]] in black_chess+red_chess:
                return 0
        for i in range(len(chess2)):
            if chess2[i]==next_pos:
                chess2[i]=[-1,-1]
                current_pos=next_pos
                return [current_pos,2]
        current_pos=next_pos
        return [current_pos,1]

#炮移动规则
def cannon_rule(chess1,chess2,current_pos,next_pos):
    if next_pos not in chess1 and current_pos[0]==next_pos[0]:
        num=0
        a,b=current_pos,next_pos
        if a[1]>b[1]:
            a,b=b,a
        for i in range(a[1]+1,b[1]):
            if [a[0],i] in black_chess+red_chess:
                num+=1
        if num==1:
            for i in range(len(chess2)):
                if chess2[i]==next_pos:
                    chess2[i]=[-1,-1]
                    current_pos=next_pos
                    return [current_pos,2]
            return 0
        elif num==0:
            current_pos=next_pos
            return [current_pos,1]
        else:
            return 0
    elif next_pos not in chess1 and current_pos[1]==next_pos[1]:
        num=0
        a,b=current_pos,next_pos
        if a[0]>b[0]:
            a,b=b,a
        for i in range(a[0]+1,b[0]):
            if [i,a[1]] in black_chess+red_chess:
                num+=1
        if num==1:
            for i in range(len(chess2)):
                if chess2[i]==next_pos:
                    chess2[i]=[-1,-1]
                    current_pos=next_pos
                    return [current_pos,2]
            return 0
        elif num==0:
            current_pos=next_pos
            return [current_pos,1]
        else:
            return 0

#马移动规则,红马chess为black_chess
def horse_rule(chess,current_pos,next_pos):
    index=[[2,-1],[1,-2],[-1,-2],[-2,-1],[-2,1],[-1,2],[1,2],[2,1]]
    leg=[[1,0],[0,-1],[0,-1],[-1,0],[-1,0],[0,1],[0,1],[1,0]]
    if next_pos not in red_chess+black_chess:
        for i in range(len(index)):
            if current_pos[0]+index[i][0]==next_pos[0] and current_pos[1]+index[i][1]==next_pos[1]:
                if [current_pos[0]+leg[i][0],current_pos[1]+leg[i][1]] not in black_chess+red_chess:
                    current_pos=next_pos
                    return [current_pos,1]
    elif next_pos in chess:
        for i in range(len(index)):
            if current_pos[0]+index[i][0]==next_pos[0] and current_pos[1]+index[i][1]==next_pos[1]:
                if [current_pos[0]+leg[i][0],current_pos[1]+leg[i][1]] not in black_chess+red_chess:
                    for i in range(len(chess)):
                        if chess[i]==next_pos:
                            chess[i]=[-1,-1]
                            current_pos=next_pos
                            return [current_pos,2]

#象移动规则,红相chess为black_chess
def elephant_rule(chess,current_pos,next_pos):
    index=[[2,-2],[-2,-2],[-2,2],[2,2]]
    leg=[[1,-1],[-1,-1],[-1,1],[1,1]]
    if chess==black_chess:
        pos=[5,7,9]
    elif chess==red_chess:
        pos=[0,2,4]
    if next_pos not in red_chess+black_chess and next_pos[1] in pos:
        for i in range(len(index)):
            if current_pos[0]+index[i][0]==next_pos[0] and current_pos[1]+index[i][1]==next_pos[1]:
                if [current_pos[0]+leg[i][0],current_pos[1]+leg[i][1]] not in black_chess+red_chess:
                    current_pos=next_pos
                    return [current_pos,1]
    elif next_pos in chess and next_pos[1] in pos:
        for i in range(len(index)):
            if current_pos[0]+index[i][0]==next_pos[0] and current_pos[1]+index[i][1]==next_pos[1]:
                if [current_pos[0]+leg[i][0],current_pos[1]+leg[i][1]] not in black_chess+red_chess:
                    for i in range(len(chess)):
                        if chess[i]==next_pos:
                            chess[i]=[-1,-1]
                            current_pos=next_pos
                            return [current_pos,2]

#士移动规则
def attendant_rule(chess1,chess2,current_pos,next_pos):
    if chess1==red_chess:
        pos1=[[3,9],[3,7],[5,7],[5,9]]
        pos2=[4,8]
    elif chess1==black_chess:
        pos1=[[3,0],[3,2],[5,2],[5,0]]
        pos2=[4,1]
    if current_pos in pos1 and next_pos==pos2 and next_pos not in chess1:
        if next_pos not in chess2:
            current_pos=next_pos
            return [current_pos,1]
        else:
            for i in range(len(chess2)):
                if chess2[i]==next_pos:
                    chess2[i]=[-1,-1]
                    current_pos=next_pos
                    return [current_pos,2]
    elif current_pos==pos2 and next_pos in pos1 and next_pos not in chess1:
        if next_pos not in chess2:
            current_pos=next_pos
            return [current_pos,1]
        else:
            for i in range(len(chess2)):
                if chess2[i]==next_pos:
                    chess2[i]=[-1,-1]
                    current_pos=next_pos
                    return [current_pos,2]

#将帅移动规则
def boss_rule(chess1,chess2,current_pos,next_pos,j_pos):
    if chess1==red_chess:
        pos=[7,8,9]
    elif chess1==black_chess:
        pos=[0,1,2]
    flag=0
    if next_pos not in chess1:
        if next_pos[0]==j_pos[0]:
            for i in range(j_pos[1]+1,next_pos[1]):
                if [j_pos[0],j_pos[1]+i] in black_chess+red_chess:
                    flag=1
                    break
            if flag==0:
                return 0
    if next_pos not in chess1 and 3<=next_pos[0]<=5 and next_pos[1] in pos:
        if next_pos not in chess2:
            if current_pos[0]==next_pos[0]:
                if current_pos[1]+1==next_pos[1] or current_pos[1]-1==next_pos[1]:
                    current_pos=next_pos
                    return [current_pos,1]
            elif current_pos[1]==next_pos[1]:
                if current_pos[0]+1==next_pos[0] or current_pos[0]-1==next_pos[0]:
                    current_pos=next_pos
                    return [current_pos,1]
        else:
            if current_pos[0]==next_pos[0]:
                if current_pos[1]+1==next_pos[1] or current_pos[1]-1==next_pos[1]:
                    for i in range(len(chess2)):
                        if chess2[i]==next_pos:
                            chess2[i]=[-1,-1]
                            current_pos=next_pos
                            return [current_pos,2]
            elif current_pos[1]==next_pos[1]:
                if current_pos[0]+1==next_pos[0] or current_pos[0]-1==next_pos[0]:
                    for i in range(len(chess2)):
                        if chess2[i]==next_pos:
                            chess2[i]=[-1,-1]
                            current_pos=next_pos
                            return [current_pos,2]

#棋子移动
def move(chess1,chess2,next_pos):
    x=0
    if i in range(5):   #兵
        x=soldier_rule(chess1,chess2,chess1[i],next_pos)
        if x!=None and x!=0:
            chess1[i]=x[0]
            print(chess1[i])
            screen.blit(img_board,(0,0))
            draw_chess()
    elif i==5 or i==6:  #炮
        x=cannon_rule(chess1,chess2,chess1[i],next_pos)
        if x!=None and x!=0:
            chess1[i]=x[0]
            print(chess1[i])
            screen.blit(img_board,(0,0))
            draw_chess()
    elif i==7 or i==15: #?
        x=car_rule(chess1,chess2,chess1[i],next_pos)
        if x!=None and x!=0:
            chess1[i]=x[0]
            print(chess1[i])
            screen.blit(img_board,(0,0))
            draw_chess()
    elif i==8 or i==14: #?
        x=horse_rule(chess2,chess1[i],next_pos)
        if x!=None and x!=0:
            chess1[i]=x[0]
            print(chess1[i])
            screen.blit(img_board,(0,0))
            draw_chess()
    elif i==9 or i==13: #相
        x=elephant_rule(chess2,chess1[i],next_pos)
        if x!=None and x!=0:
            chess1[i]=x[0]
            print(chess1[i])
            screen.blit(img_board,(0,0))
            draw_chess()
    elif i==10 or i==12:    #仕
        x=attendant_rule(chess1,chess2,chess1[i],next_pos)
        if x!=None and x!=0:
            chess1[i]=x[0]
            print(chess1[i])
            screen.blit(img_board,(0,0))
            draw_chess()
    else:   #??
        x=boss_rule(chess1,chess2,chess1[i],next_pos,chess2[11])
        if x!=None and x!=0:
            chess1[i]=x[0]
            print(chess1[i])
            screen.blit(img_board,(0,0))
            draw_chess()
    return x

white=(255,255,255)
black=(0,0,0)
def draw_text(text,x,y,size):
    pygame.font.init()
    fontObj=pygame.font.SysFont('SimHei',size )
    textSurfaceObj=fontObj.render(text, True, white,black)
    textRectObj=textSurfaceObj.get_rect()
    textRectObj.center=(x,y)
    screen.blit(textSurfaceObj, textRectObj)
    pygame.display.update()

#判断游戏是否结束
def game_over():
    if red_chess[11]==[-1,-1]:
        draw_text('黑方胜利',225,525,15)
        return 1
    elif black_chess[11]==[-1,-1]:
        draw_text('红方胜利',225,525,15)
        return 1

if __name__=='__main__':
    all_pos,progress=[],[]
    for i in range(10):
        for j in range(9):
            all_pos.append([j,i])
    draw_text('红方先走',225,525,15)
    chess_kind=0
    while True:
        draw_chess()
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
            elif event.type==MOUSEBUTTONDOWN:
                pos=pygame.mouse.get_pos()
                print(pos)
                if chess_kind==0:
                    chess1,chess2=red_chess,black_chess
                elif chess_kind==1:
                    chess1,chess2=black_chess,red_chess
                for i in range(len(chess1)):
                    if chess1[i][0]*50<pos[0]<(chess1[i][0]+1)*50 and chess1[i][1]*50<pos[1]<(chess1[i][1]+1)*50:
                        flag=False
                        while True:
                            for event in pygame.event.get():
                                if event.type==MOUSEBUTTONDOWN:
                                    pos=pygame.mouse.get_pos()
                                    next_pos=[pos[0]//50,pos[1]//50]
                                    flag=True
                                    break
                            if flag==True:
                                break
                        progress.append(move(chess1,chess2,next_pos))
                        if progress[-1]!=None and progress[-1]!=0:
                            if chess_kind==0:
                                chess_kind=1
                            elif chess_kind==1:
                                chess_kind=0
                        if chess_kind==1:
                            draw_text('轮到黑方',225,525,15)
                        elif chess_kind==0:
                            draw_text('轮到红方',225,525,15)
                        if game_over()==1:
                            while True:
                                for event in pygame.event.get():
                                    if event.type==QUIT:
                                        pygame.quit()
                                        sys.exit()
                        break

棋盘图片:

Python pygame实现中国象棋单机版源码

棋子图片:

Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码
Python pygame实现中国象棋单机版源码

运行效果:

Python pygame实现中国象棋单机版源码

到此这篇关于Python pygame实现中国象棋单机版源码的文章就介绍到这了,更多相关pygame实现中国象棋内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python通过pil将图片转换成黑白效果的方法
Mar 16 Python
使用python加密自己的密码
Aug 04 Python
python基础知识小结之集合
Nov 25 Python
Python绘制3d螺旋曲线图实例代码
Dec 20 Python
python使用生成器实现可迭代对象
Mar 20 Python
Empty test suite.(PyCharm程序运行错误的解决方法)
Nov 30 Python
详解Python数据分析--Pandas知识点
Mar 23 Python
python实现关闭第三方窗口的方法
Jun 28 Python
Python制作微信好友背景墙教程(附完整代码)
Jul 17 Python
Python高级编程之继承问题详解(super与mro)
Nov 19 Python
浅谈pymysql查询语句中带有in时传递参数的问题
Jun 05 Python
利用Python实现自动扫雷小脚本
Dec 17 Python
Python并发编程实例教程之线程的玩法
Jun 20 #Python
python迷宫问题深度优先遍历实例
Jun 20 #Python
Python虚拟环境virtualenv是如何使用的
Python机器学习之底层实现KNN
Jun 20 #Python
利用python进行数据加载
Jun 20 #Python
Python编解码问题及文本文件处理方法详解
浅谈Python协程asyncio
Jun 20 #Python
You might like
PHP 开源框架22个简单简介
2009/08/24 PHP
仿Aspnetpager的一个PHP分页类代码 附源码下载
2012/10/08 PHP
PHP7新增运算符用法实例分析
2016/09/26 PHP
Yii框架中用response保存cookie,用request读取cookie的原理解析
2019/09/04 PHP
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
Jquery创建一个层当鼠标移动到层上面不消失效果
2013/12/12 Javascript
JavaScript的jQuery库中function的存在和参数问题
2015/08/13 Javascript
JS使用eval()动态创建变量的方法
2016/06/03 Javascript
Json解析的方法小结
2016/06/22 Javascript
js实现倒计时关键代码
2017/05/05 Javascript
原生js+cookie实现购物车功能的方法分析
2017/12/21 Javascript
vue单页面在微信下只能分享落地页的解决方案
2019/04/15 Javascript
Vue移动端右滑屏幕返回上一页附源码下载
2019/06/26 Javascript
vue 实现单选框设置默认选中值
2019/11/07 Javascript
[00:35]DOTA2上海特级锦标赛 MVP.Phx战队宣传片
2016/03/04 DOTA
[55:03]LGD vs EG 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
浅谈Python爬取网页的编码处理
2016/11/04 Python
Python基于QRCode实现生成二维码的方法【下载,安装,调用等】
2017/07/11 Python
python实现发送邮件功能
2017/07/22 Python
Python使用当前时间、随机数产生一个唯一数字的方法
2017/09/18 Python
Python使用smtp和pop简单收发邮件完整实例
2018/01/09 Python
python TKinter获取文本框内容的方法
2018/10/11 Python
python+opencv像素的加减和加权操作的实现
2019/07/14 Python
美国最大的烧烤架和户外生活用品专业零售商:Barbeques Galore
2021/01/09 全球购物
EJB面试题
2015/07/28 面试题
班组长安全生产职责
2013/12/16 职场文书
企业安全生产月活动总结
2014/07/05 职场文书
开展批评与自我批评心得体会
2014/10/17 职场文书
委托书范本格式
2019/04/18 职场文书
Python控制台输出俄罗斯方块的方法实例
2021/04/17 Python
浅谈Python 中的复数问题
2021/05/19 Python
浏览器常用基本操作之python3+selenium4自动化测试(基础篇3)
2021/05/21 Python
SQL注入的实现以及防范示例详解
2021/06/02 MySQL
Java实现简易的分词器功能
2021/06/15 Java/Android
浅谈Node的内存泄露问题
2022/05/06 NodeJs
Go gorilla/sessions库安装使用
2022/08/14 Golang