在我的代码中实现随机颜色按钮
Implementing random color button in my code
我需要制作一个按钮,当我按下它时它会生成一种随机颜色,#I 可以用它来绘图。我查过其他一些随机颜色生成器,但我不知道如何实现它。到目前为止,这是我的代码。
from processing import *
tool = "paintbrush"
tool = "shapechanger"
red_color = 0
green_color = 0
blue_color = 0
def setup():
size(400,400)
draw_red_button()
def draw_red_button():
set_red()
fill(red_color, green_color, blue_color)
rect(0, 380, 20, 20)
def red_button_pressed():
if mouse.pressed and (mouse.x>0 and
mouse.x<20 and
mouse.y>380 and
mouse.y<400):
return True
else:
return False
def draw():
if red_button_pressed():
set_red()
else:
fill(red_color, green_color, blue_color)
paint()
stroke(red_color, green_color, blue_color)
def paint():
if mouse.pressed:
ellipse(mouse.x, mouse.y, 20,20)
def set_red():
global red_color
global green_color
global blue_color
red_color = 255
green_color = 0
blue_color = 0
run()
正在下载当前的Processing环境,下面是我想出的解决方案。 (您的代码提示此软件的版本不同,因此您可能需要相应地进行调整。)
from processing import *
red_color, green_color, blue_color = 0, 0, 0
# Programmer defined functions
def draw_random_button():
fill(255, 255, 255)
rect(0, 380, 20, 20)
def overButton():
return 0 < mouseX < 20 and 380 < mouseY < 400
def paint():
if mousePressed:
ellipse(mouseX, mouseY, 20, 20)
def set_random_color():
global red_color
global green_color
global blue_color
red_color = random(256)
green_color = random(256)
blue_color = random(256)
fill(red_color, green_color, blue_color)
# System defined functions
def setup():
size(400, 400)
draw_random_button()
set_random_color()
def mousePressed():
if overButton():
set_random_color()
def draw():
if not overButton():
paint()
stroke(red_color, green_color, blue_color)
一个令人困惑的事情是 mousePressed
是一个可以测试的变量 而 是一个可以定义的函数。
我需要制作一个按钮,当我按下它时它会生成一种随机颜色,#I 可以用它来绘图。我查过其他一些随机颜色生成器,但我不知道如何实现它。到目前为止,这是我的代码。
from processing import *
tool = "paintbrush"
tool = "shapechanger"
red_color = 0
green_color = 0
blue_color = 0
def setup():
size(400,400)
draw_red_button()
def draw_red_button():
set_red()
fill(red_color, green_color, blue_color)
rect(0, 380, 20, 20)
def red_button_pressed():
if mouse.pressed and (mouse.x>0 and
mouse.x<20 and
mouse.y>380 and
mouse.y<400):
return True
else:
return False
def draw():
if red_button_pressed():
set_red()
else:
fill(red_color, green_color, blue_color)
paint()
stroke(red_color, green_color, blue_color)
def paint():
if mouse.pressed:
ellipse(mouse.x, mouse.y, 20,20)
def set_red():
global red_color
global green_color
global blue_color
red_color = 255
green_color = 0
blue_color = 0
run()
正在下载当前的Processing环境,下面是我想出的解决方案。 (您的代码提示此软件的版本不同,因此您可能需要相应地进行调整。)
from processing import *
red_color, green_color, blue_color = 0, 0, 0
# Programmer defined functions
def draw_random_button():
fill(255, 255, 255)
rect(0, 380, 20, 20)
def overButton():
return 0 < mouseX < 20 and 380 < mouseY < 400
def paint():
if mousePressed:
ellipse(mouseX, mouseY, 20, 20)
def set_random_color():
global red_color
global green_color
global blue_color
red_color = random(256)
green_color = random(256)
blue_color = random(256)
fill(red_color, green_color, blue_color)
# System defined functions
def setup():
size(400, 400)
draw_random_button()
set_random_color()
def mousePressed():
if overButton():
set_random_color()
def draw():
if not overButton():
paint()
stroke(red_color, green_color, blue_color)
一个令人困惑的事情是 mousePressed
是一个可以测试的变量 而 是一个可以定义的函数。