Как исправить наложение текста в pygame
Написал код который выводит количество очков игрока на экран при нажатии ЛКМ, но проблема в том, что очки накладываются друг на друга. Как это исправить?
import pygame
import sys
pygame.init()
win = pygame.display.set_mode((720, 1280))
pygame.display.set_caption('test')
FPS = 20
clock = pygame.time.Clock()
def drawW():
win.blit(walkbg[0], (0, 0))
win.blit(pygame.image.load('ghost.png'), (-250, 700))
class TextObject:
def __init__(self, x, y, text_func, color, font_name, font_size):
self.pos = (230, 100)
self.text_func = text_func
self.color = (255, 255, 255)
self.font = pygame.font.SysFont(font_name, font_size)
self.bounds = self.get_surface(text_func())
def run_game():
drawW()
pygame.display.update()
score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
score += 1
f1 = pygame.font.Font(None, 76)
txt = ("SCORE: " + " " + str(score))
text = f1.render(txt, True, (255, 255, 255))
win.blit(text, (230, 100))
pygame.display.flip()
clock.tick(FPS)
run_game()
Ответы (1 шт):
Автор решения: gil9red
→ Ссылка
Перенес drawW внутри цикла игры, и немного по мелочи поменял.
Попробуйте:
import pygame
import sys
pygame.init()
win = pygame.display.set_mode((720, 1280))
pygame.display.set_caption('test')
FPS = 20
f1 = pygame.font.Font(None, 76)
clock = pygame.time.Clock()
def drawW():
win.fill((0, 0, 0))
# win.blit(walkbg[0], (0, 0))
# win.blit(pygame.image.load('ghost.png'), (-250, 700))
def run_game():
score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
score += 1
break
drawW()
txt = f"SCORE: {score}"
text = f1.render(txt, True, (255, 255, 255))
win.blit(text, (230, 100))
pygame.display.flip()
pygame.display.update()
clock.tick(FPS)
run_game()
