python背景颜色怎么随机_如何使用随机选择颜色

如果不使用其他导入,则相当简单: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

使用随机值(200,255,23):

EDIT: Regarding "would i just change the turtle.colormode()

to Rocket.colormode() for the next one?"

我建议的方法是创建一个函数:

^{pr2}$

这样,您可以随时调用drawColor()来选择新颜色。在

现在你已经掌握了为你的绘图随机颜色的基本知识,你可以很有创意地为一些好看的结果设置值(根据你的喜好调整整数):

#!/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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值