python圖形工具turtle繪制國際象棋棋盤
更新時間:2019年05月23日 09:40:36 作者:周作業(yè)
這篇文章主要為大家詳細介紹了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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python報錯unexpected?indent的解決辦法
這篇文章主要給大家介紹了關(guān)于python報錯unexpected?indent的解決辦法,在python中出現(xiàn)"Unexpected indent"可能是代碼的縮進出現(xiàn)問題,需要的朋友可以參考下2023-06-06
Python判斷字符串是否包含特定子字符串的多種方法(7種方法)
我們經(jīng)常會遇這樣一個需求判斷字符串中是否包含某個關(guān)鍵詞,也就是特定的子字符串,接下來通過本文給大家分享Python判斷字符串是否包含特定子字符串的多種方法(7種方法),需要的朋友可以參考下2023-03-03
詳談Python中列表list,元祖tuple和numpy中的array區(qū)別
下面小編就為大家分享一篇詳談Python中列表list,元祖tuple和numpy中的array區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04

