简体   繁体   English

不确定如何将带有变量的字体 blit 到表面上,pygame

[英]Not sure how to blit font with a variable onto a surface, pygame

trying to blit onto the screen a font with a variable - the score you have achieved - onto the screen after dying.试图在屏幕上 blit 一个带有变量的字体 - 你已经达到的分数 - 在死后出现在屏幕上。

BLACK = (0, 0, 0)
font_small = pygame.font.SysFont("Verdana", 20)
scoreMsg = "Your score: {0}".format(SCORE)
show_score = font_small.render(scoreMsg, True, BLACK)

later on I call the show_score variable like this:稍后我这样调用 show_score 变量:

screen.blit(show_score, (30, 400))

here is the full code: https://pastebin.com/5RnShSCG这是完整的代码: https://pastebin.com/5RnShSCG

edit: i forgot to mention.编辑:我忘了说。 the text "Your score:" shows up on the screen, but the variable is always 0, even if score is higher.文本“你的分数:”出现在屏幕上,但变量始终为 0,即使分数更高。

The SCORE is not tied to the show_score surface. SCOREshow_score表面无关。 The show_score surface does not magically change when you change SCORE .当您更改SCORE时, show_score表面不会神奇地改变。 You must rerender the show_score surface:您必须重新渲染show_score表面:

class Enemy(pygame.sprite.Sprite):
  # [...]

  def move(self):

    global SCORE, show_score
    
    self.rect.move_ip(0,SPEED)
    if (self.rect.top > 600):
      self.rect.top = 0
      SCORE+=1

      show_score = font_small.render("Your score: {0}".format(SCORE), True, BLACK)

      self.rect.center = (random.randint(30, 370), 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM