OK, I have here 4 cubes that light up when you either mouse click on or press
the numbers 1 to 4 (on the keyboard, not the number pad). I have it to where
when you activate one cube, the others will not be when pressing another key
or clicking on another cube simultaneously. In other words, the other cubes
will be locked out when one is activated. Otherwise, things will get messed up.
Now when I click the mouse while the cursor is off the cubes and then drag it
over a cube, it will activate the active block in the Python script and light
up the cube. That too would mess things up.
Here’s where I need help. I need it to where any of the cubes do not activate by
clicking the mouse off a cube and then dragging the cursor over the cube while it’s
pressed down. I have tried nested and compound boolean if statements to no avail.
I’m fairly new to Python so the code will look messy and stuff. But at least it
it works that way. Hopefully the comments I put there will help you understand it
better. I’ll worry about code simplicity and efficiency later.
# MultiObject42.py
import bge
from bge import render, logic, events
render.showMouse (1)
cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner
keyboard = bge.logic.keyboard
mouse = bge.logic.mouse
mouseOverOne = cont.sensors ["mouseOverOne"]
mouseOverTwo = cont.sensors ["mouseOverTwo"]
mouseOverThree = cont.sensors ["mouseOverThree"]
mouseOverFour = cont.sensors ["mouseOverFour"]
one = scene.objects ["one"]
two = scene.objects ["two"]
three = scene.objects ["three"]
four = scene.objects ["four"]
one.color = [0, .7, 0, True]
two.color = [0, .7, 0, True]
three.color = [0, .7, 0, True]
four.color = [0, .7, 0, True]
own["tapLock"] = False # to prevent re-tap
leftClick = bge.logic.KX_INPUT_JUST_ACTIVATED == mouse.events [bge.events.LEFTMOUSE]
leftHold = bge.logic.KX_INPUT_ACTIVE == mouse.events [bge.events.LEFTMOUSE]
oneKeyHold = bge.logic.KX_INPUT_ACTIVE == keyboard.events [bge.events.ONEKEY]
oneKeyTap = bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events [bge.events.ONEKEY]
twoKeyHold = bge.logic.KX_INPUT_ACTIVE == keyboard.events [bge.events.TWOKEY]
twoKeyTap = bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events [bge.events.TWOKEY]
threeKeyHold = bge.logic.KX_INPUT_ACTIVE == keyboard.events [bge.events.THREEKEY]
threeKeyTap = bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events [bge.events.THREEKEY]
fourKeyHold = bge.logic.KX_INPUT_ACTIVE == keyboard.events [bge.events.FOURKEY]
fourKeyTap = bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events [bge.events.FOURKEY]
# prevents re-tap when click-holding a cube and pressing a corresponding key or vice-versa
if oneKeyHold or leftHold and mouseOverOne.positive:
own["tapLock"] = True
if twoKeyHold or leftHold and mouseOverTwo.positive:
own["tapLock"] = True
if threeKeyHold or leftHold and mouseOverThree.positive:
own["tapLock"] = True
if fourKeyHold or leftHold and mouseOverFour.positive:
own["tapLock"] = True
if own["lockOne"] == False: # other cubes are locked out when key pressing or click any other cube simultaneously
# this block does tap and will do more than what is listed here
if oneKeyTap and own["tapLock"] == False or leftClick and mouseOverOne.positive and own["tapLock"] == False:
one.color = [0, 1, 0, True]
print("one tapped")
own["lockTwo"] = True # locks out cube 2
own["lockThree"] = True # locks out cube 3
own["lockFour"] = True # locks out cube 4
# this block keeps the cube lit while the mouse or key button is down
if oneKeyHold or leftHold and mouseOverOne.positive:
one.color = [0, 1, 0, True]
print("one active #")
own["lockTwo"] = True
own["lockThree"] = True
own["lockFour"] = True
else: # resets the lock out booleans
own["lockTwo"] = False
own["lockThree"] = False
own["lockFour"] = False
# the following does the same with cubes 2 to 4
if own["lockTwo"] == False:
if twoKeyTap and own["tapLock"] == False or leftClick and mouseOverTwo.positive and own["tapLock"] == False:
two.color = [0, 1, 0, True]
print("two tapped")
own["lockOne"] = True
own["lockThree"] = True
own["lockFour"] = True
if twoKeyHold or leftHold and mouseOverTwo.positive:
two.color = [0, 1, 0, True]
print("two active ##")
own["lockOne"] = True
own["lockThree"] = True
own["lockFour"] = True
else:
own["lockOne"] = False
own["lockThree"] = False
own["lockFour"] = False
if own["lockThree"] == False:
if threeKeyTap and own["tapLock"] == False or leftClick and mouseOverThree.positive and own["tapLock"] == False:
three.color = [0, 1, 0, True]
print("three tapped")
own["lockOne"] = True
own["lockTwo"] = True
own["lockFour"] = True
if threeKeyHold or leftHold and mouseOverThree.positive:
three.color = [0, 1, 0, True]
print("three active ###")
own["lockOne"] = True
own["lockTwo"] = True
own["lockFour"] = True
else:
own["lockOne"] = False
own["lockTwo"] = False
own["lockFour"] = False
if own["lockFour"] == False:
if fourKeyTap and own["tapLock"] == False or leftClick and mouseOverFour.positive and own["tapLock"] == False:
four.color = [0, 1, 0, True]
print("four tapped")
own["lockOne"] = True
own["lockTwo"] = True
own["lockThree"] = True
if fourKeyHold or leftHold and mouseOverFour.positive:
four.color = [0, 1, 0, True]
print("four active ####")
own["lockOne"] = True
own["lockTwo"] = True
own["lockThree"] = True
else:
own["lockOne"] = False
own["lockTwo"] = False
own["lockThree"] = False
This “game” is only a sample.
Attachments
MultiObjectTest42.blend (541 KB)