python圖形工具turtle繪制國際象棋棋盤
更新時間:2019年05月23日 09:40:36 作者:周作業(yè)
這篇文章主要為大家詳細(xì)介紹了python圖形工具turtle繪制國際象棋棋盤,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python圖形工具turtle繪制國際象棋棋盤的具體代碼,供大家參考,具體內(nèi)容如下
#編寫程序繪制一個國際象棋的棋盤
import turtle
turtle.speed(30)
turtle.penup()
off = True
for y in range(-40, 30 + 1, 10):
for x in range(-40, 30 + 1, 10):
if off:
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
turtle.color("black")
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.end_fill()
turtle.penup()
else:
turtle.goto(x, y)
turtle.pendown()
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.penup()
off = bool(int(off) - 1)
off = bool(int(off) - 1)
turtle.hideturtle()
turtle.done()
通過函數(shù)的重用優(yōu)化代碼:
先建立一個UsefulTurtleFunctions.py 的文件夾
import turtle #Draw a square def drawSquare(): turtle.pendown() turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.penup()
再在test中調(diào)用它
#編寫程序繪制一個國際象棋的棋盤
import turtle
from UsefulTurtleFunctions import *
turtle.speed(30)
turtle.penup()
off = True
for y in range(-40, 30 + 1, 10):
for x in range(-40, 30 + 1, 10):
if off:
turtle.goto(x, y)
turtle.begin_fill()
turtle.color("black")
drawSquare()
turtle.end_fill()
turtle.penup()
else:
turtle.goto(x, y)
drawSquare()
off = bool(int(off) - 1)
off = bool(int(off) - 1)
turtle.hideturtle()
turtle.done()
最后結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程實現(xiàn)微信企業(yè)號文本消息推送功能示例
這篇文章主要介紹了Python編程實現(xiàn)微信企業(yè)號文本消息推送功能,結(jié)合實例形式分析了Python微信企業(yè)號文本消息推送接口的調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Python無法安裝包的一種解決(Requirement already satisfied問題)
這篇文章主要介紹了Python無法安裝包的一種解決(Requirement already satisfied問題),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
使用Python創(chuàng)建一個隨機(jī)密碼生成器
密碼安全是當(dāng)前數(shù)字時代的一個重要議題,在保護(hù)個人信息和賬戶安全方面,安全且可靠的密碼是至關(guān)重要的,本文將帶您逐步了解如何使用Python創(chuàng)建一個隨機(jī)密碼生成器,以生成高強(qiáng)度、難以猜測的密碼,需要的朋友可以參考下2024-01-01
Pandas 重塑(stack)和軸向旋轉(zhuǎn)(pivot)的實現(xiàn)
這篇文章主要介紹了Pandas 重塑(stack)和軸向旋轉(zhuǎn)(pivot)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

