ashley coombes
ashley coombes

Reputation: 15

Pygame - add text to class instance of a rect

I am attempting to add text to class instances of a rect.

I have been able to do this by setting the "font" variable outside of the class and putting the desired text in "font.render_to" within the draw funtion but this means the text will be the same for all buttons as it is set within the class.

I'd like to set different text for each button specified in the class instance (button1, button2, button3) as shown in the below code but im getting the following error:

Traceback (most recent call last):
File "path_to_file/test.py", line 30, in <module>
button1 = Button(10, 10, 100, 50, 'One')
File "path_to_file/test.py", line 23, in __init__
self.font = font.pygame.freetype.SysFont('Arial', 25)
AttributeError: 'str' object has no attribute 'pygame'.

Code:

import pygame
import sys
import pygame.freetype


pygame.display.init()
pygame.freetype.init()
width = 300
height = 350
bg = (255, 255, 255)

# Sets the window size
screen = pygame.display.set_mode((width, height))

# Sets the background colour to white
screen.fill(bg)


# Button class - All buttons are created using the below parameters
class Button:

    def __init__(self, rect_x, rect_y, rect_width, rect_height, font):
        self.rect_x = rect_x
        self.rect_y = rect_y
        self.rect_width = rect_width
        self.rect_height = rect_height
        self.font = font.pygame.freetype.SysFont('Arial', 25)

# Draw function - Creates the rectangle and adds text
    def draw(self):
        pygame.draw.rect(screen, (200, 200, 200), (self.rect_x, self.rect_y, self.rect_width, self.rect_height))
        self.font.render_to(screen, (42, 25), self.font, bg)


# Class instances - Defines button size and location in the PyGame window
button1 = Button(10, 10, 100, 50, 'One')
button2 = Button(10, 70, 100, 50, 'Two')
button3 = Button(10, 130, 100, 50, 'Three')

# game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    button1.draw()
    button2.draw()
    button3.draw()
    pygame.display.update()

Upvotes: 1

Views: 443

Answers (1)

Rabbid76
Rabbid76

Reputation: 211116

The 5th argument of the constructor is the text. You have to store the text in an attribute and render the text. font.pygame.freetype.SysFont('Arial', 25) doesn't make any sense. The font object is constructed by pygame.freetype.SysFont('Arial', 25). The target position of render_to depends on the position of the button:

class Button:
    def __init__(self, rect_x, rect_y, rect_width, rect_height, text):
        self.rect_x = rect_x
        self.rect_y = rect_y
        self.rect_width = rect_width
        self.rect_height = rect_height   
        self.text = text   
        self.font = pygame.freetype.SysFont('Arial', 25)

    def draw(self):
        pygame.draw.rect(screen, (200, 200, 200), 
            (self.rect_x, self.rect_y, self.rect_width, self.rect_height))     
        self.font.render_to(screen, (self.rect_x + 42, self.rect_y + 25), self.text, bg)

Upvotes: 1

Related Questions