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

# -*- coding: utf-8 -*-
# -*- 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('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],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]:a,b=j_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: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

棋盘图片:
棋子图片:














运行效果:

python 中国象棋单机版with pygame相关推荐

  1. python中国象棋github_GitHub - bupticybee/elephantfish: elephantfish: 一个只有124行的中国象棋引擎...

    介绍 elephantfish 是受到 sunfish 启发而撰写的纯python的中国象棋引擎, 整个象棋引擎核心代码只有124行(见compressed.py),棋力方面我仅进行过其与象棋小巫师傻 ...

  2. python中国象棋github_GitHub - linbirg/icyChessZero: 中国象棋alpha zero程序

    icyChessZero 中国象棋alpha zero 这个项目受到alpha go zero的启发,旨在训练一个中等人类水平或高于中等人类水平的深度神经网络,来完成下中国象棋的任务.目前这个项目仍在 ...

  3. python中国象棋github_GitHub 上最火的 Python 开源项目zz

    Google 的 TensorFlow 是最流行的开源 AI 库之一.它的高计算效率,丰富的开发资源使它被企业和个人开发者广泛采用.TensorFlow 是一个采用数据流图,用于数值计算的开源软件库. ...

  4. python中国象棋人机大战_还记得浪潮杯首届象棋人机大战吗?五位高手被电脑18回合打败了...

    [DhtmlXQ] [DhtmlXQ_init]500,350[/DhtmlXQ_init] [DhtmlXQ_title]浪潮杯人机大战,电脑vs五位高手[/DhtmlXQ_title] [Dhtm ...

  5. python中国象棋 ai_人工智能:python实现-ai简介

    先发英文原版,然后和网友们一起翻译. Introduction to Artificial Intelligence In this chapter, we are going to discuss ...

  6. 我的中国象棋游戏程序单机版

    花了十天时间(2006-8-21到2006-8-31日) ,开发了一个拥有完全自主知识产权的中国象棋单机版游戏软件. 该软件在VS2003环境下开发,使用VB.NET语言.关键技术:GDI+.开发并应 ...

  7. java实现中国象棋 源代码

    java实现中国象棋 在网上找了很久中国象棋实现的源代码,终于找到了,下面就是源代码. /**中国象棋Java版V3.0*源文件:Chess.java*添加功能:实现了当前棋局的保存*/import ...

  8. QT项目三:中国象棋

    1,简介 QT实现中国象棋单机版,本来以为会花费不少时间的.由于之前2个游戏项目的练手,这个主要只在走棋算法部分花了些时间研究,最后一共2个晚上完成. 由于只是QT学习,没有考虑更多复杂功能,比如人机 ...

  9. python·pygame小游戏--中国象棋(原码附上,免费下载)

    大家好我是小豪,今天给大家带来的是pygame小游戏-中国象棋 因为看到博客上面很多上传了的中国象棋py文件,都是收费的.所以我大胆的上传个免费的-已经把原码上传了,感兴趣的可以去下载. pygame ...

最新文章

  1. oracle sql profile
  2. PHP中ini_set和ini_get函数用法简介
  3. 与服务器交互的分页组件PageComponent
  4. libiconv2.dll
  5. 在WINCE5.0开始菜单中添加应用程序
  6. Centos7常用命令[系统的关机、重启以及登出]
  7. 基于Elasticsearch实现搜索推荐
  8. 《Head First设计模式》第四章笔记 工厂模式
  9. python自动汇总excel_RPA手把手:Python轻松实现EXCEL自动化
  10. html字体置顶,2020年应使用的3种CSS字体属性
  11. leetcode python3 简单题125. Valid Palindrome
  12. 面试遇到“一问三不知”的,真替他老东家捏把汗
  13. android多线程网络通信
  14. java云购_ycyg: 源创元购,一元云购java springmvc版本 云购商城 开源一元云购 开源java商城...
  15. 数字化营销怎么做?如何做好数字化营销?
  16. Python在图片上添加文字
  17. php校花评比排名,2018大学校花排名出炉,清华高冷,人大“小郑爽”,你pick谁?...
  18. 多级放大电路的耦合方式
  19. Excel如何批量在空白单元格录入相同内容
  20. npm ERR,fatal: unable to access ‘https://github.com/nhn/raphael.git/‘: OpenSSL SSL_read: Connection

热门文章

  1. 2021年中国政府引导基金设立数量、目标规模、认缴规模及分布[图]
  2. 运动学自行车模型和动力学自行车模型
  3. sonarqube企业版最新版pojie 含中文汉化
  4. thinkphp执行定时任务定时任务
  5. 华为机试 - 构成正方形的数量
  6. Canny边缘检测图像 findContours
  7. Unifier培训: 系列讲解26 : 项目级的业务流程--变更单(总承包商业务)
  8. sizeof大合集(一)
  9. J1939故障码诊断说明
  10. 一家倒闭电商创始人写的5千字检讨书