欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python實(shí)現(xiàn)有趣的象棋游戲

 更新時(shí)間:2023年03月06日 08:26:47   作者:木木子學(xué)python  
一直以來(lái),中國(guó)象棋都是中華民族的一種象征,當(dāng)然也是人們最為喜感的一種娛樂(lè)方式。這篇文章主要介紹了如何基于Python實(shí)現(xiàn)有趣的象棋游戲,感興趣的可以了解一下

導(dǎo)語(yǔ)

一直以來(lái),中國(guó)象棋都是中華民族的一種象征,當(dāng)然也是人們最為喜感的一種娛樂(lè)方式。

在若干年前,人們都習(xí)慣于約上自己的棋友,來(lái)一種激戰(zhàn)??墒?,科技改變?nèi)祟?,也改版了人們的生活方式?,F(xiàn)如今,越來(lái)越多的玩家開(kāi)始偏向于下載中國(guó)象棋游戲,這樣一來(lái),無(wú)論何時(shí)何地,只要打開(kāi)手機(jī)或者電腦,就可以輕松游戲了。

哈嘍。我是你們的木木子同學(xué),今天小編給大家用代碼寫一款中國(guó)象棋。

中國(guó)象棋是一種起源于中國(guó)的古老而智慧的棋類游戲,有著悠久的歷史和廣泛的群眾基礎(chǔ)。中國(guó)象棋使用方形格狀棋盤,圓形棋子共有32個(gè),紅黑二色各有16個(gè)棋子,擺放和活動(dòng)在交叉點(diǎn)上。中國(guó)象棋的規(guī)則簡(jiǎn)單易懂,但是變化無(wú)窮,富有趣味和挑戰(zhàn)性。

木子玩了一天,這個(gè)游戲的對(duì)戰(zhàn)水平很高,自己跟自己下?o(╯□╰)o智商有限啊,快來(lái)個(gè)人跟我一起下!

一、游戲介紹

中國(guó)象棋游戲是一款怡神益智有益身心的游戲。象棋集文化、科學(xué)、藝術(shù)、競(jìng)技于一身,不但可以開(kāi)發(fā)智力,啟迪思維,鍛煉辨證分析能力和培養(yǎng)頑強(qiáng)的意志,而且可以修心養(yǎng)性,陶冶情操,豐富文化生活,趣味性極強(qiáng)。

棋盤桌上盡風(fēng)流,廝殺幾曾休?妙手連珠,滴水不漏,談笑寫春秋。棋子紅黑兩分明,卻在混濁中。陰風(fēng)陣陣,殺氣重重,逐鹿誰(shuí)為雄?

二、游戲規(guī)則

游戲規(guī)則太長(zhǎng)了,跟平?,F(xiàn)實(shí)中旗子的走向一樣的,這里就不給大家一個(gè)一個(gè)解說(shuō)了。大家可以自己直接百度一下就行。

三、環(huán)境準(zhǔn)備

1)運(yùn)行環(huán)境 

 開(kāi)發(fā)環(huán)境:Python3、Pycharm社區(qū)版、Pygame,tkinter部分自帶的模塊安裝Python即可使用。

 2)模塊安裝 

 第三方庫(kù)的安裝方式如下:

 一般安裝:pip install +模塊名 

 鏡像源安裝:pip install -i pypi.douban.com/simple/+模塊名…

 (還有很多國(guó)內(nèi)鏡像源,這里是豆瓣的用習(xí)慣了,其他鏡像源可以去看下之前文章都有的) 

四、代碼展示

?1)模塊導(dǎo)入

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('中國(guó)象棋')
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('開(kāi)始上一步')
            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('開(kāi)始下一步')
        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('開(kāi)始下一步')
            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)開(kāi)始界面

?2)游戲界面

?3)游戲中

到此這篇關(guān)于基于Python實(shí)現(xiàn)有趣的象棋游戲的文章就介紹到這了,更多相關(guān)Python象棋內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python3的unicode編碼轉(zhuǎn)換成中文的問(wèn)題及解決方案

    Python3的unicode編碼轉(zhuǎn)換成中文的問(wèn)題及解決方案

    這篇文章主要介紹了Python3的unicode編碼轉(zhuǎn)換成中文的問(wèn)題及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • python內(nèi)置函數(shù)compile(),complex()的使用

    python內(nèi)置函數(shù)compile(),complex()的使用

    這篇文章主要為大家詳細(xì)介紹了python內(nèi)置函數(shù)compile(),complex()的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Python configparser模塊操作代碼實(shí)例

    Python configparser模塊操作代碼實(shí)例

    這篇文章主要介紹了Python configparser模塊操作代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 深入學(xué)習(xí)Python可變與不可變對(duì)象操作實(shí)例

    深入學(xué)習(xí)Python可變與不可變對(duì)象操作實(shí)例

    Python中的數(shù)據(jù)類型可以分為可變對(duì)象和不可變對(duì)象,了解它們之間的區(qū)別對(duì)于編寫高效的Python代碼至關(guān)重要,本文將詳細(xì)介紹可變對(duì)象和不可變對(duì)象的概念,以及如何正確地使用它們來(lái)提高代碼的性能和可讀性
    2023-12-12
  • Python實(shí)現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)

    Python實(shí)現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)

    這篇文章主要實(shí)現(xiàn)四種數(shù)據(jù)結(jié)構(gòu),分別是數(shù)組、堆棧、隊(duì)列、鏈表。大家都知道可以用C語(yǔ)言實(shí)現(xiàn)這幾種數(shù)據(jù)結(jié)構(gòu),其實(shí)Python也可以實(shí)現(xiàn),下面跟著小編一起來(lái)學(xué)習(xí)。
    2016-08-08
  • Pytorch加載圖像數(shù)據(jù)集的方法

    Pytorch加載圖像數(shù)據(jù)集的方法

    這篇文章主要介紹了Pytorch加載圖像數(shù)據(jù)集的方法,加載圖像數(shù)據(jù)集(這里以分類為例),通常都需要經(jīng)過(guò)兩個(gè)步驟:定義數(shù)據(jù)集和創(chuàng)建Dataloader數(shù)據(jù)加載器,本文通過(guò)代碼示例和圖文講解的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • Python中IP地址處理IPy模塊的方法

    Python中IP地址處理IPy模塊的方法

    這篇文章主要介紹了Python中IP地址處理IPy模塊的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python中樹(shù)與樹(shù)的表示知識(shí)點(diǎn)總結(jié)

    python中樹(shù)與樹(shù)的表示知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于python中樹(shù)與樹(shù)的表示的相關(guān)知識(shí)點(diǎn),需要的讀者們學(xué)習(xí)下吧。
    2019-09-09
  • Django框架基礎(chǔ)模板標(biāo)簽與filter使用方法詳解

    Django框架基礎(chǔ)模板標(biāo)簽與filter使用方法詳解

    這篇文章主要介紹了Django框架基礎(chǔ)模板標(biāo)簽與filter使用方法,簡(jiǎn)單分析了Django模板基本語(yǔ)法、函數(shù)與自定義filter相關(guān)使用技巧,需要的朋友可以參考下
    2019-07-07
  • 淺析Django接口版本控制

    淺析Django接口版本控制

    一個(gè)項(xiàng)目在升級(jí)迭代的時(shí)候,不會(huì)立馬拋棄舊的版本,甚至?xí)霈F(xiàn)多個(gè)版本共存同時(shí)維護(hù)的情況,因此需要版本控制
    2021-06-06

最新評(píng)論