1

Basically, here is a section of my code in Pygame:

button_text=pygame.font.Font("C:\Windows\Fonts\Another Danger - Demo.otf",35)
    textSurface,textRect=text_objects("Start game",button_text)
    textRect.center=(105,295)
    screen.blit(textSurface,textRect)

This is the text I'd like to turn into a clickable format, so that when someone presses the text, it can run a function, such as running the next thing possible.

Any help would be greatly appreciated.

Thanks.

Arvy
  • 11
  • 2
  • 3
  • 1
    Was [this post](https://gamedev.stackexchange.com/questions/106344/how-do-i-check-if-pressed-on-a-text-in-pygame) not any help, or did you not search before posting this? – Random Davis Apr 26 '17 at 17:57
  • I guess it didn't come up the way I worded my question, thanks for the link though, seems to be the problem I'm having. – Arvy Apr 26 '17 at 18:52

2 Answers2

1

PyGame does not have buttons, so what you can do here is get the mouse cursor position when clicking with pygame.mouse.get_pos(). If the mouse cursor position is inside the text then you know the text was selected.

Here is an example:

import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1000, 700))
clock = pygame.time.Clock()
tx, ty = 250, 250
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
                    pygame.quit()
                    quit()
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            mouse = pygame.mouse.get_pos()
            if mouse[0]in range (tx, tx + 130) and mouse[1] in range (ty, ty + 20):
                print("You clicked on the text.") 
    myfont = pygame.font.SysFont("Marlett", 35)
    textsurface = myfont.render(("Start game"), True, (230, 230, 230))
    screen.blit(textsurface,(tx, ty))
    pygame.display.update()
    clock.tick(60)

In this example, I used tx and ty for sizes but you can use rect, it's the same thing.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
1

Get the rect from the surface that font.render returns and use it for collision detection and the blit position.

import sys
import pygame as pg


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()

    font = pg.font.Font(None, 30)
    text_surface = font.render('text button', True, pg.Color('steelblue3'))
    # Use this rect for collision detection with the mouse pos.
    button_rect = text_surface.get_rect(topleft=(200, 200))

    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    # Use event.pos or pg.mouse.get_pos().
                    if button_rect.collidepoint(event.pos):
                        print('Button pressed.')

        screen.fill((40, 60, 70))
        screen.blit(text_surface, button_rect)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()
skrx
  • 19,980
  • 5
  • 34
  • 48