I'm trying to make a small game that is composed of blocks that should interact with the player when clicked. To try to make this work I've made a matrix that stores the blocks so they can be easily accessed. I know it works in normal python with all kind of objects, as explained in this Creating a list of objects in Python question (python matrixes being lists of lists).
The problem is that the methods of the object are not detecting clicking properly. Has anyone idea of why is this? Is it a bug on processing?
Here´s the code if you need it:
class Block():#creates an object that senses clicking input by the user
def __init__(self,position=(),dimentions=(),rgb=(0,0,0)):
#set variables
self.x=position[0]
self.y=position[1]
self.w=dimentions[0]
self.h=dimentions[1]
#draw on screen:
fill(rgb[0],rgb[1],rgb[2])
rect(position[0],position[1],dimentions[0],dimentions[1])
def IsPressed(self):# senses if mouse is within the block and if it is pressed, if both are true output is true, else it´s false
if mouseX in range(self.x,self.x+self.w) and mouseY in range(self.y, self.y+self.h) and mousePressed==True:
return True
else:
return False
def createMatrix(matrix):#for loop function to create the matrix and give all blocks a location
r=-1#int for rows, required for the location assignment to work properly
c=-1#int for columns (items in row)rows, required for the location assignment to work properly
#nested loop:
for row in matrix:
r=r+1
c=-1
for item in row:
c=c+1
matrix[r][c]=Block((r*10,c*10),(10,10),(255,30,200))
def setup():
global blockgrid #allows usage of the blockgrid in the draw function
blockgrid=[[0]*3]*3 #creates the matrix in which blocks are stored
createMatrix(blockgrid)
print(blockgrid)
def draw():
#test to see if sensing works:
if blockgrid[1][1].IsPressed():
#blockgrid[1] [1] is the center one!!
print(True)
else:
print(False)