0

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)
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    Please see [mvce]. I don't think the block of code you pasted is enough to reproduce your issue. Can you provide a smaller example where you use the bare minimum amount of code to reproduce the issue you describe? – campellcl Aug 29 '18 at 01:12
  • It would be nice if you could provide a `main` function where the commands are called. ;) –  Aug 29 '18 at 01:22

1 Answers1

2

blockgrid=[[0]*3]*3 is a well-known bug. You haven't created a 3x3 matrix. instead, you have created a list of length 3 which contains 3 references to exactly the same list of length 3. No matter what you do, blockgrid[0][i] = blockgrid[1][i] = blockgrid[2][i] (for i = 0,1,2) so you are continually overwriting some of your blocks.

Instead, use something like blockgrid=[[0]*3 for _ in range(3)].

For testing purposes, I recommend using e.g. size(300,300) in setup(), making the blocks bigger so that you could be sure which one you are clicking. Your current blocks are not much bigger than the tip of the mouse cursor. More generally -- rather than hard-wiring in the size of the blocks, why not calculate them in terms of the sketch width and height?

John Coleman
  • 51,337
  • 7
  • 54
  • 119