1. 程式人生 > >python用turtle畫國際象棋棋盤

python用turtle畫國際象棋棋盤

imp got log ima images gin lac 學習 pytho

python學習的練習

用turtle畫的國際象棋棋盤

import turtle
turtle.speed(10)
a = 30

#畫背景
turtle.up()
turtle.goto( -10, -10)
turtle.down()
turtle.fillcolor("Khaki")
turtle.begin_fill()
turtle.fd( a * 8 + 20)
turtle.left(90)
turtle.fd( a * 8 + 20)
turtle.left(90)
turtle.fd( a * 8 + 20)
turtle.left(90)
turtle.fd( a * 8 + 20)
turtle.left(90)
turtle.end_fill()

#畫黑格

for i in range(8):
    if i % 2 == 0:#偶數行
        turtle.up()
        turtle.goto(0, i * a)
        turtle.down()
        for j in range(4):
            turtle.fillcolor("black")
            turtle.begin_fill()
            turtle.fd( a )
            turtle.left(90)
            turtle.fd( a )
            turtle.left(90)
            turtle.fd( a )
            turtle.left(90)
            turtle.fd( a )
            turtle.left(90)
            turtle.end_fill()
            turtle.up()
            turtle.fd(a * 2)
            turtle.down()
    else:#奇數行
        turtle.up()
        turtle.goto(a , a * i)
        turtle.down()
        for n in range( 4 ):
            turtle.fillcolor("black")
            turtle.begin_fill()
            turtle.fd( a )
            turtle.left(90)
            turtle.fd( a )
            turtle.left(90)
            turtle.fd( a )
            turtle.left(90)
            turtle.fd( a )
            turtle.left( 90 )
            turtle.end_fill()
            turtle.up()
            turtle.fd( a * 2 )
            turtle.down()
#畫白格
turtle.up()
turtle.goto(0, 0)
turtle.down()
turtle.fd( a * 8 )
turtle.left(90)
turtle.fd( a * 8 )
turtle.left(90)
turtle.fd( a * 8 )
turtle.left(90)
turtle.fd( a * 8 )
turtle.left(90)

turtle.done()

成果

技術分享圖片

python用turtle畫國際象棋棋盤