文字显示重叠的pygame碰撞

时间:2019-01-22 12:33:48

标签: python pygame

def main():  #my main loop 
running = True
clock = pygame.time.Clock()  # A clock to limit the frame rate.
score = (0)
score = str(score)
myfont = pygame.font.SysFont('OpenSans', 30)        
textsurface = myfont.render('Level ONE:   Greenland', False, (0, 0, 0))        
background.blit(textsurface,(500,10))

该分数将转换为可以显示在屏幕上的字符串,但在添加分数时会转换回整数

textsurface = myfont.render(score, False, (0, 0, 0))        
background.blit(textsurface,(10,10))
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    player_hit_list = pygame.sprite.spritecollide(player, enemy_list, True)
    for enemy in player_hit_list:
        pygame.quit()
    player_hit_list = pygame.sprite.spritecollide(player, coin_list, True)


    for coin in player_hit_list:
        textsurface = myfont.render(score, False, (0, 0, 0))        
        background.blit(textsurface,(10,10))
        score = int(score)+ 1
        score = str(score)
        print(score)

在这里,我试图让角色触摸硬币时将分数更新到屏幕上,但该分数与先前的分数重叠,例如0、1、2、3。所以我需要摆脱以前的分数。

1 个答案:

答案 0 :(得分:0)

textsurface = myfont.render(score, False, (0, 0, 0))        
background.blit(textsurface,(10,10))
while running:

    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    player_hit_list = pygame.sprite.spritecollide(player, enemy_list, True)
    for enemy in player_hit_list:
        pygame.quit()
    player_hit_list = pygame.sprite.spritecollide(player, coin_list, True)


    for coin in player_hit_list:
        textsurface = myfont.render(score, False, (0, 0, 0))        
        background.blit(textsurface,(10,10))
        score += 1
        print("{0}".format(score))

Python/Pygame How to make my Score Text update itself forever?