0

I was trying to make a button, the code works but I was trying to figure out how you can make the rectangle smaller and move it to the top left corner, I was also wondering how you can make multiple buttons. I would recommend if you could run the code in pycharm, and put the in the code to change the position.

Button Code:

import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
win.fill((255, 255, 255))

#START
class button():
   def __init__(self, color, x, y, width, height, text=''):
       self.color = color
       self.x = x
       self.y = y
       self.width = width
       self.height = height
       self.text = text   


def draw(self, win, outline=None):
    # Call this method to draw the button on the screen
    if outline:
        pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

    pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

    if self.text != '':
        font = pygame.font.SysFont('comicsans', 60)
        text = font.render(self.text, 1, (0, 0, 0))
        win.blit(text, (
        self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))

def isOver(self, pos):
    # Pos is the mouse position or a tuple of (x,y) coordinates
    if pos[1] > self.x and pos[1] < self.x + self.width:
        if pos[1] > self.y and pos[1] < self.y + self.height:
            return True

def redrawWindow ():
   win.fill((255, 255, 255))
   greenButton.draw(win, (0, 0, 0))


run = True
greenButton = button((0, 255, 0), 150, 225, 250, 100, 'Start')
while run:
    redrawWindow()
    pygame.display.update()

    for event in pygame.event.get():
          pos = pygame.mouse.get_pos()

    if event.type == pygame.QUIT:
        run = False
        pygame.quit()
        quit()

    if event.type == pygame.MOUSEBUTTONDOWN:
        if greenButton.isOver(pos):
            print('clicked')

    if event.type == pygame.MOUSEMOTION:
        if greenButton.isOver(pos):
            greenButton.color = (0,100,0)
        else:
            greenButton.color = (0, 255, 0)
ruohola
  • 19,113
  • 6
  • 51
  • 82
Blue
  • 21
  • 4
  • Changing position and size is quite easy because you used a class. In your `__init__` function, you specify these values as parameters. In this: `((0, 255, 0), 150, 225, 250, 100, 'Start')` line here, you describe all the parameters. Its colour is `(0, 255, 0)`. It's position is `(150, 225)`. Its size is `250x100`, and its text is `Start` – User 12692182 Feb 23 '21 at 13:38
  • Thank You!, but is there any way that I can add multiple buttons, because I copy pasted the first button code then I changed the greenButton variable to redButton, and then changed the position of the seccond button so it would not overlap, but the second button does not show up even after doing this. – Blue Feb 23 '21 at 14:09
  • Yes, what you just described would work, however you still need to draw, pass mouse events & update the second button, just as you did with the first button – User 12692182 Feb 23 '21 at 14:11
  • And a tip: the event loop runs super fast, and during that period, there is no time to move the mouse. Therefore, there is no need to check the position of the mouse every time the event loop runs, so you can move this line: `pos = pygame.mouse.get_pos()` out of the event loop. This will reduce the time the event loop takes to run. Having lengthy processes run in the event loop is a common error that results in a lot of lag, delayed processing, and much more. – User 12692182 Feb 23 '21 at 14:22
  • Okay, I wrote the position of the button inside `if greenButton.isOver (200, 2)`, but its telling me `TypeError: isOver() takes 2 positional arguments but 3 were given` – Blue Feb 23 '21 at 15:05
  • The first argument of `isOver` is `self`. The second should be `pos`, in the form of `(x, y)`. You are passing `x` and `y` separately, meaning it gets 3 arguments, `self`, `x`, `y`, instead of the two it requires. Pack `x` and `y` into a tuple, and pass `isOver` the tuple, instead of `x` and `y` individually. – User 12692182 Feb 23 '21 at 15:24

0 Answers0