0

What i want to happen is that when button 3 (b3) is pressed that it draws a rectangle that says permanently, right now the rectangle only stays for one frame and i don't know how i can fix it. I tried making a variable that changed to True when the button was pressed so that i draw the rectangle but i couldn't get that to work either.

import pygame
import time

screen = pygame.display.set_mode((400, 400))

black = (8, 8, 8)
grey = (108, 108, 108)
white = (255, 255, 255)
stone_color = (99, 99, 99)
wood_color = (136, 88, 33)
house_color = (31, 36, 179)
cooldown_color = (147, 147, 147)

wood_amount = 0
stone_amount = 0
house_amount = 0

class button:
    def __init__(self, buttonstartx, buttonstarty, buttonlenghtx, buttonlenghty, color):
        self.buttonstartx = buttonstartx
        self.buttonstarty = buttonstarty
        self.buttonlenghtx = buttonlenghtx
        self.buttonlenghty = buttonlenghty
        self.color = color

        pygame.draw.rect(screen, color, (buttonstartx, buttonstarty, buttonlenghtx, buttonlenghty))

class cooldown:
    def __init__(self, b, secs):
        global coolactive
        self.b = b
        self.secs = secs
            pygame.draw.rect(screen, cooldown_color, (b.buttonstartx - 100, b.buttonstarty, b.buttonlenghtx, b.buttonlenghty))

def housebuild():
    global wood_amount
    global house_amount
    if wood_amount > 4:
        wood_amount = wood_amount - 5
        house_amount = house_amount + 1


running = True
while running:
    pygame.init()
    screen.fill(black)
    pygame.font.init()
    myfont = pygame.font.SysFont('default', 30)
    woodtext = myfont.render(("Wood : " + str(wood_amount)), False, (255, 255, 255))
    screen.blit(woodtext, (10, 10))
    stonetext = myfont.render(("Stone : " + str(stone_amount)), False, (255, 255, 255))
    screen.blit(stonetext, (10, 30))
    housetext = myfont.render(("Houses : " + str(house_amount)), False, (255, 255, 255))
    screen.blit(housetext, (10, 50))
    # button def

    b1 = button(75, 300, 50, 50, wood_color)
    b2 = button(275, 300, 50, 50, stone_color)
    b3 = button(350, 350, 50, 50, white)
    for event in pygame.event.get():

        musx, musy = pygame.mouse.get_pos()
        if event.type == pygame.QUIT:
            pygame.quit()

        # Button collision detection
        if event.type == pygame.MOUSEBUTTONDOWN:
            if b1.buttonstartx < musx < b1.buttonstartx + b1.buttonlenghtx and b1.buttonstarty < musy < b1.buttonstarty + b1.buttonlenghty:
                wood_amount = wood_amount + 1

            if b2.buttonstartx < musx < b2.buttonstartx + b2.buttonlenghtx and b2.buttonstarty < musy < b2.buttonstarty + b2.buttonlenghty:
                stone_amount = stone_amount + 1

            if b3.buttonstartx < musx < b3.buttonstartx + b3.buttonlenghtx and b3.buttonstarty < musy < b3.buttonstarty + b3.buttonlenghty:
                housebuild()



    pygame.display.flip()

'''
Alex GkG
  • 13
  • 2
  • 2
    add it to list, in the main loop, iterate over that list and draw the rectangle – Matiiss Dec 23 '21 at 23:00
  • Alternatively you can create [`gygame.sprite.Sprite`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite) object and add it to a [`pygame.sprite.Group`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group). Anyway you need to provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Where do you try to draw the rectangle? What is the position, size and color of the rectangle? – Rabbid76 Dec 24 '21 at 10:58

0 Answers0