简体   繁体   English

如何使用随机选择颜色

[英]How to use random to choose colors

I'm trying to make a little program to learn more, but am stuck when it comes to using random. 我正在尝试制作一个程序来学习更多内容,但在使用随机数时却陷入困境。

Here is an example of what I'm going off of https://trinket.io/Python/3338c95430 这是一个我要离开https://trinket.io/Python/3338c95430的例子

I've tried using random.randrange, random.choice, random.random with everything for them and it sends an error code saying random doesn't have a function of randrange, choice, or random. 我已经尝试使用random.randrange,random.choice,random.random和它们的所有内容,它发送一个错误代码,说随机没有randrange,choice或random的功能。

import turtle, math, random, time

wn = turtle.Screen()
wn.bgcolor('grey')
Rocket = turtle.Turtle()
Rocket.speed(0)
Rocket.color('red') ## this is what i want to randomize
rotate=int(90)

def drawCircles(t,size):
    for i in range(15):
        t.circle(size)
        size=size-10
def drawSpecial(t,size,repeat):
    for i in range(repeat):
        drawCircles(t,size)
        t.right(360/repeat)
drawSpecial(Rocket,100,10)

Eventually I would like to implement more randomized processes like the size and placement but for now I'm just focusing on color. 最终我想实现更多的随机化过程,如大小和位置,但现在我只关注颜色。

Without using additional imports it's fairly simple: 不使用额外的导入它很简单:

turtle.colormode(255) # sets the color mode to RGB

R = random.randrange(0, 256, 100) # last value optional (step) 
B = random.randrange(0, 256)
G = random.randrange(0, 256)

# using step allows more control if needed
# for example the value of `100` would return `0`, `100` or `200` only

Rocket.color(R, G, B) ## randomized from values above

Using randomized values of ( 200 , 255 , 23 ): 使用的(随机值20025523 ):

在此输入图像描述

EDIT : Regarding "would i just change the turtle.colormode() to Rocket.colormode() for the next one?" 编辑 :关于“我只是将turtle.colormode()更改为Rocket.colormode()用于下一个吗?”

The way I would recommend doing it would be to create a function: 我建议这样做的方法是创建一个函数:

def drawColor():

    turtle.colormode(255)

    R = random.randrange(0, 256)
    B = random.randrange(0, 256)
    G = random.randrange(0, 256)

    return R, G, B

Rocket.color(drawColor())

This way you can call drawColor() anytime you want a new color. 这样,您可以随时调用drawColor()以获得新颜色。

Now that you have the basics of randomizing colors for your drawing you can get quite creative with the values for some awesome looking results (adjust integers to your liking): 现在您已经掌握了绘图随机化颜色的基础知识,您可以通过一些非常棒的结果(根据您的喜好调整整数)来获得相当大的创意:

在此输入图像描述

#!/usr/bin/python

import turtle, math, random, time

def drawColor(a, b, o):
    turtle.colormode(255)
    R = random.randrange(a, b, o) # last value is step (optional)
    B = random.randrange(a, b, o)
    G = random.randrange(a, b, o)
    # print(R, G, B)
    return R, G, B

def drawRocket(offset):
    Rocket = turtle.Turtle()
    Rocket.speed(0)
    Rocket.color(drawColor(20, 100, 1)) # start (0-256), end (0-256), offset 
    rotate=int(random.randrange(90))
    drawSpecial(Rocket,random.randrange(0, 10), offset)

def drawCircles(t,size):
    for i in range(30):
        t.circle(size)
        size = size - 20

def drawSpecial(t,size,repeat):
    for i in range(repeat):
        drawCircles(t,size)
        t.right(360/repeat)

def drawMain(x, y):
    wn = turtle.Screen()
    wn.bgcolor(drawColor(0, 20, 2))

    for i in range(3): # iterations
        drawRocket(x)
        x+=y
        # print(x)

drawMain(2, 10) # offset, step
input("Press ENTER to exit")

One way to generate random colors (assuming RGB) within the 256 range is to use np.random.choice . 在256范围内生成随机颜色(假设RGB)的一种方法是使用np.random.choice

import numpy as np

color = tuple(np.random.choice(range(256), size=3))
print(color)

As per jpmc26's comment, another method without requiring Numpy is to use random.choices if you're using Python 3.6+. 根据jpmc26的评论,如果你使用Python 3.6+,另一种不需要Numpy的方法就是使用random.choices This is probably a better option since importing Numpy just to create 3 random integers is overkill. 这可能是一个更好的选择,因为导入Numpy只是为了创建3个随机整数是过度的。

import random

color = tuple(random.choices(range(256), k=3))
print(color)

Example random RGB output 随机RGB输出示例

(248, 88, 28) (248,88,28)

(165, 248, 150) (165,248,150)

(188, 29, 135) (188,29,135)

random.choice returns a random element from a sequence, just pass it a list with the values you want to choose from: random.choice从序列中返回一个随机元素,只需传递一个包含您想要选择的值的列表:

rand_color = random.choice(['blue', 'red'])
Rocket.color(rand_color)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM