1. 程式人生 > >Python圖形工具turtle繪製國際象棋棋盤

Python圖形工具turtle繪製國際象棋棋盤

#編寫程式繪製一個國際象棋的棋盤
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()

通過函式的重用優化程式碼:

先建立一個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中呼叫它
#編寫程式繪製一個國際象棋的棋盤

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()

最後結果:


相關推薦

Python圖形工具turtle繪製國際象棋棋盤

#編寫程式繪製一個國際象棋的棋盤 import turtle turtle.speed(30) turtle.penup() off = True for y in range(-40, 30 + 1

pythonturtle國際象棋棋盤

imp got log ima images gin lac 學習 pytho python學習的練習 用turtle畫的國際象棋棋盤 import turtle turtle.speed(10) a = 30 #畫背景 turtle.up() turtle.goto(

Python中使用turtle繪製棋盤詳解

效果 實現 原始碼 import turtle #宣告棋盤的單元格寬度為30,數量為18 width = 30 num = 18 #設定畫布大小 turtle.screensize(1200,900,"green") #宣告列表來存取座標 x1 = [(-400,400),(-40

Python圖形Turtle

隱藏 當前 如果 pan svi idt dot 左旋 16px 畫筆繪制狀態函數 函數 描述 pendown() 放下畫筆 penup() 提起畫筆,與pendown()配合使用 pensize(width

Python圖形介面turtle入門

簡單入門,畫兩個正方形,把幾個常用的操作加入了其中。 import turtle t=turtle.Pen() for i in range(4): t.forward(100) t.left(4) t.reset() i = 0 while i < 4: t.backw

Python實現使用turtle繪製聖誕樹

簡單的繪製聖誕樹 新建tree1.py或者直接輸入下面程式碼執行 #宣告樹的高度 height = 5 #樹的雪花數,初始為1 stars = 1 #以數的高度作為迴圈次數 for i in range(height):     print((' ' * (h

python庫之turtle圖形繪製) 開啟新的快樂源泉

相信有不少人學習python 都是聽了老前輩的推薦 “學python好,python有趣的程式碼多” 比如說畫一隻小獅子 這就是今天想要介紹的繪製圖形庫-turtle 如果也想這樣畫一隻小獅子,或者其他的小動物那就趕緊點贊收藏學起來吧 一、什麼是turtle庫 turtle庫是python的基礎繪相簿 這

python繪制圖形Turtle模塊)

tle 繪制圖形 pos for port imp eth down 結束 用python的Turtle模塊可以繪制很多精美的圖形,下面簡單介紹一下使用方法。 需要用到的工具有python,python 的安裝這裏就不再細說。自行搜索。 1 from turtle

Python-turtle庫知識小結(python繪圖工具

thead color 運行 入庫 logs erl pen 顏色 word   turtle:海龜(海龜庫) Turtle庫是Python語言中一個很流行的繪制圖像的函數庫 使用之前需要導入庫:import turtle ? turtle.setup(wid

python中的turtle庫繪制圖形

坐標軸 src 完成 弧度 tle backward 像素 font 多邊形 1. 前奏: 在用turtle繪制圖形時,需要安裝對應python的解釋器以及IDE,我安裝的是pycharm,在安裝完pycharm後,在pycharm安裝相應庫的模塊,繪圖可以引入turtle

使用Python中的Turtle庫繪制簡單的圖形

Python Turtle Turtle圖形庫 Turtle庫是Python內置的圖形化模塊,屬於標準庫之一,位於Python安裝目錄的lib文件夾下,常用函數有以下幾種: 畫筆控制函數 penup():擡起畫筆; pendown():落下畫筆; pensize(width):畫筆寬度; pe

python之繪制圖形turtle

nta false stat 準備 setw *args 列表 methods spa 關於繪制圖形庫turtle# 畫布上,默認有一個坐標原點為畫布中心的坐標軸(0,0),默認"standard"模式坐標原點上有一只面朝x軸正方向

Python 繪製圍棋棋盤

import turtle pen = turtle.Pen() pen.speed(10) width = 30 # 格子寬度 count = 18 # 橫向縱向格子數 o = width * count / 2 # 開始繪製原點 for i in range(cou

Python入門程式練習題-Turtle繪製疊邊形

Python入門程式練習題-Turtle繪製疊邊形 題目來自BIT的mooc 題目說明 使用turtle庫,繪製一個疊邊形,其中,疊邊形內角為80度。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪

根據檔案中讀取的資料使用turtle繪製圖形路徑

前言 這篇部落格主要是對從檔案中讀取的資料使用turtle繪製圖形路徑的操作 嗯,獨立完成,秀操作,明天也要加油鴨!!! 資料 資料是.txt檔案,共9行,每一行的第一列表示烏龜前進的畫素數,第二列表示轉動的方向(0表示向左),第三列表示

Python使用turtle繪製函式y=9-x^2的影象

turtle是Python自帶的繪圖模組,可以輕鬆完成很多圖形的繪製,啟動IDLE

python 包的使用 (三)——turtle:使用海龜圖形turtle graphics)繪製圖像

 Turtle庫是Python語言中一個很流行的繪製圖像的函式庫,想象一個小烏龜,在一個橫軸為x、縱軸為y的座標系原點,(0,0)位置開始,它根據一組函式指令的控制,在這個平面座標系中移動,從而在它爬行的路徑上繪製了圖形。turtle繪圖的基礎知識:1. 畫布(canvas)

Python利用turtle繪製五角星

繪製用到的引數我們存放在data.txt檔案中,data.txt檔案內容如下: 300,0,144,1,0,0 300,0,144,0,1,0 300,0,144,0,0,1 300,0,144,1,1,0 300,0,108,0,1,1 184,0,72,1,0,1 1

Turtle繪製帶顏色和字型的圖形(Python3)

在Python中有很多編寫圖形程式的方法,一個簡單的啟動圖形化程式設計的方法是使用Python內嵌的Turtle模組。Turtle是Python內嵌的繪製線、圓以及其他形狀(包括文字)的圖形模組。它

第二次用Python和Plotly成功繪製圖形

import pymysql import plotly import plotly.graph_objs plotly.tools.set_credentials_file(username='JaneGinkgo', api_key='IDOV3Q7jOtABYvw7