python圖形工具turtle繪制國(guó)際象棋棋盤
本文實(shí)例為大家分享了python圖形工具turtle繪制國(guó)際象棋棋盤的具體代碼,供大家參考,具體內(nèi)容如下
#編寫程序繪制一個(gè)國(guó)際象棋的棋盤 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)化代碼:
先建立一個(gè)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)用它
#編寫程序繪制一個(gè)國(guó)際象棋的棋盤 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é)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程實(shí)現(xiàn)微信企業(yè)號(hào)文本消息推送功能示例
這篇文章主要介紹了Python編程實(shí)現(xiàn)微信企業(yè)號(hào)文本消息推送功能,結(jié)合實(shí)例形式分析了Python微信企業(yè)號(hào)文本消息推送接口的調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2017-08-08python關(guān)鍵字傳遞參數(shù)實(shí)例分析
在本篇文章里小編給大家整理的是一篇關(guān)于python關(guān)鍵字傳遞參數(shù)實(shí)例分析內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2021-06-06Python無(wú)法安裝包的一種解決(Requirement already satisfied問題)
這篇文章主要介紹了Python無(wú)法安裝包的一種解決(Requirement already satisfied問題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08使用Python創(chuàng)建一個(gè)隨機(jī)密碼生成器
密碼安全是當(dāng)前數(shù)字時(shí)代的一個(gè)重要議題,在保護(hù)個(gè)人信息和賬戶安全方面,安全且可靠的密碼是至關(guān)重要的,本文將帶您逐步了解如何使用Python創(chuàng)建一個(gè)隨機(jī)密碼生成器,以生成高強(qiáng)度、難以猜測(cè)的密碼,需要的朋友可以參考下2024-01-01Pandas 重塑(stack)和軸向旋轉(zhuǎn)(pivot)的實(shí)現(xiàn)
這篇文章主要介紹了Pandas 重塑(stack)和軸向旋轉(zhuǎn)(pivot)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07用Python獲取智慧校園每日課表并自動(dòng)發(fā)送至郵箱
很多小伙伴們都在為查看智慧校園課表而煩惱,今天特地整理了這篇文章,不僅可以用Python獲取智慧校園每日課表,還會(huì)自動(dòng)發(fā)至你郵箱,需要的朋友可以參考下2021-05-05Python3實(shí)現(xiàn)騰訊云OCR識(shí)別
這篇文章主要為大家詳細(xì)介紹了Python3實(shí)現(xiàn)騰訊云OCR識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11