【问题标题】:Python Turtle, how to get colors to change by themselves?Python Turtle,如何让颜色自己改变?
【发布时间】:2015-04-10 00:14:20
【问题描述】:

为我的世界创建一个立方体的图像。 试图让盒子里的颜色自己改变。除了使用随机数之外,我还可以使用某种整数算法来实现这一点吗?因为现在,它会创建随机颜色,但我希望小盒子自己改变颜色。有任何想法吗?

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)

【问题讨论】:

  • 海龟速度数字有点奇怪。速度 0 是最快的,然后它们从 1 最慢,2 稍微快一点,6 是正常速度,等等,直到 10 是快。任何大于 10 或小于 0.5 的数字都会转换为 0。请参阅 help(turtle.speed)

标签: python colors minecraft cube turtle-graphics


【解决方案1】:

根据您对问题的描述,我相信这就是您所要求的 - 一个随机颜色框的网格,这些框看起来可以随机改变颜色:

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()

这个问题的关键,就像许多海龟问题一样,是stamp,而不是draw。标记图像可以做许多绘制图像不能做的事情 - 在这种情况下,它们可以单独删除并替换为其他标记。

另外,永远不要让你的海龟代码永远运行,也不要让它接近它——使用ontimer() 事件代替,以便其他海龟事件(如正确关闭窗口)可以正确触发。

【讨论】:

    【解决方案2】:

    random.random() 创建一个介于 0 和 1 之间的随机数。因此,当您需要生成一个介于 0 和 MAX 之间的随机数时,只需简单地与它相乘,在您的情况下,就像这样:

    int(random.random()*256)
    

    顺便说一句,我再次检查了海龟文档。 turtle.color(*args) 需要两个颜色参数,“返回或设置 pencolor 和 fillcolor”。这意味着您需要以这种方式传递 turtle.color((40, 80, 120), (160, 200, 240)) 。

    【讨论】:

    • 一直说颜色顺序不好。 : (xxx,xxx,xxx)
    • 这似乎更像是一个评论而不是一个答案。作为答案,它没有解决 OP 的核心问题。作为评论,它通常是错误的。默认的turtle.colormode() 是1.0,这意味着random.random() 是生成颜色的有效方式,正如您在运行代码时看到的那样。此外,turtle.color() 可以只接受一个参数,该参数同时被视为 pencolor 和 fillcolor - 您不需要同时提供两者。
    猜你喜欢
    • 2018-07-07
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2021-11-16
    • 2021-07-24
    相关资源
    最近更新 更多