Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

python - I need to add text to my rectangles, how would I do this?

I need to draw text into the buttons, which can be seen as the four smaller rectangles in my program, alongside this I need to draw text onto the title as well. I am unsure on how to do this, as the structure of my program, is different to others that I have seen.

Followed other questions, and the answer they've received in an attempt to influence mine.

import pygame
import sys

def main():
    pygame.init()
    clock = pygame.time.Clock()
    fps = 60
    size = [700, 600]
    bg = [255, 255, 255]
    font = pygame.font.Font('freesansbold.ttf', 32) 

    screen = pygame.display.set_mode(size)

    black = (0, 0, 0)

    button = pygame.Rect(400, 400, 250, 125) 
    button2 = pygame.Rect(50, 400, 250, 125)
    button3 = pygame.Rect(400, 250, 250, 125)
    button4 = pygame.Rect(50, 250, 250, 125)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False


            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = event.pos  # gets mouse position

                # checks if mouse position is over the button

                if button.collidepoint(mouse_pos):
                    # prints current location of mouse
                    print('Instructions'.format(mouse_pos))

                if button2.collidepoint(mouse_pos):
                    # prints current location of mouse
                    print('Controls'.format(mouse_pos))

                if button3.collidepoint(mouse_pos):
                    # prints current location of mouse
                    print('Information'.format(mouse_pos))

                if button4.collidepoint(mouse_pos):
                    # prints current location of mouse
                    print('Start Game'.format(mouse_pos))


        screen.fill(bg)

        pygame.draw.rect(screen, black, (button))  # draw button
        pygame.draw.rect(screen, black, (button2))
        pygame.draw.rect(screen, black, (button3))
        pygame.draw.rect(screen, black, (button4))
        pygame.draw.rect(screen, black, (50, 25, 600, 200))

        pygame.display.update()
        clock.tick(fps)

    pygame.quit()
    sys.exit


if __name__ == '__main__':
    main()

I expect the buttons to have text on them, so in the future when I click on them they will open a new window.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use pygame.font, the you've to render the text by pygame.font.Font.render:

e.g.

 red = (255, 0, 0)
 button = pygame.Rect(400, 400, 250, 125) 
 text = font.render("button 1", True, red)

The result is a pygame.Surface, which can be .blit to the center of the rectangular button area:

pygame.draw.rect(screen, black, button) 
textRect = text.get_rect()
textRect.center = button.center
screen.blit(text, textRect)

The other option is to use pygame.freetype:

e.g.

import pygame.freetype
ft_font = pygame.freetype.SysFont('Times New Roman', 32)

An to render the text directly to the screen by pygame.freetype.Font.render_to

text2 = "button 2"
textRect2 = ft_font.get_rect("button 2")
pygame.draw.rect(screen, black, button2)
textRect2.center = button2.center
ft_font.render_to(screen, textRect2, text2, red)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...