Python 蟒蛇龟,如何让颜色自行改变?
为minecraft创建立方体图像。 试图让盒子里的颜色自行改变。除了使用随机数,还有什么整数算法可以用来实现这一点吗?因为现在,它创建了随机的颜色,但我希望小盒子能够自己改变颜色。有什么想法吗Python 蟒蛇龟,如何让颜色自行改变?,python,colors,minecraft,cube,turtle-graphics,Python,Colors,Minecraft,Cube,Turtle Graphics,为minecraft创建立方体图像。 试图让盒子里的颜色自行改变。除了使用随机数,还有什么整数算法可以用来实现这一点吗?因为现在,它创建了随机的颜色,但我希望小盒子能够自己改变颜色。有什么想法吗 import turtle import random minecraft = turtle.Turtle() minecraft.ht() minecraft.speed(9999999999999) #I guess there is a max speed??? wanted it to m
import turtle
import random
minecraft = turtle.Turtle()
minecraft.ht()
minecraft.speed(9999999999999) #I guess there is a max speed??? wanted it to make the mini cubes a lot faster.
#centers the box
minecraft.up()
minecraft.goto(-50,50)
minecraft.down()
#end of center box
for i in range(4): #Creates the box
minecraft.forward(100)
minecraft.right(90)
for i in range(1000): #Repeats all this code over and over
for i in range(10): #makes the 10 cubes going down, then it comes back up and repeates making cubes until it gets to the last cube.
for i in range(10): #initiate the random colors
red = random.random()
blue = random.random()
yellow = random.random()
minecraft.color(red, blue, yellow)
for i in range(1): #the little boxes
minecraft.begin_fill()
minecraft.forward(10)
minecraft.right(90)
minecraft.forward(10)
minecraft.right(90)
minecraft.forward(10)
minecraft.right(90)
minecraft.forward(10)
minecraft.right(90)
minecraft.end_fill()
minecraft.right(90) #little boxes changing directions
minecraft.forward(10)
minecraft.right(-90)
minecraft.forward(10) #little boxes changing directions...again
minecraft.right(-90)
minecraft.forward(100)
minecraft.right(90)
minecraft.right(180) #and again...
minecraft.forward(100)
minecraft.right(180)
random.random()创建介于0和1之间的随机数。因此,当您需要生成一个介于0和MAX之间的随机数时,只需简单地与它相乘,在您的情况下,如下所示:
int(random.random()*256)
顺便说一句,我又查了海龟的文件。color(*args)需要两个颜色args,“Return或set pencolor和fillcolor”。这意味着你需要通过它。颜色((40,80,120),(160,200,240))这样。根据你对问题的描述,这就是我认为你想要的——一个由随机颜色的框组成的网格,它们看起来会随机地改变颜色:
from turtle import Turtle, Screen
import random
BOX_SIZE = 100
SQUARE_SIZE = 10
DELAY = 100 # milliseconds
minecraft = Turtle(shape="square", visible=False)
minecraft.shapesize(SQUARE_SIZE / 20)
minecraft.speed("fastest")
# Center the box
minecraft.up()
minecraft.goto(-BOX_SIZE//2, BOX_SIZE//2)
minecraft.down()
# Create the box
for _ in range(4):
minecraft.forward(BOX_SIZE)
minecraft.right(90)
minecraft.up()
# Move turtle inside box
minecraft.forward(SQUARE_SIZE//2)
minecraft.right(90)
minecraft.forward(SQUARE_SIZE//2)
minecraft.left(90)
squares = []
for i in range(BOX_SIZE // SQUARE_SIZE):
# makes the 10 cubes going across, then it backs up and
# repeats making cubes until it gets to the last cube.
for j in range(BOX_SIZE // SQUARE_SIZE):
# initiate the random colors
red = random.random()
blue = random.random()
yellow = random.random()
minecraft.color(red, blue, yellow)
squares.append((minecraft.position(), minecraft.stamp()))
minecraft.forward(SQUARE_SIZE)
minecraft.backward(SQUARE_SIZE)
minecraft.sety(minecraft.ycor() - SQUARE_SIZE)
minecraft.right(180) # little boxes changing directions
def change():
random_choice = random.choice(squares)
squares.remove(random_choice)
position, stamp = random_choice
minecraft.goto(position)
red = random.random()
blue = random.random()
yellow = random.random()
minecraft.color(red, blue, yellow)
minecraft.clearstamp(stamp)
squares.append((minecraft.position(), minecraft.stamp()))
screen.ontimer(change, DELAY)
screen = Screen()
screen.ontimer(change, DELAY)
screen.exitonclick()
与许多海龟问题一样,解决这个问题的关键是盖章,而不是画画。戳记图像可以完成许多绘制图像无法完成的事情——在本例中,它们可以单独删除并替换为其他戳记
另外,千万不要让您的turtle代码永远运行,也不要让任何接近它的代码永远运行——改用
ontimer()
事件,以便其他turtle事件(如正确关闭窗口)能够正确触发 一直说颜色顺序不好(xxx,xxx,xxx)这似乎更多的是一个评论而不是一个答案。作为回答,它没有解决OP的核心问题。作为评论,这通常是错误的。默认的turtle.colormode()
为1.0,这意味着random.random()
是一种有效的生成颜色的方法,您可以在运行代码时看到它。另外,turtle.color()
可以只接受一个参数,该参数被视为pencolor和fillcolor——您不需要同时提供这两个参数。turtle的速度数字有点奇怪。速度0是最快的,但是他们从1变为最慢,2变为稍微快一点,6变为正常速度,等等,直到10变为最快。任何大于10或小于0.5的数字都将转换为0。请参阅帮助(海龟速度)
。