I am a pygame novice and this is my first post on stackoverflow (hello!). I’m hoping you’ll be able to help me with a problem I’m having creating a board for a simple puzzle game. The board consists of a 3x3 grid of buttons which, when pressed, change the colour of other buttons in the grid. The problem I’m having is how best to reference each button so that a mouse click can be identified and I can control the colour change of the other buttons based on this click.
In the absence of code, I can provide the following information which I hope will add clarity to my problem (I can try and tidy and provide the test code I have currently written if it helps).
I have imported a button image and have managed to create a grid by blitting it 9 times in a loop, referencing a co-ordinate list:
For i in range(9):
gamedisp.blit(button,coordList[i])
So I have the grid in place but how do I specify which button is which? My first thought was to assign them to variables button0 – button8 but a brief search here and elsewhere makes it clear that this is not good practice. So what’s the alternative? How can I store and reference the information in such a way that I can perform the following?
E.g. The top-left button has been clicked therefore change the colour of all buttons in the middle row. I'm pretty sure I can handle the colour change itself, its just the object referencing I'm struggling with.
Thanks in advance for your help. Please let me know if I can provide further detail.