基于Python實現有趣的象棋游戲
導語
一直以來,中國象棋都是中華民族的一種象征,當然也是人們最為喜感的一種娛樂方式。
在若干年前,人們都習慣于約上自己的棋友,來一種激戰(zhàn)。可是,科技改變人類,也改版了人們的生活方式?,F如今,越來越多的玩家開始偏向于下載中國象棋游戲,這樣一來,無論何時何地,只要打開手機或者電腦,就可以輕松游戲了。
哈嘍。我是你們的木木子同學,今天小編給大家用代碼寫一款中國象棋。
中國象棋是一種起源于中國的古老而智慧的棋類游戲,有著悠久的歷史和廣泛的群眾基礎。中國象棋使用方形格狀棋盤,圓形棋子共有32個,紅黑二色各有16個棋子,擺放和活動在交叉點上。中國象棋的規(guī)則簡單易懂,但是變化無窮,富有趣味和挑戰(zhàn)性。
木子玩了一天,這個游戲的對戰(zhàn)水平很高,自己跟自己下?o(╯□╰)o智商有限啊,快來個人跟我一起下!
一、游戲介紹
中國象棋游戲是一款怡神益智有益身心的游戲。象棋集文化、科學、藝術、競技于一身,不但可以開發(fā)智力,啟迪思維,鍛煉辨證分析能力和培養(yǎng)頑強的意志,而且可以修心養(yǎng)性,陶冶情操,豐富文化生活,趣味性極強。
棋盤桌上盡風流,廝殺幾曾休?妙手連珠,滴水不漏,談笑寫春秋。棋子紅黑兩分明,卻在混濁中。陰風陣陣,殺氣重重,逐鹿誰為雄?
二、游戲規(guī)則
游戲規(guī)則太長了,跟平?,F實中旗子的走向一樣的,這里就不給大家一個一個解說了。大家可以自己直接百度一下就行。
三、環(huán)境準備
1)運行環(huán)境
開發(fā)環(huán)境:Python3、Pycharm社區(qū)版、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)游戲中

到此這篇關于基于Python實現有趣的象棋游戲的文章就介紹到這了,更多相關Python象棋內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python3的unicode編碼轉換成中文的問題及解決方案
這篇文章主要介紹了Python3的unicode編碼轉換成中文的問題及解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12
python內置函數compile(),complex()的使用
這篇文章主要為大家詳細介紹了python內置函數compile(),complex()的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06

