# [Beginner] Application of Lists (Part 2)

I’m having a tough time trying to create a game that involves conditionals, loops and lists together. The instructions are relatively long and I don’t know if I’m allowed to post it here for a better explanation. In a nutshell, I need to make a game that starts with 3 blocks stacked (with random red, green or blue colors), and by pressing b it gives a new square (with one of random colors), and by pressing s, it moves the last drawn square to a new stack, and I can only move more squares if they’re the same color (moving 3 to same stack gives me 5 points). And by pressing d, it destroys the last square created (costing 1 point).

My main issue is how to get to drawn random squares stacked with only colors of pure red, blue and green, but no other colors.

The conditionals, and loops I know enough to get it on my own.
If anybody can help me out on this one I would really appreciate.

You can define colours as hexadecimal values and get pure hues. Put those values in an array and use a random number to pick a colour from the array. hexadecimals can be appointed to a variable and processing understands them as colour definitions. You can use that colour then for fill and draw the rectangle like this:

``````def setup():
colors = ["#0000FF", "#00FF00", "#FF0000"] #Colours to be used
index = int(random(3)) #Random integer with values of 0, 1 or 2
c = colors[index] #Pick the colour
fill(c) #Set fill colour to randomly chosen colour
rect(25, 25, 50, 50) #Draw a rectangle
``````
2 Likes

Thank you! Is there any way to doing that with RGB colors? Because I need those RGB values to be stored, so I can create my conditionals for the game.

sorry forgot to mention that colours were blue, green and red. And those hexadecimal values are RGB.
#0000FF is same thing as (0,0,255). Hexadecimals as colours is nicely explained for instance here https://www.mathsisfun.com/hexadecimal-decimal-colors.html

and you can get hexadecimal values from color mixer on that page

2 Likes

You can convert `RGB(0-255, 0-255, 0-255)` to hexadecimal using the hex() function. You can do the reverse operation using unhex().

Also, the Processing IDE includes a color mixer, under Tools > Color Selector…

2 Likes

I need to make two lists(main_stack, and scoring_stacks), and they are items that I believe should store the colors like: main_stack = [blue, red, green, blue, blue, red, …], and the scoring stack should have a sequence of 3 of same color. I’m pretty stuck on how to add the blocks on these lists by their colors(that must be random).

It sounds like you need to use Index Brackets and different List Methods (see the reference) to shuffle your blocks between stacks?

``````colors = ['#FF0000', '#00FF00', '#0000FF']
stack1 = []
stack2 = []

# add a random block to stack 1
randomblock = colors[int(random(len(colors)))]
stack1.append(randomblock)
# add another random block to stack 1
randomblock = colors[int(random(len(colors)))]
stack1.append(randomblock)
print('stack1: ' + str(stack1))

# move last block from stack 1 to stack 2
stack2.append(stack1.pop(-1))
print('stack1: ' + str(stack1))
print('stack2: ' + str(stack2))
``````

This is just an example of working with lists. You’ll need to restructure everything for your sketch. Of course, you’ll need to add the loops, if statements, event listeners, etc.

1 Like

I am trying to create a conditional inside the main_stack that if the last 3 blocks in stack are same colors, then it should send these 3 last blocks to the scoring_stack. My issue is that I am not sure how to do that, and how to change the x and y values of those blocks so I can change their position to the right of the main_stack.
Any help?

``````#list with values of blocks which will be 0 for red, 1 for green, 2 for blue
main_stack = []
scoring_stack = []
score = 0

def setup():
size(500,500)

def draw():
"""conditionals with drawings so it'll draw the right block according to random value originated by b key
"""
background(255)
print(main_stack)
global x,y, main_stack, scoring_stack, score
x = 150
y = 500
textSize(25)
text("Score:  " + str(score), 300, 50)

for i in main_stack:
if i == int("0"):
y -= 50
fill(255,0,0)
rect(x,y, 50, 50)
elif i == int("1"):
y -= 50
fill(0, 255, 0)
rect(x,y, 50, 50)
elif i == int("2"):
y -= 50
fill(0, 0, 255)
rect(x,y, 50, 50)

def keyPressed():
"""b key is for adding new random blocks, d is to destroy a block, s is to move to the scoring stack the blocks that match colors"""
global x,y, main_stack, scoring_stack, score
if key == "b":
score -= 1
main_stack.append(int(random(0,3)))

if key== "d" and len(main_stack)> 0:
score -= 1
main_stack.pop()
if key == "s":
x+=100
y= 500
score += 5
`````````

Processing programs are quite often based on principal: Clear screen and then redraw it, all this inside draw()-function. It works nicely with moving elements. And it should work with your program too. This model is dependent on some global variables that store the current situation. Actually model could be formulated as

clear
update variables (this causes the change between draw()-calls)
draw graphics

You have some elements there already. You have stacks that store current situation and
by manipulating them you can do the update.
after updates are done (events from mouse or keyboard can be part of updates) draw all the rectangles using data in those stacks.

Take a look at examples in processing.org to get hang of this way of working.

1 Like