基于Python实现有趣的象棋游戏
作者:木木子学python 发布时间:2022-06-11 20:37:15
导语
一直以来,中国象棋都是中华民族的一种象征,当然也是人们最为喜感的一种娱乐方式。
在若干年前,人们都习惯于约上自己的棋友,来一种激战。可是,科技改变人类,也改版了人们的生活方式。现如今,越来越多的玩家开始偏向于下载中国象棋游戏,这样一来,无论何时何地,只要打开手机或者电脑,就可以轻松游戏了。
哈喽。我是你们的木木子同学,今天小编给大家用代码写一款中国象棋。
中国象棋是一种起源于中国的古老而智慧的棋类游戏,有着悠久的历史和广泛的群众基础。中国象棋使用方形格状棋盘,圆形棋子共有32个,红黑二色各有16个棋子,摆放和活动在交叉点上。中国象棋的规则简单易懂,但是变化无穷,富有趣味和挑战性。
木子玩了一天,这个游戏的对战水平很高,自己跟自己下?o(╯□╰)o智商有限啊,快来个人跟我一起下!
一、游戏介绍
中国象棋游戏是一款怡神益智有益身心的游戏。象棋集文化、科学、艺术、竞技于一身,不但可以开发智力,启迪思维,锻炼辨证分析能力和培养顽强的意志,而且可以修心养性,陶冶情操,丰富文化生活,趣味性极强。
棋盘桌上尽风流,厮杀几曾休?妙手连珠,滴水不漏,谈笑写春秋。棋子红黑两分明,却在混浊中。阴风阵阵,杀气重重,逐鹿谁为雄?
二、游戏规则
游戏规则太长了,跟平常现实中旗子的走向一样的,这里就不给大家一个一个解说了。大家可以自己直接百度一下就行。
三、环境准备
1)运行环境
开发环境:Python3、Pycharm社区版、Pygame,tkinter部分自带的模块安装Python即可使用。
2)模块安装
第三方库的安装方式如下:
一般安装:pip install +模块名
镜像源安装:pip install -i pypi.douban.com/simple/+模块名…
(还有很多国内镜像源,这里是豆瓣的用习惯了,其他镜像源可以去看下之前文章都有的)
四、代码展示
1)模块导入
import pygame as pg
import sys
import tkinter as tk
from tkinter import *
import tkinter.filedialog,tkinter.messagebox
from threading import Thread
import time,os,re
2)主程序
def gettime():
t=time.strftime('%Y%m%d%H%M%S',time.localtime())
return t
def on_closing():
if tkinter.messagebox.askokcancel("退出", "你确定要退出吗?"):
root.destroy()
os._exit(1)
root = tk.Tk()
embed = tk.Frame(root, width = 900, height = 764) #creates embed frame for pygame window
embed.grid(columnspan = (600), rowspan = 500) # Adds grid
embed.pack(side = LEFT) #packs window to the left
root.title('中国象棋')
root.resizable(False,False)
root.protocol("WM_DELETE_WINDOW", on_closing)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
#棋子65
filename=tkinter.StringVar()
countqipu=0
step=0
def openfile():
global filename
file=tkinter.filedialog.askopenfilename()
filename.set(file)
file_name=os.path.basename(file)
compile=re.compile(r'\d{14}')
content=compile.search(file_name)
if content:
restart()
tkinter.messagebox.showinfo('提示', "载入棋谱成功")
else:
tkinter.messagebox.showerror('提示', "载入棋谱失败")
def writeqipu():
global countqipu
if countqipu==0:
with open('{}'.format(gettime()+'.txt'),'w') as f:
f.write(str(selectlist)+'\n'+str(chesslist)+'\n'+str(value_list)+'\n'+str(ischizi_list)+'\n'+str(death_value))
countqipu+=1
def upstep():
global step,countqipu,running
running=False
if filename.get()!='':
try:
print('开始上一步')
with open(filename.get(),'r') as f:
info=f.readlines()
for i,j in enumerate(info):
info[i]=j.replace('\n','')
select_list=eval(info[0])
chess_list=eval(info[1])
valuelist=eval(info[2])
ischizilist=eval(info[3])
deathvalue_list=eval(info[4])
countqipu+=1
print(info)
if step>0:
selecttuple=select_list[step-1]
chesstuple=chess_list[step-1]
chessvalue=valuelist[step-1]
czpd=ischizilist[step-1]
deathvalue=deathvalue_list[step-1]
if czpd:
list[chesstuple[0]][chesstuple[1]]=deathvalue
list[selecttuple[0]][selecttuple[1]] = chessvalue
else:
list[chesstuple[0]][chesstuple[1]]=0
list[selecttuple[0]][selecttuple[1]] = chessvalue
step -= 1
except Exception as e:
print(e)
else:
print('未载入棋谱')
def downstep():
global step,countqipu,running
running=False
if filename.get() != '':
print('开始下一步')
try:
with open(filename.get(),'r') as f:
info=f.readlines()
for i,j in enumerate(info):
info[i]=j.replace('\n','')
select_list=eval(info[0])
chess_list=eval(info[1])
valuelist=eval(info[2])
print(info)
countqipu+=1
print(step)
# if running:
step += 1
if step>0:
selecttuple=select_list[step-1]
chesstuple=chess_list[step-1]
chessvalue=valuelist[step-1]
print(chessvalue,chesstuple[0],chesstuple[1])
list[selecttuple[0]][selecttuple[1]]=0
list[chesstuple[0]][chesstuple[1]]=chessvalue
except Exception as e:
print(e)
else:
print('未载入棋谱')
def auto():
global step,countqipu,running
running=False
button2.config(state=tkinter.DISABLED)
button3.config(state=tkinter.DISABLED)
if filename.get() != '':
# if running:
try:
print('开始下一步')
with open(filename.get(),'r') as f:
info=f.readlines()
for i,j in enumerate(info):
info[i]=j.replace('\n','')
select_list=eval(info[0])
chess_list=eval(info[1])
valuelist=eval(info[2])
countqipu+=1
step += 1
if step>0:
selecttuple=select_list[step-1]
chesstuple=chess_list[step-1]
chessvalue=valuelist[step-1]
print(chessvalue,chesstuple[0],chesstuple[1])
list[selecttuple[0]][selecttuple[1]]=0
list[chesstuple[0]][chesstuple[1]]=chessvalue
button4.after(1000, auto)
except Exception as e:
print(e)
else:
print('未载入棋谱')
五、效果展示
1)开始界面
2)游戏界面
3)游戏中
来源:https://juejin.cn/post/7206302271079694396
猜你喜欢
- 本文实例讲述了Python闭包和装饰器用法。分享给大家供大家参考,具体如下:Python的装饰器的英文名叫Decorator,作用是完成对一
- 需求查询某个字段的时候需要给一个字段同样的值。这个值你可以写死,也可以从数据库获取1、写死值SELECT mfr_id AS mfrId,
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 18 - Classes part IClass(
- 字符型图片验证码识别完整过程及Python实现的博主,我的大部分知识点都是从他那里学来的。想要识别验证码,收集足够多的样本后,首先要做的就是
- 首先要介绍的是 Python Imaging Library,使用方法如下:from PIL import Imagefrom PIL.Ex
- 当然,5.6的GUID功能的出现也带来了multi-master的无限可能,不过这个已经是题外话了。 本文主要介绍一种非实时的适用于各版本M
- 本文主要介绍了OpenCV 图像对比度,具有一定的参考价值,感兴趣的可以了解一下实现原理图像对比度指的是一幅图像中明暗区域最亮的白和最暗的黑
- M2广义货币供应量:流通于银行体系之外的现金加上企业存款、居民储蓄存款以及其他存款,它包括了一切可能成为现实购买力的货币形式,通常反映的是社
- 每次在操作数据库的时候最烦的就是根据表单提交的内容写sql语句,特别是字段比较多的时候很麻烦,动不动就容易写错。所以我就写了下面的生成sql
- 今天一个域名查询系统出现故障,该系统是用的ASP调用XMLHTTP获取whois库的数据,具体错误如下: msxml3.dll 错
- 如果备份的数据库有2个文件,分别是.LDF 和 .MDF,打开企业管理器,在实例上右击---所有任务--附加数据库,然后选择那个.MDF文件
- mysql-connector-java与mysql版本的对应我们知道版本更新经常带来的问题就是兼容性问题。在编程过程中版本的错误选择很可能
- 目录1 摘要2 概述2.1 什么是并行计算?2.2 为什么要并行计算?2.3 谁都在使用并行计算?科学界和工程界:工业界和商业界:全球应用:
- Javascript 实现计算器:系列文章:JS 实现计算器详解及实例代码(一)Javascript 实现计算器时间功能详解及实例(二)小型
- 1、编译原理在传统编译语言的流程中,程序中的一段代码执行前会经历三个步骤。统称为“编译”。词法分析 将代码字符串分解成有意义的代码块,这些代
- 主题众所周知,django.forms极其强大,不少的框架也借鉴了这个模式,如Scrapy。在表单验证时,django.forms是一绝,也
- Flask的模板功能是基于Jinja2模板引擎来实现的。模板文件存放在当前目前下的子目录templates(一定要使用这个名字)下。main
- 如下所示:distances = np.sqrt(np.sum(np.asarray(airportPosition - x_vals)**
- 需求场景,五百个文件里面,选取50个指定文件,放入新的文件夹里。1、准备工作1 安装python环境可能会报错,并且pip install
- <%@ transaction = required %><%response.b