I'm trying to make a Cookie Clicker type game using PyGame, but when I use this bit of code:
def display(thing: pygame.Surface, where: tuple=(0, 0), center=False):
WIN.blit(thing, where if center == False else thing.get_rect(center = (WIN.get_width() // 2, WIN.get_height() // 2)))
def font(font: pygame.font.Font, text: str, colour: tuple=(255, 255, 255)):
return font.render(text, False, colour)
def main():
cookies = 0
cps = 1
clock = pygame.time.Clock()
tim = time.time()
run = True
while run:
clock.tick(FPS)
if time.time() - tim > 1:
tim = time.time()
cookies = cps
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
t = font(CFONT64, f"{cookies} Cookies")
display(t, t.get_rect(center = (WIN.get_width() // 2, WIN.get_height() * .07)))
pygame.display.update()
quitPy()
(not full code)
and overlap the text when it next updates.
How do I clear the text/change the text?
CodePudding user response:
Everything that is drawn is drawn on the target Surface. The entire scene has to be redrawn in each frame. Therefore, the display must be cleared at the beginning of each frame in the application loop:
while run:
# [...]
# clear display
WIN.fill((0, 0, 0))
# draw scene
display(t, t.get_rect(center = (WIN.get_width() // 2, WIN.get_height() * .07)))
# update disaply
pygame.display.update()