不确定如何将带有变量的字体 blit 到表面上,pygame
Not sure how to blit font with a variable onto a surface, pygame
尝试将带有变量的字体 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)
稍后我这样调用 show_score 变量:
screen.blit(show_score, (30, 400))
完整代码如下:https://pastebin.com/5RnShSCG
编辑:我忘记说了。文本“你的分数:”出现在屏幕上,但变量始终为 0,即使分数更高。
SCORE
未绑定到 show_score
表面。当您更改 SCORE
时,show_score
表面不会神奇地改变。您必须重新渲染 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)
尝试将带有变量的字体 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)
稍后我这样调用 show_score 变量:
screen.blit(show_score, (30, 400))
完整代码如下:https://pastebin.com/5RnShSCG
编辑:我忘记说了。文本“你的分数:”出现在屏幕上,但变量始终为 0,即使分数更高。
SCORE
未绑定到 show_score
表面。当您更改 SCORE
时,show_score
表面不会神奇地改变。您必须重新渲染 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)