【Python游戏开发】史上最有趣的一款象棋代码,真是太好玩了,这些人都说它比国际象棋更好玩,不服你往下看?(敲赞啊~)

导语

一直以来,中国象棋都是中华民族的一种象征,当然也是人们最为喜感的一种

娱乐方式。在若干年前,人们都习惯于约上自己的棋友,来一种激战。可是,

科技改变人类,也改版了人们的生活方式。现如今,越来越多的玩家开始偏向

于下载中国象棋游戏,这样一来,无论何时何地,只要打开手机或者电脑,就

可以轻松游戏了。

🚀 **文章源码免费获取 :为了感谢每一个关注我的小可爱💓``   ``每篇文章的项目源码都是无偿分享滴💓👇👇记得添加我的``   ``学习交流Python群(712623775)自取滴!

哈喽。我是你们的木木子同学,今天小编给大家用代码写一款中国象棋。

中国象棋是一种起源于中国的古老而智慧的棋类游戏,有着悠久的历史和广泛

的群众基础。中国象棋使用方形格状棋盘,圆形棋子共有32个,红黑二色各

有16个棋子,摆放和活动在交叉点上。中国象棋的规则简单易懂,但是变化

无穷,富有趣味和挑战性。

木子玩了一天,这个游戏的对战水平很高,自己跟自己下?o(╯□╰)o智商有限

啊,快来个人跟我一起下!

正文

一、游戏介绍

中国象棋游戏是一款怡神益智有益身心的游戏。象棋集文化、科学、艺术、竞

技于一身,不但可以开发智力,启迪思维,锻炼辨证分析能力和培养顽强的意

志,而且可以修心养性,陶冶情操,丰富文化生活,趣味性极强。

棋盘桌上尽风流,厮杀几曾休?妙手连珠,滴水不漏,谈笑写春秋。棋子红黑

两分明,却在混浊中。阴风阵阵,杀气重重,逐鹿谁为雄?

二、游戏规则

游戏规则太长了,跟平常现实中旗子的走向一样的,这里就不给大家一个一个

解说了。大家可以自己直接百度一下就行。

三、环境准备

1)运行环境

开发环境:Python3、Pycharm社区版、Pygame,tkinter部分自带的模块

安装Python即可使用。

相对应的安装包/安装教程/激活码/使用教程/学习资料/工具插件 可以直接找

我厚台获取 。

2)模块安装

第三方库的安装方式如下:

 `一般安装:pip install +模块名``镜像源安装:pip install -i https://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)游戏中

总结

下棋的小伙伴都会吧,不会的也可以学习一下中国传统文化哦~

自己跟自己下棋精力有限啊~23333哈哈哈,好啦,今天就先介绍到这里了,

感兴趣的小伙伴可以拿代码去试试啦。

点击下方安全链接前往获取

CSDN大礼包:《Python入门&进阶学习资源包》免费分享

👉Python实战案例👈

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

图片

图片

👉Python书籍和视频合集👈

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

图片

👉Python副业创收路线👈

图片

这些资料都是非常不错的,朋友们如果有需要《Python学习路线&学习资料》,点击下方安全链接前往获取

CSDN大礼包:《Python入门&进阶学习资源包》免费分享

本文转自网络,如有侵权,请联系删除。

  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值