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)
2
  • 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? Commented Aug 29, 2018 at 1:12
  • It would be nice if you could provide a main function where the commands are called. ;) Commented Aug 29, 2018 at 1:22

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

5 Comments

I disagree that it's a bug, I think it's a feature... potato / potato I guess :)
@en_Knight I don't think that Python has a bug here -- but it is a bug if you write code like that while misunderstanding the semantics
ah I see what you meant, my mistake. Agreed
thanks. I had no idea python had been coping the lists. I thought I was creating 3*3 spaces that had 0 inside and then writing over them my blocks.
and also, do the lists inside 'blockgrid' refer to the same location in memory, so overwriting one makes the others change?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.