0
$\begingroup$

I'd like to ask if somebody might help me with my problem. I try to create 4 objects in BGE. Their color is a random color (Green, Pink, Brown, Blue, Red, Yellow). My problem now: How can I let python know, that two objects shouldn't have the same color?

Name of cubes: solution1, 2, 3 and 4

Name of cubes: solution1, 2, 3 and 4

My scripts for each cube

cube 1

import bge
import random


#I've made different scrips for each cube

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    blue = [0,0,1,True]
    green = [0,1,0,True]
    red = [1,0,0,True]
    yellow = [255,255,0,True]
    brown = [0.207917,0.105,0.030,True]
    pink = [2.55,0.20,1.47,True]

    color = [blue, green, red, yellow, brown, pink]

    own.color = random.choice(color)


main()

cube2

import bge
import random


#I've made different scrips for each cube

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    solution1 = bge.logic.getCurrentScene().objects['solution1']
    solution2 = bge.logic.getCurrentScene().objects['solution2']
    solution3 = bge.logic.getCurrentScene().objects['solution3']
    solution4 = bge.logic.getCurrentScene().objects['solution4']

    blue = [0,0,1,True]
    green = [0,1,0,True]
    red = [1,0,0,True]
    yellow = [255,255,0,True]
    brown = [0.207917,0.105,0.030,True]
    pink = [2.55,0.20,1.47,True]

    color = [blue, green, red, yellow, brown, pink]

    own.color = random.choice(color)

    #Part for solution1

    if solution1 == red:
        color = [blue, green, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == green:
        color = [blue, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == blue:
        color = [green, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == yellow:
        color = [blue, red, green, brown, pink]
        own.color = random.choice(color)

    elif solution1 == pink:
        color = [blue, red, yellow, brown, green]
        own.color = random.choice(color)

    elif solution1 == brown:
        own.color = green
        color = [blue, red, yellow, green, pink]
        own.color = random.choice(color)



main()

cube3

import bge
import random


#I've made different scrips for each cube

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    solution1 = bge.logic.getCurrentScene().objects['solution1']
    solution2 = bge.logic.getCurrentScene().objects['solution2']
    solution3 = bge.logic.getCurrentScene().objects['solution3']
    solution4 = bge.logic.getCurrentScene().objects['solution4']

    blue = [0,0,1,True]
    green = [0,1,0,True]
    red = [1,0,0,True]
    yellow = [255,255,0,True]
    brown = [0.207917,0.105,0.030,True]
    pink = [2.55,0.20,1.47,True]

    color = [blue, green, red, yellow, brown, pink]

    own.color = random.choice(color)

    #Part for solution1

    if solution1 == red:
        color = [blue, green, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == green:
        color = [blue, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == blue:
        color = [green, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == yellow:
        color = [blue, red, green, brown, pink]
        own.color = random.choice(color)

    elif solution1 == pink:
        color = [blue, red, yellow, brown, green]
        own.color = random.choice(color)

    elif solution1 == brown:
        own.color = green
        color = [blue, red, yellow, green, pink]
        own.color = random.choice(color)

    #Part for solution2

    if solution2 == red:
        color = [blue, green, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution2 == green:
        color = [blue, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution2 == blue:
        color = [green, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution2 == yellow:
        color = [blue, red, green, brown, pink]
        own.color = random.choice(color)

    elif solution2 == pink:
        color = [blue, red, yellow, brown, green]
        own.color = random.choice(color)

    elif solution2 == brown:
        own.color = green
        color = [blue, red, yellow, green, pink]
        own.color = random.choice(color)



main()

cube4

import bge
import random


#I've made different scrips for each cube

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    solution1 = bge.logic.getCurrentScene().objects['solution1']
    solution2 = bge.logic.getCurrentScene().objects['solution2']
    solution3 = bge.logic.getCurrentScene().objects['solution3']
    solution4 = bge.logic.getCurrentScene().objects['solution4']

    blue = [0,0,1,True]
    green = [0,1,0,True]
    red = [1,0,0,True]
    yellow = [255,255,0,True]
    brown = [0.207917,0.105,0.030,True]
    pink = [2.55,0.20,1.47,True]

    color = [blue, green, red, yellow, brown, pink]

    own.color = random.choice(color)

    #Part for solution1

    if solution1 == red:
        color = [blue, green, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == green:
        color = [blue, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == blue:
        color = [green, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution1 == yellow:
        color = [blue, red, green, brown, pink]
        own.color = random.choice(color)

    elif solution1 == pink:
        color = [blue, red, yellow, brown, green]
        own.color = random.choice(color)

    elif solution1 == brown:
        own.color = green
        color = [blue, red, yellow, green, pink]
        own.color = random.choice(color)

    #Part for solution2

    if solution2 == red:
        color = [blue, green, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution2 == green:
        color = [blue, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution2 == blue:
        color = [green, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution2 == yellow:
        color = [blue, red, green, brown, pink]
        own.color = random.choice(color)

    elif solution2 == pink:
        color = [blue, red, yellow, brown, green]
        own.color = random.choice(color)

    elif solution2 == brown:
        own.color = green
        color = [blue, red, yellow, green, pink]
        own.color = random.choice(color)

    #Part for solution3

    if solution3 == red:
        color = [blue, green, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution3 == green:
        color = [blue, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution3 == blue:
        color = [green, red, yellow, brown, pink]
        own.color = random.choice(color)

    elif solution3 == yellow:
        color = [blue, red, green, brown, pink]
        own.color = random.choice(color)

    elif solution3 == pink:
        color = [blue, red, yellow, brown, green]
        own.color = random.choice(color)

    elif solution3 == brown:
        own.color = green
        color = [blue, red, yellow, green, pink]
        own.color = random.choice(color)



main()
$\endgroup$
2
  • $\begingroup$ solution1_object is undefined. What is solution1_object? maybe you should defined it as solution1_object = bge.logic.getCurrentScene().objects['objectName'] $\endgroup$ Dec 21, 2017 at 17:22
  • $\begingroup$ this is what I need to know, I'll try it later on $\endgroup$ Dec 21, 2017 at 17:40

1 Answer 1

0
$\begingroup$

To access a object color in game you use own.color. That returns a list [R,G,B,A] You will need to compare each component of the color individuality.

Something like

R = False
G = False
B = False
amount = .1
if other_color[0] > own.color[0] > other_color[0]+amount:
    R = True
if other_color[1] > own.color[1] > other_color[1]+amount:
    G = True
if other_color[2] > own.color[2] > other_color[2]+amount:
    B = True

if R and G and B:
    #colors match do something

But I would simply remove the color from the list after you use it on an object, ensuring that they all have unique colors.

colors = [[1,0,0,1], [0,1,0,1], [0,0,1,1], [1,.75,0,1], [.3,.15,.02,1], [1,.2,1,1]]
random.shuffle(colors)

own.color = colors[0]
colors.pop(0)
$\endgroup$
1
  • $\begingroup$ Maybe I'd show you better what I mean. I'll edit the post. $\endgroup$ Dec 22, 2017 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .