又完成一个小游戏啦~
显示健康值和时间
现在游戏进行得很不错了!我们现在需要不断记录分数,看看小兔子的防守做得怎
么样。我们可以在屏幕左上角加一个“血槽”来表示现在堡垒的健康值,并在屏幕
右上角显示存活的时间。
首先把下面的代码加在第七部分(#7)开头,让屏幕右上角显示时间:
#
6.4
–
显示时间
font
=
pygame.font.Font(None,
24)
survivedtext
=
font.render(str((90000-
pygame.time.get_ticks())/60000)+":"+str((90000-
pygame.time.get_ticks())/1000%60).zfill(2),
True,
(0,0,0))
textRect
=
survivedtext.get_rect()
textRect.topright=[635,5]
screen.blit(survivedtext,
textRect)
上面的代码设置了一个字号为
24
的新字体,
font.render
把这个新字体用于屏幕上
所有的函数。接着,我们把文字放在屏幕右上角(topright)并显示在屏幕上。
现在我们加“血槽”。不过在此之前,我们需要先加载血槽的图像。把下面两行代
码加到第三部分(#3)末尾:
healthbar
=
pygame.image.load("resources/images/healthbar.png")
health
=
pygame.image.load("resources/images/health.png")