Python 海龟棋盘
Python Turtle Checkerboard
import turtle
def main():
t=turtle
s=int(input("Enter the length of each square: "))
t.screensize(2000,2000,"lightblue")
for row in range(0,5):
for column in range(0,5):
if (row+column)%2==0:
t.pendown()
t.fillcolor("black")
t.begin_fill()
square(s,row,column)
else:
t.pendown()
t.fillcolor("white")
t.begin_fill()
square(s,row,column)
t.goto(s+row*s,s+column*s)
def square(s,row,column):
t=turtle
t.penup()
n=0
for count in range(4):
t.pendown()
t.forward(s)
t.left(90)
t.end_fill()
t.penup()
main()
所以今天我接到了一项任务,要求我创建一个 5 x 5 的棋盘。到目前为止,我有这段代码可以创建大部分棋盘。但是,我还是有哪里出错了,或者漏掉了一些关键信息。
所附图片显示了程序出现错误时的样子。
程序从创建黑色方块开始,可以在左下角看到它。然后它一直工作到右上角,在那里可以看到空的space。
请帮忙。
将 t.goto(s+row*s,s+column*s)
移动到内部 for 循环的开头就可以了。
基本上我们需要先将海龟移动到起始位置,然后再开始绘制。
我还清理了代码,将多余的行放在 square
函数中。
此外,添加了 t.penup()
以便乌龟在到达起始位置并开始绘制之前不会显示绘图。
import turtle
def main():
t=turtle
t.penup()
s=int(input("Enter the length of each square: "))
t.screensize(2000,2000,"lightblue")
for row in range(0,5):
for column in range(0,5):
t.goto(s+row*s,s+column*s)
if (row+column)%2==0:
square(s,row,column,"black")
else:
square(s,row,column,"white")
def square(s,row,column,color):
t=turtle
t.pendown()
t.fillcolor(color)
t.begin_fill()
t.penup()
n=0
for count in range(4):
t.pendown()
t.forward(s)
t.left(90)
t.end_fill()
t.penup()
main()
Anil_M 比我快了几分钟;但我想提供一些额外的代码清理,因为你有太多不必要的 penup
s、pendown
s 和不必要的参数传递。
试试这个:
import turtle
t = turtle.Turtle()
t.speed(0)
def main():
s=int(input("Enter the length of each square: "))
for row in range(5):
for column in range(5):
if (row+column)%2==0:
color = "black"
else:
color = "white"
t.penup()
t.goto(row*s,column*s)
t.pendown()
filled_square(s, color)
def filled_square(s, color):
t.fillcolor(color)
t.begin_fill()
for count in range(4):
t.forward(s)
t.left(90)
t.end_fill()
main()
让我们尝试冲压而不是绘图。这在简化逻辑的同时提高了我们的速度。我们印上一个大的黑色方块来代表棋盘,然后在上面印上白色方块:
from turtle import Turtle, Screen
SQUARES_PER_SIDE = 5
CURSOR_SIZE = 20
def main():
length = int(input("Enter the length of each square: "))
screen = Screen()
screen.bgcolor("lightblue")
turtle = Turtle('square', visible=False)
turtle.shapesize(SQUARES_PER_SIDE * length / CURSOR_SIZE)
turtle.speed('fastest')
turtle.stamp() # black background
turtle.shapesize(length / CURSOR_SIZE)
turtle.fillcolor("white")
turtle.penup()
edge = (1 - SQUARES_PER_SIDE) / 2 * length # center of left or bottom square
turtle.goto(edge, edge)
for row in range(SQUARES_PER_SIDE):
for column in range(SQUARES_PER_SIDE):
if (row + column) % 2 == 0:
turtle.stamp() # white square
turtle.forward(length)
turtle.goto(edge, edge + (row + 1) * length)
screen.exitonclick()
main()
输出
import turtle
def main():
t=turtle
s=int(input("Enter the length of each square: "))
t.screensize(2000,2000,"lightblue")
for row in range(0,5):
for column in range(0,5):
if (row+column)%2==0:
t.pendown()
t.fillcolor("black")
t.begin_fill()
square(s,row,column)
else:
t.pendown()
t.fillcolor("white")
t.begin_fill()
square(s,row,column)
t.goto(s+row*s,s+column*s)
def square(s,row,column):
t=turtle
t.penup()
n=0
for count in range(4):
t.pendown()
t.forward(s)
t.left(90)
t.end_fill()
t.penup()
main()
所以今天我接到了一项任务,要求我创建一个 5 x 5 的棋盘。到目前为止,我有这段代码可以创建大部分棋盘。但是,我还是有哪里出错了,或者漏掉了一些关键信息。
所附图片显示了程序出现错误时的样子。
程序从创建黑色方块开始,可以在左下角看到它。然后它一直工作到右上角,在那里可以看到空的space。
请帮忙。
将 t.goto(s+row*s,s+column*s)
移动到内部 for 循环的开头就可以了。
基本上我们需要先将海龟移动到起始位置,然后再开始绘制。
我还清理了代码,将多余的行放在 square
函数中。
此外,添加了 t.penup()
以便乌龟在到达起始位置并开始绘制之前不会显示绘图。
import turtle
def main():
t=turtle
t.penup()
s=int(input("Enter the length of each square: "))
t.screensize(2000,2000,"lightblue")
for row in range(0,5):
for column in range(0,5):
t.goto(s+row*s,s+column*s)
if (row+column)%2==0:
square(s,row,column,"black")
else:
square(s,row,column,"white")
def square(s,row,column,color):
t=turtle
t.pendown()
t.fillcolor(color)
t.begin_fill()
t.penup()
n=0
for count in range(4):
t.pendown()
t.forward(s)
t.left(90)
t.end_fill()
t.penup()
main()
Anil_M 比我快了几分钟;但我想提供一些额外的代码清理,因为你有太多不必要的 penup
s、pendown
s 和不必要的参数传递。
试试这个:
import turtle
t = turtle.Turtle()
t.speed(0)
def main():
s=int(input("Enter the length of each square: "))
for row in range(5):
for column in range(5):
if (row+column)%2==0:
color = "black"
else:
color = "white"
t.penup()
t.goto(row*s,column*s)
t.pendown()
filled_square(s, color)
def filled_square(s, color):
t.fillcolor(color)
t.begin_fill()
for count in range(4):
t.forward(s)
t.left(90)
t.end_fill()
main()
让我们尝试冲压而不是绘图。这在简化逻辑的同时提高了我们的速度。我们印上一个大的黑色方块来代表棋盘,然后在上面印上白色方块:
from turtle import Turtle, Screen
SQUARES_PER_SIDE = 5
CURSOR_SIZE = 20
def main():
length = int(input("Enter the length of each square: "))
screen = Screen()
screen.bgcolor("lightblue")
turtle = Turtle('square', visible=False)
turtle.shapesize(SQUARES_PER_SIDE * length / CURSOR_SIZE)
turtle.speed('fastest')
turtle.stamp() # black background
turtle.shapesize(length / CURSOR_SIZE)
turtle.fillcolor("white")
turtle.penup()
edge = (1 - SQUARES_PER_SIDE) / 2 * length # center of left or bottom square
turtle.goto(edge, edge)
for row in range(SQUARES_PER_SIDE):
for column in range(SQUARES_PER_SIDE):
if (row + column) % 2 == 0:
turtle.stamp() # white square
turtle.forward(length)
turtle.goto(edge, edge + (row + 1) * length)
screen.exitonclick()
main()
输出