Zgn
Zgn

Reputation: 127

Pygame: font.render() gives "TypeError: Invalid foreground RGBA argument"

I tried to make a total clicks bar with screen.blit() (also i want it to update the text when you click), I tried

text = font.render('Your total clicks are',totalbal, True, WHITE)

but it doesn't work

line 21, in <module>
    text = font.render('Your total clicks are',totalbal, True, WHITE)
TypeError: Invalid foreground RGBA argument

I also tried it with str(totalbal) but doesn't work again.

import pygame, sys, time
from pygame.locals import *

pygame.init()
WHITE = 255,255,255
font = pygame.font.SysFont(None, 44)
baltotal = open("totalbal.txt", "r+")
totalbal = int(baltotal.read())
w = 800
h = 600
screen = pygame.display.set_mode((w,h))
Loop = True
text = font.render('Your total clicks are',totalbal, True, WHITE)
while Loop: # main game loop
    ...

        if event.type == MOUSEBUTTONDOWN: #detecting mouse click
                totalbal += cps
                print("Your total clicks are", totalbal, end="\r")
    ...
    screen.blit(text, (235,557))
    pygame.display.flip()
    pygame.display.update()
    clock.tick(30)

with open("totalbal.txt", "w") as baltotal:
    baltotal.write(str(totalbal))
baltotal.close

pygame.quit()
sys.exit()

Upvotes: 1

Views: 206

Answers (1)

Rabbid76
Rabbid76

Reputation: 210890

You have to concatenate the strings

text = font.render('Your total clicks are ' + str(totalbal), True, WHITE)

or use a formatted string literals

text = font.render(f'Your total clicks are {totalbal}', True, WHITE)

If the text changes, you need to render the text Surface again:

text = font.render(f'Your total clicks are {totalbal}', True, WHITE)
while Loop: # main game loop
    # [...]

    for event in pygame.event.get():

        if event.type == MOUSEBUTTONDOWN: #detecting mouse click
            totalbal += cps
            text = font.render(f'Your total clicks are {totalbal}', True, WHITE)

    # [...]

Upvotes: 1

Related Questions