-1

I'm trying to put buttons in a surface*(screen)* and want to include them in just one class. This is the code:

larguraTela, alturaTela = (640, 480)
tela = pygame.display.set_mode((larguraTela, alturaTela))
verde = (38, 117, 1)

# Rect(left, top, width, height)
#botaoMenu = pygame.Rect(20, 415, 150, 45)
#setaEsquerda = pygame.Rect(10, 280, 45, 45)
#setaDireita = pygame.Rect(580, 280, 45, 45)
#pygame.draw.rect(tela, verde, botaoMenu)
#pygame.draw.rect(tela, verde, setaDireita)
#pygame.draw.rect(tela, verde, setaEsquerda)

class Button:

    def __init__(self, left, top, width, height):
        self.button = pygame.Rect(left, top, width, height)

    def draw(self, screen, color, botao):
        self.rect = pygame.draw.rect(screen, color, botao)

buttonMenu = Button(20, 415, 150 ,45)
buttonMenu.draw(tela, verde, buttonMenu)

The error is:

Traceback (most recent call last):
  File "/home/rafael/ibi/Rafael/Código organizado/menu_equipe_otimizado.py", line 31, in <module>
buttonMenu.draw(tela, verde, buttonMenu)    
  File "/home/rafael/ibi/Rafael/Código organizado/menu_equipe_otimizado.py", line 28, in draw
    self.rect = pygame.draw.rect(self, screen, color, botao)
TypeError: argument 1 must be pygame.Surface, not Button
Kurokishin
  • 35
  • 5
  • 1
    It looks like you meant to write `buttonMenu.draw(tela, verde, buttonMenu)`. `botao` is a `Rect` object and has no `draw` method – Ted Klein Bergman Jun 15 '21 at 18:51
  • You accidentally swapped `buttonMenu` and `botao`. It has to be `buttonMenu.draw(tela, verde, botao) ` – Rabbid76 Jun 15 '21 at 18:57
  • This error appears when I changed that line: Traceback (most recent call last): File "/home/rafael/ibi/Rafael/Código organizado/menu_equipe_otimizado.py", line 32, in buttonMenu.draw(tela, verde, buttonMenu) File "/home/rafael/ibi/Rafael/Código organizado/menu_equipe_otimizado.py", line 29, in draw self.rect = pygame.draw.rect(self, screen, color, botao) TypeError: argument 1 must be pygame.Surface, not Button – Kurokishin Jun 15 '21 at 19:00
  • `self.rect = pygame.draw.rect(screen, color, botao)`. See [Pygame Drawing a Rectangle](https://stackoverflow.com/questions/19780411/pygame-drawing-a-rectangle/64629716#64629716) and [`pygame.draw.rect`](http://www.pygame.org/docs/ref/draw.html#pygame.draw.rect) – Rabbid76 Jun 15 '21 at 19:01
  • @Rabbid76 I made some edits bc I messed up some things, please take a look – Kurokishin Jun 15 '21 at 19:19
  • @Kurokishin I've given an answer. Read the answer and my previous comments. – Rabbid76 Jun 15 '21 at 19:20

1 Answers1

1

You don't need the button attribute at all. Pass the color and the rectangle to the constructor of the class. Save the color and rectangle in an attribute and use the attributes to draw the button:

class Button:

    def __init__(self, color, rect):
        self.color = color
        self.rect = rect

    def draw(self, screen):
        pygame.draw.rect(screen, self.color, self.rect)
botao = pygame.Rect(20, 415, 150, 45)
buttonMenu = Button(verde, botao)
buttonMenu.draw(tela) 

Alternatively, you can pass the position and size of the rectangle to the constructor:

class Button:

    def __init__(self, color, left, top, width, height):
        self.color = color
        self.rect = pygame.Rect(left, top, width, height)

    def draw(self, screen):
        pygame.draw.rect(screen, self.color, self.rect)
buttonMenu = Button(verde, 20, 415, 150, 45)
buttonMenu.draw(tela) 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174