Hello,之前有粉丝要求我写一篇关于象棋游戏的,好啦,今天我就来发表一篇关于象棋的文章,不多说,直接上代码

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 13 15:41:56 2021@author: Administrator
"""import pygame
from pygame.locals import *
import sys
import mathpygame.init()
screen=pygame.display.set_mode((450,550))
pygame.display.set_caption('中国象棋')img_board=pygame.image.load('board.png')
img_redSoldier=pygame.image.load('chess_redSoldier.png')
img_redCannon=pygame.image.load('chess_redCannon.png')
img_redCar=pygame.image.load('chess_redCar.png')
img_redHorse=pygame.image.load('chess_redHorse.png')
img_redElephant=pygame.image.load('chess_redElephant.png')
img_redAttendant=pygame.image.load('chess_redAttendant.png')
img_chief=pygame.image.load('chess_chief.png')
img_blackSoldier=pygame.image.load('chess_blackSoldier.png')
img_blackCannon=pygame.image.load('chess_blackCannon.png')
img_blackCar=pygame.image.load('chess_blackCar.png')
img_blackHorse=pygame.image.load('chess_blackHorse.png')
img_blackElephant=pygame.image.load('chess_blackElephant.png')
img_blackAttendant=pygame.image.load('chess_blackAttendant.png')
img_general=pygame.image.load('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],1elif chess1==black_chess:pos,index=[3,4],-1if 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_posreturn [current_pos,2]current_pos=next_posreturn [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_posreturn [current_pos,2]current_pos=next_posreturn [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_posreturn [current_pos,2]current_pos=next_posreturn [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_posreturn [current_pos,2]current_pos=next_posreturn [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_posif a[1]>b[1]:a,b=b,afor i in range(a[1]+1,b[1]):if [a[0],i] in black_chess+red_chess:return 0for i in range(len(chess2)):if chess2[i]==next_pos:chess2[i]=[-1,-1]current_pos=next_posreturn [current_pos,2]current_pos=next_posreturn [current_pos,1]elif next_pos not in chess1 and current_pos[1]==next_pos[1]:a,b=current_pos,next_posif a[0]>b[0]:a,b=b,afor i in range(a[0]+1,b[0]):if [i,a[1]] in black_chess+red_chess:return 0for i in range(len(chess2)):if chess2[i]==next_pos:chess2[i]=[-1,-1]current_pos=next_posreturn [current_pos,2]current_pos=next_posreturn [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=0a,b=current_pos,next_posif a[1]>b[1]:a,b=b,afor i in range(a[1]+1,b[1]):if [a[0],i] in black_chess+red_chess:num+=1if num==1:for i in range(len(chess2)):if chess2[i]==next_pos:chess2[i]=[-1,-1]current_pos=next_posreturn [current_pos,2]return 0elif num==0:current_pos=next_posreturn [current_pos,1]else:return 0elif next_pos not in chess1 and current_pos[1]==next_pos[1]:num=0a,b=current_pos,next_posif a[0]>b[0]:a,b=b,afor i in range(a[0]+1,b[0]):if [i,a[1]] in black_chess+red_chess:num+=1if num==1:for i in range(len(chess2)):if chess2[i]==next_pos:chess2[i]=[-1,-1]current_pos=next_posreturn [current_pos,2]return 0elif num==0:current_pos=next_posreturn [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_posreturn [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_posreturn [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_posreturn [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_posreturn [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_posreturn [current_pos,1]else:for i in range(len(chess2)):if chess2[i]==next_pos:chess2[i]=[-1,-1]current_pos=next_posreturn [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_posreturn [current_pos,1]else:for i in range(len(chess2)):if chess2[i]==next_pos:chess2[i]=[-1,-1]current_pos=next_posreturn [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=0if 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=1breakif flag==0:return 0if 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_posreturn [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_posreturn [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_posreturn [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_posreturn [current_pos,2]#棋子移动
def move(chess1,chess2,next_pos):x=0if 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 xwhite=(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 1elif black_chess[11]==[-1,-1]:draw_text('红方胜利',225,525,15)return 1if __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=0while 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_chesselif chess_kind==1:chess1,chess2=black_chess,red_chessfor 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=Falsewhile 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=Truebreakif flag==True:breakprogress.append(move(chess1,chess2,next_pos))if progress[-1]!=None and progress[-1]!=0:if chess_kind==0:chess_kind=1elif chess_kind==1:chess_kind=0if 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

以上就是代码啦,做的有一些毛手毛脚,若有好建议,请私信我哦~

哦对了,想起图片还没有给大家呢,以下为图片

好啦,图片也上传啦

!!!提醒!!!

图片网址记得修改哟(^U^)ノ~YO

好了,今天的文章就到这里就结束了,如果还有什么问题或想法,欢迎你的私信,等你来哟~

play chess象棋相关推荐

  1. Silverlight+WCF 新手实例 象棋 棋子(三)

    2019独角兽企业重金招聘Python工程师标准>>> 棋盘上有棋子,棋子应该还有一些属性,按原始冲动新建一个棋子类. 上google翻译下棋子E文叫什么,查到了叫Chessman, ...

  2. Silverlight+WCF 新手实例 象棋 主界面-事件区-返回退出(三十三)

    在线演示地址:Silverlight+WCF 新手实例 象棋 在线演示 还是那张图: 本节实现返回大厅和退出系统: 一:返回大厅,其实很简单的说,就是转向房间列表了. 可是,转向前也有很多事情要处理的 ...

  3. html5的canvas实现中国象棋

    html5的canvas实现中国象棋 最近有了一个写中国象棋程序的想法,就根据canvas实现了一下.下面是最终效果: 首先,分析页面布局.主要的是有一个显示图像的区域,左下角是显示当前选中的棋子的& ...

  4. Silverlight+WCF 新手实例 象棋 主界面-棋谱-回放-结局(四十)

    在线演示地址: Silverlight+WCF 新手实例 象棋 在线演示 在Silverlight+WCF 新手实例 象棋 主界面-棋谱-回放(三十九)中,我们实现了用户的棋谱回放,在文章的下面,我们 ...

  5. Win7封装无损廋身清单

    整理了一下,大致如下.清理不会伤及系统功能.娱乐性的东西建议删除,因为这些不是功能性的,包括示例视频.示例音乐和一些主题图片以及一些系统自带的游戏.另外一些属于安装过程中产生的,重装封装不需要这些文件 ...

  6. 红心大战c语言程序设计,Win 7系统安全优化、瘦身攻略(2)

    以下 这些删不删看你了 C:/Program Files/Microsoft Games/Chess 象棋高手(30.3M) C:/Program Files/Microsoft Games/Free ...

  7. 再次安装fedora23的一些遗留问题的解决

    当你习惯了某个版本后, 就不想再更换了. 安装fedora23的磁盘空间 获得? 在安装新的fedora23 的时候, 原来的磁盘没有清空, 于是 就 have not enough free apa ...

  8. 英语四级口语资料整理——自我介绍篇

    英语四级口语资料整理 自我介绍 模板 谈运动 谈工作 谈性格 谈爱好 拓展词汇 专业课程 性格态度 业余爱好 自我介绍 出门在外,自我介绍肯定少不了.一个好的自我介绍能给对方增加不少印象分,下面一起来 ...

  9. Windows 7 封装篇(一)【母盘定制】[手动制作]定制合适的系统母盘

    Windows 7 封装篇(一)[母盘定制][手动制作]定制合适的系统母盘 http://www.win10u.com/article/html/10.html Windows 7 封装篇(一)[母盘 ...

最新文章

  1. 【Vue】IView之table组件化学习(二)
  2. 问题解决-Failed to resolve: com.android.support.constraint:constraint-layout:1.0.0-alpha7
  3. python培训Day1 随笔
  4. 分享3一个博客HTML5模板
  5. springboot 接受数组对象_SpringBoot+RabbitMQ 方式收发消息
  6. 重温SQL——行转列,列转行
  7. IP地址分类及CIDR划分方法
  8. [mybatis]log4j
  9. leetcode 227. 基本计算器 II(栈)
  10. 英语笔记:词组句子:0712
  11. 基于R语言绘制BBC风格图表
  12. “一个人会不会一直穷下去”“先看看他关注的公众号”
  13. 中国人工智能学会通讯——融合经济学原理的个性化推荐
  14. 第十三章 确定性策略梯度(Deterministic Policy Gradient Algorithms,DPG)-强化学习理论学习与代码实现(强化学习导论第二版)
  15. 对lua 实现面向对象的理解
  16. c++如何生成一个不能被继承的类
  17. 使用Python face_recognition 人脸识别 - 12 人脸图片1-N比对
  18. 【技术邻】FloEFD热仿真分析之结果处理
  19. 线性回归之最小二乘法公式推导和原理介绍
  20. 深度学习---之显存单位,KiB,MiB与MB区别

热门文章

  1. vue拖拽排序(原创组件)
  2. Linux进阶(10)--防火墙策略优化
  3. prometheus 监控mysql数据库
  4. java获取局域网IP
  5. 文件上传(transferTo)
  6. PS(CS6)制作gif动画 生日蛋糕上燃烧的蜡烛效果
  7. GNS3使用及ip地址规划
  8. 关于计算机网络安全知识,关于电脑安全上网的10个基础常识
  9. WebRTC:P2P 连接过程完全解析
  10. 【详解】计算机视觉之目标分割