首页 > 编程语言 >Python pygame实现中国象棋单机版源码

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

时间:2023-07-15 17:44:32浏览次数:40  
标签:中国象棋 单机版 chess1 pos next current 源码 chess chess2

今天给大家带来的是关于Python实战的相关知识,文章围绕着用Python pygame实现中国象棋单机游戏版展开,文中有非常详细的代码示例,需要的朋友可以参考下

# -*- 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
————————————————
版权声明:本文为CSDN博主「夜刺」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/heiyefengdi/article/details/131742137

标签:中国象棋,单机版,chess1,pos,next,current,源码,chess,chess2
From: https://www.cnblogs.com/yeci/p/17556580.html

相关文章

  • 如何定义yum源以及定制本地yum源码
    1、如何指定yum源地址cd/etc/yum.repos.d 可以指定centos、阿里云、163等,可自行百度搜索配置2、如何自定义yum源以centos集群为例制作yum源;当我们采用最小安装的方式安装完Linux系统后,会发现输入常用命令显示没有该命令,一般做法就使用yum命令进行安装;但是yum......
  • 下载k8s源码
    设置GOPATH环境变量goenv-wGO111MODULE=autocd$GOPATHmkdir-psrc/k8s.iocdsrc/k8s.iogitclonehttps://github.com/kubernetes/kubernetes.gitcdkubernetesgitcheckoutrelease-1.15设置GolandFile->Settings->GO->GOPATH勾选UseGOPATHthat's......
  • Windows子系统Ubuntu或虚拟机Ubuntu通过编译源码的方式安装wine8.0.1
    wine源码编译安装下载源码源码链接为:wine源码本文以8.1.1版本为例,下载的源码包为:wine-8.11.tar.xz拷贝包到Ubuntu使用xftp或其他工具,将压缩包拷贝到ubuntu下的home/$username/目录解包tar-Jxfwine-8.11.tar.xz //xz格式的包或tar-xvfyour_tar_file.tar //tar格式......
  • React18内核探秘:手写React高质量源码迈向高阶开发
    第1章课程简介试看1节|8分钟导学介绍课程内容,及你所获得~第2章登高望远,手写源码前的思想准备8节|54分钟建立全局观,为后续在源码中吸取精华做好思想准备,避免就源码而分析源码。第3章原始版-初始化渲染:实现最原始的渲染过程11节|122分钟实现初次渲染的基础逻辑,初步体验......
  • EaselJS 源码分析系列--第一篇
    什么是EaselJS?事儿还得从Flash说起,因为我最早接触的就是Flash,从Flash入行编程的Flash最早的脚本是Actionscript2.0它的1.0我是没用过。Actionscript2.0与Javascript非常像(es3时代的Javascript)后来又推出了完全面向对象的Actionscript3.0而毕业后的我也......
  • centos7上源码编译安装LAMP的多虚拟主机wordpress,discuz,用lamp.sh脚本实现
    环境:centos7.4apr-1.6.3.tar.gzapr-util-1.6.1.tar.gzhttpd-2.4.33.tar.bz2mariadb-10.2.15-linux-x86_64.tar.gzphp-7.1.18.tar.bz2wordpress-4.9.4-zh_CN.tar.gz1安装包:yumgroupinstall"developmenttools"yuminstallpcre-develope......
  • AI智能识别微信小程序源码-带流量主功能
     AI智能识别微信小程序源码带流量主功能。基于腾讯云ocr识别接口做的识别工具(自动识别图片、证件、车牌、身份证等)。 演示地址:www.runruncode.com/wxapp/19459.html  ......
  • 老杜 JavaWeb 讲解(十) —— HttpServletRequest接口源码分析
    (十二)HttpServletRequest接口源码分析对应视频:21-HttpServletRequest接口12.1介绍HttpServletRequest是一个接口,全限定名称:jakarta.servlet.http.HttpServletRequestHttpServletRequest接口是Servlet规范中的一员。HttpServletRequest接口的父接口:ServletRequest......
  • YOLOX目标检测实战:LabVIEW+YOLOX ONNX模型实现推理检测(含源码)
    (文章目录)前言好长一段时间没更博了,没更新博客的这段时间博主都有在努力产出,前段时间好多朋友私信问我说自己的yolov5模型是比较老的版本,使用LabVIEW推理的时候会报错。为各位朋友新老版本都能兼容,博主这段时间做了一个LabVIEWYOLOv5的插件,里面包含了大部分的新旧版本,老版本的......
  • ckeditor粘贴word图片且图片文件自动上传源码
    ​ 如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>......