0

I am trying to develop a Keno-style game within pygame but instead of creating 80 separate buttons, I was trying to make them in a loop.

I've attached the button loop as well as the button-generating loop.

def button(self, msg, x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    # print(click)
    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(self.gameDisplay, ac, (x, y, w, h))
        if click[0] == 1 and action is not None:
            action()
    else:
        pygame.draw.rect(self.gameDisplay, ic, (x, y, w, h))
    smallText = pygame.font.SysFont("calibri", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ((x + (w / 2)), (y + (h / 2)))
    self.gameDisplay.blit(textSurf, textRect)
def button_gen(self):
    x_val = 100
    y_val = 100
    num = 1

    for i in range (100):
        while y_val <= 800:
            while x_val <= 1000:
                self.button(f"{num}", 100, 100, x_val, y_val, green, bright_green, self.exitGame)
                num += 1
                x_val += 100
            x_val = 100
            y_val += 100

My code displays one giant 1000 x 800 (the width and height of the entire button grid) button labelled '80'. Instead, I wish the loop should create 80 separate different buttons (labelled 1 to 80), set up in a 1000 x 800 grid.

0 Answers0