python實(shí)現(xiàn)象棋游戲
更新時(shí)間:2022年05月10日 11:39:10 作者:Moyan_0307
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)象棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python實(shí)現(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python+Tinify實(shí)現(xiàn)高效批量壓縮圖片
這篇文章主要為大家詳細(xì)介紹了Python如何利用Tinify實(shí)現(xiàn)高效批量壓縮圖片功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2025-02-02
解決TensorFlow模型恢復(fù)報(bào)錯(cuò)的問題
今天小編就為大家分享一篇解決TensorFlow模型恢復(fù)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python爬取數(shù)據(jù)保存為Json格式的代碼示例
今天小編就為大家分享一篇關(guān)于Python爬取數(shù)據(jù)保存為Json格式的代碼示例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
一文帶你解密Python迭代器的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Python中迭代器的實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下2022-12-12
Python 中導(dǎo)入csv數(shù)據(jù)的三種方法
這篇文章主要介紹了Python 中導(dǎo)入csv數(shù)據(jù)的三種方法,內(nèi)容比較簡單,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11

