python飞机大战游戏显示分数的方法
来源:网络收集 点击: 时间:2024-03-02首先,如图,设计分数显示位置在游戏画面的左上角位置。

然后,在
class PlaneGame(object):
飞机大战主游戏
def __init__(self):
下添加代码:self.score = 0

然后,在 def __update_sprites(self):
下添加
self.drawText(计分:+str(self.score),SCREEN_RECT.width - 500,30)

然后,在
def __game_over():
print(游戏结束)
pygame.quit()
exit()
后添加drawText函数:
def drawText(self,text,posx,posy,textHeight=45,fontColor=(1,1,1),backgroundColor=(255,255,255)):
fontObj = pygame.font.Font(simsun.ttc,textHeight)
textSurfaceObj = fontObj.render(text,13,fontColor,backgroundColor)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (posx,posy)
self.screen.blit(textSurfaceObj,textRectObj)

其中:
def drawText(self,text,posx,posy,textHeight=45,fontColor=(1,1,1),backgroundColor=(255,255,255)):
fontObj = pygame.font.Font(simsun.ttc,textHeight)
simsun.ttc为字体样式,需要将字体放在目录文件下。
textHeight=45为字体大小
fontColor=(1,1,1)为字体颜色
backgroundColor=(255,255,255)为背景颜色

最后运行游戏,可以看到如下图,飞机大战游戏显示分数成功。

tips1:个人原创,请勿转载!
tips2:图文结合查看!
飞机大战PYTHON显示分数计分板击毁敌机版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.ff371.cn/art_216771.html