So, I am trying to write a program that draws white circles at mouseX, mouseY coordinates. When I press “p”, each circle is supposed to be randomly colored individually. When I click the mouse again, more white circles are drawn and the colored ones stay the same color, and if I press p again all circles are randomly colored again.
My code draws white circles fine, but when I press “p” the color for all the circles changes as one random color(not individually), and if I draw new circles, all colors become white.
Any help?
circle_x = []
circle_y = []
circle_size = 20
colour_r = []
colour_g = []
colour_b = []
# def circles(x,y,r,g,b):
# global circle_x, circle_y, circle_size
# fill(r,g,b)
# ellipse(x, y, circle_size, circle_size)
def setup():
size(400, 400)
background(155)
colour_r = [255]
colour_g = [255]
colour_b = [255]
def draw():
global circle_x, circle_y, colour_r, colour_g, colour_b, circle_size
background(155)
fill(255)
for z in range(0, len(colour_r)):
fill(colour_r[z], colour_g[z], colour_b[z])
for a in range(0, len(circle_x)):
ellipse(circle_x[a], circle_y[a], circle_size, circle_size)
def mouseClicked():
global circle_x, circle_y
circle_x.append(mouseX)
circle_y.append(mouseY)
colour_r.append(255)
colour_g.append(255)
colour_b.append(255)
def keyPressed():
if key == "p":
for z in range(0, len(colour_r)):
colour_r[z] = random(256)
for a in range(len(colour_g)):
colour_g[z] = random(256)
for a in range(len(colour_b)):
colour_b[z] = random(256)
3 Likes
I managed to fix it!! No need for responses
4 Likes
Would you like to post your own solution?
May I suggest some ideas?
Why create 3 lists for the color components?
colors = []
colors.append(color(255))
You could even have a single list for the circles… with a tuple inside
circles = []
circles.append((mouseX, mouseY, 10, color(255)))
In a bigger project you might want to investigate Named Tuples so you can use legible fields like circle.x
, circle.color
!
2 Likes
IMO, when we’ve got a bunch of containers to describe the characteristics of a collection of objects, we should simply turn everything into 1 class
, and then have 1 container of instances of that class
.
Although written for the Java Mode, this old article explains it w/ more details:
3 Likes
Those two methods are very helpful for my other program that I’m working on! Thank you! I’ll post my solution!
2 Likes
I finished my program and I pasted it bellow:
#circle_x, circle_y are list that stores circles x,y coordinates
circle_x = []
circle_y = []
circle_size = 20 #circle diameter
#lists that stores r,g,b colors, respectivelly
colour_r = []
colour_g = []
colour_b = []
def setup():
size(400, 400)
background(155)
def draw():
"""draws circles at mouseclicked coords and assigns colours according to key 'p' pressed
colors and coordinates are taken from the lists """
global circle_x, circle_y, colour_r, colour_g, colour_b, circle_size
background(155)
fill(255)
for z in range(0, len(colour_r)):
fill(colour_r[z], colour_g[z], colour_b[z])
ellipse(circle_x[z], circle_y[z], circle_size, circle_size)
def mouseClicked():
"""adds values to their list whenever mouse is clicked"""
global circle_x, circle_y
circle_x.append(mouseX)
circle_y.append(mouseY)
colour_r.append(255)
colour_g.append(255)
colour_b.append(255)
def keyPressed():
"""it changes the values of the lists for colours to random values"""
if key == "p":
for z in range(len(colour_r)):
colour_r[z] = random(256)
colour_g[z] = random(256)
colour_b[z] = random(256)
4 Likes