Python:尝试在一个随机点做一个圆,半径随机,用一组给定的颜色随机填充。粘在p颜色上

2024-05-17 01:12:56 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我目前所掌握的情况。色环(myTurtle)似乎不起作用,我真的不知道该怎么做。有点业余,任何提示都将不胜感激。在

def drawPolygon(myTurtle,sideLength,numSides):
    turnAngle = 360/numSides
    for i in range(numSides):
        myTurtle.forward(sideLength)
        myTurtle.right(turnAngle)

def drawCircle(myTurtle,radius):
    circumference = 2 * 3.1415 * radius
    sideLength = circumference / 360
    drawPolygon(myTurtle, sideLength, 360)

def colorCircle(myTurtle):
    x = random.random()
    y = random.random()
    myTurtle.up()
    myTurtle.goto(x,y)
    myTurtle.down()
    radius = random.random()
    color = [('white',0),('yellow',1),('green',2),('blue',3),('purple',4),('red',5),('black',6),('magenta',7),('pink',8),('brown',9)]
    random.randint(0,9)
    myTurtle.begin_fill(color)
    drawCircle(myTurtle)

Tags: fordef情况randomcolor业余radiuscircumference
2条回答

首先,你不能把你的随机数分配给任何东西

random.randint(0,9) 
#You just say okay give me random number from 0 to 9 and then don't do anything with it

第二个问题是不能用begin_fill(color)填充,但可以键入begin_fill(),但默认填充颜色为黑色。所以你需要做的是这样:
color = [('white',0),('yellow',1),('green',2),('blue',3),('purple',4),('red',5),('black',6),('magenta',7),('pink',8),('brown',9)] randomPos = random.randint(0,9) #We randomly choose which color will it be myTurtle.fill_color(color[ randomPos ]) #We set fill color to color we previously chose myTurtle.begin_fill() #We start to fill

如果你有什么问题我可以问我。在

这个:

color = [('white',0),('yellow',1),('green',2),('blue',3),('purple',4),('red',5),('black',6),('magenta',7),('pink',8),('brown',9)]

random.randint(0,9)
myTurtle.begin_fill(color)

相当于:

^{pr2}$

因为你没有把随机数赋给一个变量,在这个例子中,这个随机数被丢弃了。然后将整个颜色列表作为begin_fill()的参数传递。在

要获得随机颜色,可以执行以下操作:

colors = ['white', 'yellow'....]  #Note the change here.
rand_num = random.randint(0,9)
selected_color = colors[rand_num]

但是,python制造的东西easier远不止这些:

import random

colors = ['white', 'yellow'....]
selected_color = random.choice(colors)  #Pick a random element from the colors list

编辑:===========

但是,您试图将参数传递给begin_fill()函数,但是begin_fill()不接受任何参数。默认情况下,begin_fill()使用黑色作为填充颜色。要将填充颜色设置为其他颜色,请使用以下函数之一:

fill_color('red')  #Sets fill color.
color('red')       #Sets both pen and fill color.

以下是您可以做的一些示例:

import turtle
import random

def drawPolygon(myTurtle,sideLength,numSides):
    turnAngle = 360/numSides
    for i in range(numSides):
        myTurtle.forward(sideLength)
        myTurtle.right(turnAngle)

def drawCircle(myTurtle, radius, fill_color="blue"):
    circumference = 2 * 3.1415 * radius
    sideLength = circumference / 360
    myTurtle.color(fill_color) #color() sets pen and fill color

    myTurtle.begin_fill()  #The next shape that is drawn will be filled.
    drawPolygon(myTurtle, sideLength, 360)
    myTurtle.end_fill()  #Disable filling.

def getRandomColor():
    colors = ['yellow', 'green', 'blue']
    rand_color = random.choice(colors)
    return rand_color


rand_color = getRandomColor()
drawCircle(turtle, 40, rand_color)
turtle.exitonclick()

或者你可以这样做:

import turtle
import random

def drawPolygon(myTurtle,sideLength,numSides):
    turnAngle = 360/numSides
    for i in range(numSides):
        myTurtle.forward(sideLength)
        myTurtle.right(turnAngle)

def getRandomColor():
    colors = ['yellow', 'green', 'blue']
    rand_color = random.choice(colors)
    return rand_color

def drawCircle(myTurtle, radius):
    circumference = 2 * 3.1415 * radius
    sideLength = circumference / 360

    rand_color = getRandomColor()
    myTurtle.color(rand_color) #color() sets pen and fill color

    myTurtle.begin_fill()
    drawPolygon(myTurtle, sideLength, 360)
    myTurtle.end_fill()

drawCircle(turtle, 40)
turtle.exitonclick()

==============

最后,颜色不是列表的好名字。列表名称应该是复数,例如:颜色、数字、单词;然后可以编写如下循环:

for color in colors:
    ...

for number in numbers:
    ...

for word in words:
    ...

相关问题 更多 >