【发布时间】:2017-10-16 22:28:43
【问题描述】:
对于我的任务,我正在尝试制作一个 5 x 5 的棋盘,用户可以选择颜色和正方形大小。我知道了如何根据用户输入制作正方形大小和颜色,但在如何开始循环或如何创建 5 x 5 棋盘格方面遇到了一些麻烦。我只是不确定我能做些什么来移动乌龟来制作一个 5x5 板。到目前为止我已经完成了这么多,如果有人可以帮助我开始,我将非常感激!
import turtle
def main():
length = int(input("Enter a desired length (from 1-150.)"))
keepGoing = 'y'
while keepGoing == 'y':
print("What color would you like to draw?")
print(" Enter 1 for Black")
print(" 2 for Blue")
print(" 3 for Red")
print(" 4 for Green")
choice = int(input(" Your choice?"))
if choice == 1:
square(0,0,length,'black')
elif choice == 2:
square(0,0,length,'blue')
elif choice == 3:
square(0,0,length,'red')
elif choice == 4:
square(0,0,length,'green')
else:
print("ERROR: only enter 1-4.")
def square(x, y, width, color):
turtle.clear()
turtle.penup() # Raise the pen
turtle.goto(x, y) # Move to (X,Y)
turtle.fillcolor(color) # Set the fill color
turtle.pendown() # Lower the pen
turtle.begin_fill() # Start filling
for count in range(4): # Draw a square
turtle.forward(width)
turtle.left(90)
turtle.end_fill()
#calling main function
main()
【问题讨论】:
-
只需绘制并填充每个正方形。遍历所有方块,确定它们的位置。然后在每个位置绘制一个适当颜色的正方形。
标签: python input turtle-graphics