【问题标题】:call random values for drawing shapes and removing shapes调用随机值以绘制形状和删除形状
【发布时间】:2023-04-02 08:14:01
【问题描述】:

我的程序使用 Turtle 来绘制形状。它显示以下菜单:

  1. 进入圈子
  2. 输入矩形
  3. 移除形状
  4. 绘制形状
  5. 退出

如果用户输入“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


【解决方案1】:

您的函数缺少参数,使用“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 

【讨论】:

  • 我现在无法使用您的代码绘制圆形或矩形,“退出”选项不再起作用,删除形状选项给我一个错误:“第 64 行,在 COMMANDS[choice ] TypeError: list indices must be integers or slices, not float",并且draw shape选项只绘制矩形。
  • 我没有编辑选项 1 和 2,它们只是缺少函数调用。不管怎样,你去吧,我为你添加了它们。对于删除,选择是一个浮点数,您必须先将其解析为 int,然后才能将其用作索引
  • 唯一不起作用的是“退出”选项和删除形状选项。我怎样才能让它们发挥作用?
  • 谢谢! “删除形状”选项效果很好,但是当我输入“5”退出时,它显示“无效,重试...”
  • 没问题,努力吧,这个问题是条件语句的问题,你只需要把“if choice == 0”改成“if choice == 5”跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
相关资源
最近更新 更多