python實現(xiàn)象棋游戲
本文實例為大家分享了python實現(xiàn)象棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
import math from turtle import * speed(0) ? #調(diào)整畫的速度 1-10,數(shù)字越大,速度越快 width=50 hideturtle() ? ? #隱藏畫筆的形狀a #繪制棋盤 for i in range(5): ? ? forward(8*width) ? ? penup() ? ? goto(0,(i+1)*width) ? ? pendown() left(90) for i in range(9): ? ? penup() ? ? goto(i*width,0) ? ? pendown() ? ? forward(4*width) for i in range(9): ? ? penup() ? ? goto(i*width,-width) ? ? pendown() ? ? forward(-4*width) left(270) for i in range(5): ? ? penup() ? ? goto(0,-width*(i+1)) ? ? pendown() ? ? forward(8*width) penup() goto(-10,4*width+10) pendown() right(90) for i in range(2): ? ? forward(9*width+20) ? ? left(90) ? ? forward(8*width+20) ? ? left(90) #田線 penup() goto(3*width,4*width) pendown() left(45) fd(2*math.sqrt(5000)) penup() goto(3*width,2*width) pendown() left(90) fd(2*math.sqrt(5000)) penup() goto(3*width,-5*width) pendown() fd(2*math.sqrt(5000)) penup() goto(3*width,-3*width) pendown() right(90) fd(2*math.sqrt(5000)) #繪制炮兵標(biāo)記 def fun(x,y): ? ? penup() ? ? home() ? ? goto(x*width,y*width+3) ? ? fd(-6) ? ? pendown() ? ? for i in range(4): ? ? ? ? fd(3) ? ? ? ? left(90) ? ? ? ? fd(3) ? ? ? ? penup() ? ? ? ? right(90) ? ? ? ? fd(6) ? ? ? ? pendown() ? ? ? ? right(90) for i in range(5): ? ? fun(i*2,1) for i in range(5): ? ? fun(i*2,-2) fun(1,2) fun(7,2) fun(1,-3) fun(7,-3) def write_chees(str,color1): ?#棋字 ? ? color(color1) ? ? write(str,font=('隸書',width//2,'normal')) def drawcircle(radius): ? #畫圓 ? ? pensize(3) ? ? begin_fill() ? ? fillcolor('white') ? ? circle(radius) ? ? end_fill() def chees(x,y,str,color): ?#繪制棋子 ? ? penup() ? ? home() ? ? goto(x*width,y*width) ? ? right(90) ? ? fd(1/3*width) ? ? left(90) ? ? pendown() ? ? pencolor(color) ? ? drawcircle(1/3*width) ? ? penup() ? ? fd(-1/3*width) ? ? pendown() ? ? write_chees(str,color) red='red' black='black' #紅棋子 for i in range(5): ? ? chees(i*2,1,'兵','red') chees(1,2,'炮',red) chees(7,2,'炮',red) chees(0,4,'車',red) chees(1,4,'馬',red) chees(2,4,'相',red) chees(3,4,'仕',red) chees(4,4,'帥',red) chees(5,4,'仕',red) chees(6,4,'相',red) chees(7,4,'馬',red) chees(8,4,'車',red) #黑棋子 for i in range(5): ? ? chees(i*2,-2,'卒','black') chees(1,-3,'炮',black) chees(7,-3,'炮',black) chees(0,-5,'車',black) chees(1,-5,'馬',black) chees(2,-5,'象',black) chees(3,-5,'士',black) chees(4,-5,'將',black) chees(5,-5,'士',black) chees(6,-5,'象',black) chees(7,-5,'馬',black) chees(8,-5,'車',black) done()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python利用 SVM 算法實現(xiàn)識別手寫數(shù)字
支持向量機(jī) (Support Vector Machine, SVM) 是一種監(jiān)督學(xué)習(xí)技術(shù),它通過根據(jù)指定的類對訓(xùn)練數(shù)據(jù)進(jìn)行最佳分離,從而在高維空間中構(gòu)建一個或一組超平面。本文將介紹通過SVM算法實現(xiàn)手寫數(shù)字的識別,需要的可以了解一下2021-12-12python使用numpy中的size()函數(shù)實例用法詳解
在本篇文章里小編給整理的是一篇關(guān)于python使用numpy中的size()函數(shù)實例用法詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總
這篇文章主要介紹了Python中的Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法,創(chuàng)建Dataframe主要是使用pandas中的DataFrame函數(shù),其核心就是第一個參數(shù):data,傳入原始數(shù)據(jù),因此我們可以據(jù)此給出六種創(chuàng)建Dataframe的方法,需要的朋友可以參考下2023-05-05Python實現(xiàn)掃描局域網(wǎng)活動ip(掃描在線電腦)
這篇文章主要介紹了Python實現(xiàn)掃描局域網(wǎng)活動ip(掃描在線電腦),本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-04-04Python使用Beautiful Soup爬取豆瓣音樂排行榜過程解析
這篇文章主要介紹了Python使用Beautiful Soup爬取網(wǎng)頁過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08Python爬蟲beautifulsoup4常用的解析方法總結(jié)
今天小編就為大家分享一篇關(guān)于Python爬蟲beautifulsoup4常用的解析方法總結(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02python實現(xiàn)銀行實戰(zhàn)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)銀行實戰(zhàn)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02