本篇内容主要讲解“基于Python如何实现有趣的象棋游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基于Python如何实现有趣的象棋游戏”吧!
中国象棋游戏是一款怡神益智有益身心的游戏。象棋集文化、科学、艺术、竞技于一身,不但可以开发智力,启迪思维,锻炼辨证分析能力和培养顽强的意志,而且可以修心养性,陶冶情操,丰富文化生活,趣味性极强。
棋盘桌上尽风流,厮杀几曾休?妙手连珠,滴水不漏,谈笑写春秋。棋子红黑两分明,却在混浊中。阴风阵阵,杀气重重,逐鹿谁为雄?
游戏规则太长了,跟平常现实中旗子的走向一样的,这里就不给大家一个一个解说了。大家可以自己直接百度一下就行。
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('未载入棋谱')
到此,相信大家对“基于Python如何实现有趣的象棋游戏”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。