1
votes

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.

1
Welcome to Stack Overflow! Please add your code to the question (a minimal, complete example). Do you already have experience with classes/object oriented programming? - skrx
Hi. Thank you for your response. I will add code later when I have it available. I am learning about classes and OOP at the moment and I think I'm getting close to cracking this problem myself after some frantic searching. I think I need to create a Button class including x, y, image, etc. I can initiate this 9 time to populate a list, buttons[]. Once I have this I can reference each element based on its index and identify a click using a collision with the get_rect() method. Am I getting warm? The details are still a bit patchy but I will post the code (or solution) later. Thanks again. - Pilsmer
Yes, sounds like you're on the right track. - skrx

1 Answers

1
votes

I have managed to find a solution to my own problem and have included my code in the hope that it may help others in the future. I have created a Button class and looped through it 9 times to create the grid. The class has a method 'controls' which is a list of button indexes that are changed by a button press. I can access these buttons by using buttons[i].controls.

As mentioned in the original post I am a python and pygame novice so if there are any errors or inefficiencies in my code then please feel free to post improvements. I have been using Python Programming for the Absolute Beginner (Third Edition) by Michael Dawson and pythonprogramming.net among other sources in my learning.

##import and initiate pygame
import pygame as pg

pg.init()

##Set display dimensions and the button size as a function of these dimensions.
display_width = 400
display_height = 600
buttonSize = (display_width/4)

##Create game display surface.
gameDisplay = pg.display.set_mode((display_width,display_height))

white = (255,255,255)

##posList holds nine pairs of X,Y multipliers. These are multiplied by buttonSize to draw the grid. 
posList = ((0.25,0.25), (0.25,1.5), (0.25,2.75), (1.5,0.25), (1.5,1.5), (1.5,2.75), (2.75,0.25), (2.75,1.5), (2.75,2.75))

##board is a list of nine lists. Each nested list holds the butttons that switched by each button press.
board = ((0,1,3),(1,0,2,4),(2,1,5),(3,4),(4,1,7),(5,4),(6,3,7),(7,4,6,8),(8,5,7))

##I import the button image
redSquare = pg.image.load('Button Image.png').convert()
redSquare = pg.transform.scale(redSquare, (int(buttonSize), int(buttonSize)))

##The Button class holds coordinate, image and controls attributes
class Button:
    def __init__(self, x, y, image, controls, green = False):
        self.x = x
        self.y = y
        self.image = image
        self.isgreen = green
        self.controls = controls

##It has a blit method.
    def buttonBlit(self):
        gameDisplay.blit(self.image, (self.x, self.y))

##I create an empty list called buttons.
buttons = []

##I populate buttons by looping through the Button class 9 times.
for i in range(9):   
    buttons.append(Button(posList[i][0] * buttonSize, posList[i][1] * buttonSize, redSquare, board[i]))

##The blit method draws the buttons.
gameDisplay.fill(white)
for button in buttons:
    Button.buttonBlit(button)

pg.display.update()

##I can reference each button by using the index in the buttons list. Furthermore, I can use buttons[i].controls to perform the actions of a button press.