如何获得分数以更新并显示在屏幕上?

时间:2019-04-04 21:49:02

标签: python pygame

当玩家触摸硬币分数时,分数会更新并显示,例如1,2,3等不清楚得分。我正在尝试获取它,以便以前的得分消失并且仅显示新得分。

def main():  #my main loop 
    running = True
    clock = pygame.time.Clock()  # A clock to limit the frame rate.
    score = (1)
    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, coin_list,True) 
        for coin in player_hit_list:
             textsurface = myfont.render(score, False, (0, 0, 0))
             background.blit(textsurface,(90,10))
             score = str(int(score)+ 1))

    sprites.update()
    screen.blit(background, (0, 0))
    sprites.draw(screen)  # Draws all of the sprites onto the screen
    clock.tick(60)  # Limit the frame rate to 60 FPS.
    pygame.display.update()

除左上方的我显示分数:1,当触摸硬币时,它会更新,例如得分:

1 个答案:

答案 0 :(得分:0)

使用PyGame surface subsurface() function,复制背景位于核心正下方的部分,可能会增加一些额外内容来处理核心中的更多数字。

然后更新分数,通过写背景的那一部分来擦除现有分数,然后绘制文本位图。

类似的东西:

# rectangle around score
under_score_rect = [ 90, 10,  100,  40 ]  
# copy of background that's under the score
under_score_background = background.subsurface( under_score_rect ).copy()

...

def drawScore( score, screen, background_img ):
    global score_font
    textsurface = score_font.render( score, False, (0, 0, 0) )
    screen.blit( background_img, ( 90, 10 ) )
    screen.blit( textsurface, ( 90,10 ) )

...

drawScore( score, window, under_score_background )