在龟屏上画一个方格旗

时间:2015-04-16 00:39:59

标签: python turtle-graphics

问题:实现以下伪代码以在屏幕上绘制方格旗。

1.  Ask the user for the size of the checkered flag (n).
2.  Draw an n x n grid to the screen.
3.  For i = 0,2,4,...,62:
4.     row = i // n
5.     offset = row % 2
6.     col = (i % n) + offset

请复制并粘贴该链接,请参阅output

我实现了伪代码,但我需要一些帮助。我能够绘制n * n网格;此外,我一直收到此错误:NameError: name 'row' is not defined

我的节目:

from turtle import*

def size():
  size = eval(input("Please enter the size of the checkered flag: "))
  return size

def draw(n):
  wn = Screen()
  wn.setworldcoordinates(-1,-1,10,10)
  pen = Turtle()
  for i in range(0,n+1):
    pen.up()
    pen.goto(0,i)
    pen.down()
    pen.forward(n)

  pen.left(90)
  for i in range(0,n+1):
    pen.up()
    pen.goto(i,0)
    pen.down()
    pen.forward(n)

def findGrid(n):
  for i in range(0,63):
    row = i // n
    offset = row % 2
    col = (i % n) + offset

  return row, col

def fillSquare(x,y):
  pen = Turtle()
  pen.hideturtle()
  pen.speed(10)
  pen.up()
  pen.goto(x,y)
  pen.fillcolor("black")
  pen.begin_fill()

def main():
  x = size()
  y = draw(x)
  row, col = findGrid(x)             #I think the problem is here.
  f = fillSquare(row, col)

main()

2 个答案:

答案 0 :(得分:0)

您在问题中发布的代码不会绘制正方形,因为您在pen.begin_fill()之后没有任何海龟操作。

您可以像这样绘制一个填充的正方形:

  turtle.begin_fill()
  for i in range(4):
    turtle.forward(1)
    turtle.right(90)
  turtle.end_fill()

伪代码实际上有一个错误。偏移计算offset = row % 2仅在n(行数)为偶数时有效。当n为奇数时,伪代码无法计算方格平方位置。

要使代码适用于n的偶数和奇数值,您可以按如下方式评估偏移量:

    offset = ~(n % 2) & (row % 2)

我已在下面的代码中实现了这些更改。我还通过在绘图函数外部定义乌龟并将其作为参数传递来修改程序的结构。这让我们只需设置一次乌龟的速度和可见度设置,而不必在每个绘图功能中这样做。

from turtle import*

# Ask the user for the size of the checkered flag (n).
def getSize():
  size = eval(input('Please enter the size of the checkered flag: '))
  return size

# Draw an n x n grid to the screen.
def drawGrid(turtle, n):
  for i in range(0, n+1):
    turtle.up()
    turtle.goto(0, i)
    turtle.down()
    turtle.forward(n)
  turtle.left(90)
  for i in range(0, n+1):
    turtle.up()
    turtle.goto(i, 0)
    turtle.down()
    turtle.forward(n)

# Fill the square in the given row and column.
def fillSquare(turtle, row, col):
  turtle.up()
  turtle.goto(col, row)
  turtle.begin_fill()
  for i in range(4):
    turtle.forward(1)
    turtle.right(90)
  turtle.end_fill()

def main():
  # Get the user's input.
  n = getSize()

  # Set up the drawing coordinates.
  screen = Screen()
  screen.setworldcoordinates(-1, -1, 10, 10)

  # Make a turtle object for use in drawing. Maximize its speed.
  turtle = Turtle()        
  turtle.speed('fastest')
  turtle.hideturtle()      

  # Draw the checkered flag.
  drawGrid(turtle, n)
  for i in range(0, n*n, 2):
    row = i // n
    offset = ~(n % 2) & (row % 2)
    col = i % n + offset
    fillSquare(turtle, row, col)

  print('Hit Enter to quit.')
  input()

main()

答案 1 :(得分:0)

这种情况我认为 stamping 比Python龟中的绘图更简单:

from turtle import Turtle, Screen

CURSOR_SIZE = 20

def getSize():
    """ Ask user for the size of the checkered flag. """

    return int(input('Please enter the size of the checkered flag: '))

cells = getSize()

screen = Screen()

size = min(screen.window_width() - 10, screen.window_height() - 30) / cells
offset = (cells % 2) * size/2 + size/2  # properly center odd & even cells

turtle = Turtle('square', visible=False)
turtle.shapesize(size / CURSOR_SIZE)
turtle.speed('fastest')
turtle.color('black')
turtle.penup()

for row in range(-cells // 2, cells // 2):
    parity = row % 2  # properly color cells
    turtle.goto(-cells // 2 * size + offset, row * size + offset)

    for column in range(cells):
        turtle.fillcolor(['white', 'black'][parity == column % 2])
        turtle.stamp()
        turtle.forward(size)

screen.exitonclick()

当我们处理更大的绘图块时,冲压也会使程序更快。