This script creates 5 instances of a sphere object, and allows to move 2 of them.
I would like to set the color of those spheres which are less than 3 distance from each
other to red, others should be green. My problem is that it is not working for all of the spheres.
It should be like this:
from direct.showbase.ShowBase import ShowBase
class Game(ShowBase):
def __init__(self):
super().__init__()
self.set_background_color(0.1, 0.1, 0.1, 1)
self.cam.setPos(10, -60, 0)
self.taskMgr.add(self.update, "update")
self.sphere = self.loader.loadModel("models/sphere")
for i in range(5):
self.spheres = self.render.attachNewNode("Spheres" + str(i + 1))
self.spheres.setPos(i * 5, 0, 0)
self.sphere.instanceTo(self.spheres)
def update(self, task):
is_down = self.mouseWatcherNode.is_button_down
for x in range(1, 3):
spheres_a = self.render.get_child(x)
if is_down("w"):
spheres_a.set_z(spheres_a, 0.1)
if is_down("s"):
spheres_a.set_z(spheres_a, -0.1)
if is_down("a"):
spheres_a.set_x(spheres_a, -0.1)
if is_down("d"):
spheres_a.set_x(spheres_a, 0.1)
for x in range(3, 6):
spheres_b = self.render.get_child(x)
distance = spheres_b.getDistance(spheres_a)
if distance < 3:
spheres_b.setColor(1, 0, 0, 1)
spheres_a.setColor(1, 0, 0, 1)
else:
spheres_b.setColor(0, 1, 0, 1)
spheres_a.setColor(0, 1, 0, 1)
# for x in range(1, 6):
# spheres_all = self.render.get_child(x)
# distance = spheres_all.getDistance(spheres_all)
# if distance < 3:
# spheres_all.setColor(1, 0, 0, 1)
# else:
# spheres_all.setColor(0, 1, 0, 1)
return task.cont
Game().run()