Hello community,
this is my first attempt to use the blender game engine and my first steps in python
(I know how to program, used Java a lot before…)
So I want to create a little tetris clone. (with Blender 2.55 beta)
(Explanation might be a little long, but necessary)
So far i “modelled” the bricks as groups of 4 cubes each.
They should move together, and stay in shape when colliding with the
ground or already existing bricks.
But when there is a horizontal row of cubes the according cubes
should disappear and the “rest” of the brick should drop down, the
remaining cubes should be together again then.
I already tried it with parenting, but i do not remember the exact
settings now, whether the empty was static and the cubes dynamic or
the other way round.
This gave odd results up to now. Maybe i will retry it later.
Well, NOW i am experimenting with groups of cubes.
When i add a group instance object (the brick) the cubes are
automatically added to the game engine as well, they are all dynamic.
They fall together and when hitting the ground the brick “collapses” to its parts
So far so good.
Now i want them to stay in shape, so when hitting the ground i want do:
cube.suspendDynamics()
But the problem is to “find” the correct cube instances belonging to
the currently adde group instance. (they are NOT children)
The KX_GameObject has no “group support” whatsoever, so I try to
subclass it in a separate script (or module?) called “Groups.py”
import bge
class GroupObject(bge.types.KX_GameObject):
def __init__(self, obj):
if type(obj) == bge.types.KX_GameObject:
bge.types.KX_GameObject.__init__(obj)
self.__group = None
self.__members = []
def __del__(self):
self.__group = None
del self.__members[:]
del self.__members
#del self.__gameObject
def addMember(self, groupObject):
if type(groupObject) == GroupObject:
self.__members.append(groupObject)
groupObject.__group = self
def getMembers(self):
return self.__members
members = property(getMembers)
def getGroup(self):
return self.__group
group = property(getGroup)
The purpose of this class is to give access to the group members.
My “main” script (its a mess at this point, however)
import bge
import bpy
import copy
from Groups import GroupObject
from List import mylist
cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner
timer = cont.sensors["Always"]
#keyb = cont.sensors["Near"]
if hasattr(bge.logic, "pX") == 0:
bge.logic.pX = -4
def createStone():
#store current object!
currObj = None
currAct = None
# bge.logic.id = bge.logic.id + 1
# print(bge.logic.id)
#randomize stone type 0Q 1I 2S 3L
typeRnd = cont.actuators["Type"]
cont.activate(typeRnd)
type = own.get("type")
print(type)
if type == 0:
currAct = "QStone" #cont.actuators["QStone"]
elif type == 1:
currAct = "IStone" #cont.actuators["IStone"]
elif type == 2:
currAct = "SStone" #cont.actuators["SStone"]
elif type == 3:
currAct = "LStone" #cont.actuators["LStone"]
elif type == 4:
currAct = "TStone" #cont.actuators["TStone"]
# cont.activate(currAct)
# if currAct.objectLastCreated == None:
# return None
# else:
# print("EQUAL: " + str(currAct.object ==
currAct.objectLastCreated))
currObj = GroupObject(scene.addObject(currAct, currAct, 0))
l = mylist(scene.objects)
members = bpy.data.groups[currAct].objects
for member in members:
child = scene.objects[member.name]
index = l.lastIndex(child)
child = scene.objects[index]
currObj.addMember(GroupObject(child))
#randomize posX
posXRnd = cont.actuators["PosX"]
cont.activate(posXRnd)
posX = own.get("posX")
pos = currObj.worldPosition
# if type == 1:
# pos[0] = posX * 2 - 1
#
# else:
# pos[0] = posX * 2
pos[0] = bge.logic.pX * 4
currObj.worldPosition = pos
bge.logic.pX += 1
print (currObj.worldPosition)
createStone()
#randomize rotX
#rotXRnd = cont.actuators["RotX"]
#cont.activate(rotXRnd)
#rotX = own.get("rotX")
#angleX = rotX * 180
#orientation = currObj.worldOrientation
#print(orientation)
#orientation[
This script should randomly create, place and rotate the bricks after
creating them
It responds to an “Always” sensor.
Another script (snippet) “Stop.py”:
def stop(obj):
# print(type(obj))
# obj = GroupObject(obj)
for member in obj.members:
print(member)
member.suspendDynamics()
if near.positive:
stop(near.hitObject)
This script should access the group members and stop them from falling when the brick
is near to the ground (indeed, near.hitObject WAS from type GroupObject
but it seemed NOT to belong to the scene any more whatsoever)
Now the problem: In GroupObject, when I want to do print(obj) or use it
it tells me that Blender already freed that object? :spin:
OK, I sat several days at that problem… Hope somebody can help me.
Thanks in advance.
Oh, maybe adding the WHOLE .blend might help…
quadblox3.blend (467 KB)