【发布时间】:2023-04-02 08:14:01
【问题描述】:
我的程序使用 Turtle 来绘制形状。它显示以下菜单:
- 进入圈子
- 输入矩形
- 移除形状
- 绘制形状
- 退出
如果用户输入“4”,程序应该按照它们在列表中的顺序绘制形状。
[问题]:所以它应该用随机值绘制一个圆形和一个矩形。如果用户输入“3”,我在尝试这样做以及删除形状时遇到问题。我留下了一个例子,说明我使用圆的随机函数来绘制形状。
另外,如果选择 == 3(删除形状),我的代码应该是什么?
非常感谢所有帮助。谢谢!
import turtle
import random
def circle():
turtle.pensize(1)
turtle.penup()
turtle.goto(x, yCoordinate)
turtle.pendown()
turtle.color(color)
turtle.circle(radius)
turtle.hideturtle()
turtle.done()
def rectangle():
turtle.penup()
turtle.goto(xRectangle - width / 2, yRectangle - height / 2)
turtle.pendown()
turtle.color(colorRectangle)
turtle.goto(xRectangle - width / 2, yRectangle + height / 2)
turtle.goto(xRectangle + width / 2, yRectangle + height / 2)
turtle.goto(xRectangle + width / 2, yRectangle - height / 2)
turtle.goto(xRectangle - width / 2, yRectangle - height / 2)
turtle.hideturtle()
turtle.done()
def remove():
pass
def shapes():
turtle.pensize(1)
turtle.penup()
turtle.goto(x.random, yCoordinate.random)
turtle.pendown()
turtle.color(random)
turtle.circle(radius.random)
COMMANDS = [None, circle, rectangle, remove, shapes, exit]
ABORT = len(COMMANDS) - 1
PROMPT = "1. Draw a circle\n2. Draw a rectangle\n3. Remove Shapes\n4. Draw Shapes\n5. Quit"
while True:
choice = turtle.numinput("Pick a number", prompt=PROMPT, default=ABORT, minval=1, maxval=ABORT)
if choice == 1:
x = int(input("Enter the X Coordinate: "))
yCoordinate = int(input("Enter the Y Coordinate: "))
radius = int(input("Enter the Radius: "))
color = str(input("Enter desired Color (choose red, yellow, blue or green): "))
xCoordinate = (x - radius)
if choice == 2:
xRectangle = int(input("Enter the X Coordinate: "))
yRectangle = int(input("Enter the Y Coordinate: "))
height = int(input("Enter Height: "))
width = int(input("Enter Width: "))
colorRectangle = str(input("Enter desired Color (choose red, yellow, blue or green): "))
if choice == 3:
print(1)
if choice == 4:
print(shapes)
if choice is None:
choice = ABORT
else:
choice = int(choice)
if 1 <= choice <= ABORT:
COMMANDS[choice]()
turtle.mainloop() # never reached
【问题讨论】:
-
你到底有什么问题?
-
“如果选择 == 3”和“如果选择 == 4”。如何删除形状以及如何使用随机值绘制形状(圆形和矩形(按此顺序))。
-
保留所有形状的列表。对于 3,使用带有
random.randint()的随机整数,从列表中删除随机索引处的项目。对于4,用类似的方法生成一个随机整数,如果随机整数为1,则创建一个矩形,2为圆,以此类推。 -
但我不想随机调用我的形状并简单地绘制它们。我需要圆形和矩形的值是随机的。例如,随机半径、随机宽度、随机高度和随机坐标。程序应该先画一个圆,然后再画一个矩形。
-
然后使用
random.uniform(1,5)例如创建一个介于 1 和 5 之间的半径 :)
标签: python python-3.x turtle-graphics