1

I'm currently new to pygame and am wondering how to write the positioning of a rectangle for it to print "start"

My loop so far is

def game_intro():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEMOTION:
                for button in buttons:
                    if button[1].collidepoint(event.pos):
                        button[2] = HOVER_COLOR
                    else:
                        button[2] = BLACK

                        screen.blit(bg, (0, 0))
        for text, rect, color in buttons:
            pygame.draw.rect(screen, color, rect)
            screen.blit(text, rect)
            if event.type == pygame.MOUSEBUTTONDOWN:
                mx, my = pygame.mouse.get_pos()

and my positioning and sizing of my rectangle is

rect1 = pygame.Rect(300,300,205,80)
rect2 = pygame.Rect(300,400,205,80)
rect3 = pygame.Rect(300,500,205,80)
Cian Mc
  • 49
  • 7
  • How are `buttons` defined? Or is that what you're asking? – snipsnipsnip Aug 01 '18 at 02:35
  • basically what im trying to do is to `print ('start')` when the mouse clicks over the start button i have already drawn. the start button is defined as `rect1`. – Cian Mc Aug 01 '18 at 03:28

2 Answers2

0

Since you have defined the rects as separate variables, you can check if a specific one is colliding with the mouse position by calling the pygame.Rect.collidepoint method in the event loop:

def game_intro():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:  # 1 = left button, 2 = middle, 3 = right.
                    # `event.pos` is the mouse position.
                    if rect1.collidepoint(event.pos):
                        print('Start')

If you only have the rects (buttons) in a list and no separate variables for the buttons, you would have to assign a callback function to each of them, use a for loop to iterate over the buttons, check if one collides and then call the callback function (see addendum 1 and 2 of this answer).

skrx
  • 19,980
  • 5
  • 34
  • 48
0

You should create a seperate function for buttons:

def button(x, y, w, h, inactive, active, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        gameDisplay.blit(active, (x, y))      #Images are a bit easier to use
        if click[0] == 1 and action is not None:
              print("start")
    else:
        gameDisplay.blit(inactive, (x, y))

After you have done that, you can call it like this:

#For Example
button(100, 350, 195, 80, startBtn, startBtn_hover, game_loop)

Here is what each parameter means:

  • x: x-coordinate of button
  • y: y-coordinate of button
  • w: width of button
  • h: height of button
  • active: picture of button when the mouse is hovering over it
  • inactive: picture of button when it is idle
IT_Guys
  • 27
  • 4