为绘制形状和移除形状调用随机值
call random values for drawing shapes and removing shapes
我的程序使用 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
您的函数缺少参数,使用 "elif" 也是一个好习惯,它可以减少您进行的检查次数,也会减少逻辑错误
此外,请务必参考文档以获取语法帮助!
https://docs.python.org/3.3/library/turtle.html?highlight=turtle#turtle.clear
我对你的代码做了一些修改来实现你所需要的
import turtle
import random
# Create a turtle object
myturtle = turtle.Turtle()
def circle(x, yCoordinate, radius, color):
myturtle.pensize(1)
myturtle.penup()
myturtle.goto(x, yCoordinate)
myturtle.pendown()
myturtle.pencolor(color)
myturtle.circle(10)
myturtle.hideturtle()
def rectangle(xRectangle, yRectangle, width, height, colorRectangle):
myturtle.penup()
myturtle.goto(xRectangle - width / 2, yRectangle - height / 2)
myturtle.pendown()
myturtle.pencolor(colorRectangle)
myturtle.goto(xRectangle - width / 2, yRectangle + height / 2)
myturtle.goto(xRectangle + width / 2, yRectangle + height / 2)
myturtle.goto(xRectangle + width / 2, yRectangle - height / 2)
myturtle.goto(xRectangle - width / 2, yRectangle - height / 2)
myturtle.hideturtle()
def rremove():
myturtle.clear()
def shapes():
myturtle.pensize(1)
myturtle.penup()
myturtle.goto(x.random, yCoordinate.random)
myturtle.pendown()
myturtle.color(random)
myturtle.circle(radius.random)
COMMANDS = [None, circle, rectangle, rremove, shapes, exit]
ABORT = len(COMMANDS) - 1
PROMPT = "1. Draw a circle\n2. Draw a rectangle\n3. Remove Shapes\n4. Draw Shapes\n5. Quit"
COLORS = ['red', 'yellow', 'blue', 'green']
while True:
choice = turtle.numinput("Pick a number", prompt=PROMPT, default=ABORT, minval=0, maxval=ABORT)
choice = int(choice)
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)
circle(x, yCoordinate, radius, color)
elif 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): "))
rectangle(xRectangle, yRectangle, width, height, colorRectangle)
elif choice == 3:
COMMANDS[choice]()
elif choice == 4:
#Here you can adjust the lower and upper bounds of the random number
x, yCoordinate, color, radius = random.randint(10,40), random.randint(10,200), random.choice(COLORS), random.randint(10,200)
COMMANDS[1](x, yCoordinate, radius,color)
print("Drew a circle")
xRectangle, yRectangle, width, height, colorRectangle = random.randint(10,200), random.randint(10,200), random.randint(10,200),random.randint(10,200), random.choice(COLORS)
COMMANDS[2](xRectangle, yRectangle, width, height, colorRectangle)
print("Drew a rectangle")
elif choice == 5:
COMMANDS[choice]()
else:
print("Invalid, try again...")
continue
我的程序使用 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
您的函数缺少参数,使用 "elif" 也是一个好习惯,它可以减少您进行的检查次数,也会减少逻辑错误
此外,请务必参考文档以获取语法帮助! https://docs.python.org/3.3/library/turtle.html?highlight=turtle#turtle.clear
我对你的代码做了一些修改来实现你所需要的
import turtle
import random
# Create a turtle object
myturtle = turtle.Turtle()
def circle(x, yCoordinate, radius, color):
myturtle.pensize(1)
myturtle.penup()
myturtle.goto(x, yCoordinate)
myturtle.pendown()
myturtle.pencolor(color)
myturtle.circle(10)
myturtle.hideturtle()
def rectangle(xRectangle, yRectangle, width, height, colorRectangle):
myturtle.penup()
myturtle.goto(xRectangle - width / 2, yRectangle - height / 2)
myturtle.pendown()
myturtle.pencolor(colorRectangle)
myturtle.goto(xRectangle - width / 2, yRectangle + height / 2)
myturtle.goto(xRectangle + width / 2, yRectangle + height / 2)
myturtle.goto(xRectangle + width / 2, yRectangle - height / 2)
myturtle.goto(xRectangle - width / 2, yRectangle - height / 2)
myturtle.hideturtle()
def rremove():
myturtle.clear()
def shapes():
myturtle.pensize(1)
myturtle.penup()
myturtle.goto(x.random, yCoordinate.random)
myturtle.pendown()
myturtle.color(random)
myturtle.circle(radius.random)
COMMANDS = [None, circle, rectangle, rremove, shapes, exit]
ABORT = len(COMMANDS) - 1
PROMPT = "1. Draw a circle\n2. Draw a rectangle\n3. Remove Shapes\n4. Draw Shapes\n5. Quit"
COLORS = ['red', 'yellow', 'blue', 'green']
while True:
choice = turtle.numinput("Pick a number", prompt=PROMPT, default=ABORT, minval=0, maxval=ABORT)
choice = int(choice)
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)
circle(x, yCoordinate, radius, color)
elif 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): "))
rectangle(xRectangle, yRectangle, width, height, colorRectangle)
elif choice == 3:
COMMANDS[choice]()
elif choice == 4:
#Here you can adjust the lower and upper bounds of the random number
x, yCoordinate, color, radius = random.randint(10,40), random.randint(10,200), random.choice(COLORS), random.randint(10,200)
COMMANDS[1](x, yCoordinate, radius,color)
print("Drew a circle")
xRectangle, yRectangle, width, height, colorRectangle = random.randint(10,200), random.randint(10,200), random.randint(10,200),random.randint(10,200), random.choice(COLORS)
COMMANDS[2](xRectangle, yRectangle, width, height, colorRectangle)
print("Drew a rectangle")
elif choice == 5:
COMMANDS[choice]()
else:
print("Invalid, try again...")
continue