python飞机大战游戏显示分数的方法

 时间:2026-02-12 09:05:34

1、首先,如图,设计分数显示位置在游戏画面的左上角位置。

python飞机大战游戏显示分数的方法

2、然后,在

class PlaneGame(object):

    '''飞机大战主游戏'''

    def __init__(self):

下添加代码:self.score = 0

python飞机大战游戏显示分数的方法

3、然后,在    def __update_sprites(self):

下添加

self.drawText("计分:"+str(self.score),SCREEN_RECT.width - 500,30)

python飞机大战游戏显示分数的方法

4、然后,在 

    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)

python飞机大战游戏显示分数的方法

5、其中:

 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)为背景颜色

python飞机大战游戏显示分数的方法

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

python飞机大战游戏显示分数的方法

  • vb中如何利用do-until循环求累加和
  • table对象如何使用cellpadding属性?
  • 用Mathematica学习微分几何——曲线论(一)
  • python大小写字母转换
  • 单性别教育的好处:[2]好处
  • 热门搜索
    销售月总结怎么写 蜜蜂蛰消肿的最快方法 报价单怎么做 十九禁漫画大全之无彩翼漫 头像图片大全 减肚子最有效的方法 保险箱怎么开 怎么取消快捷支付 三叉神经痛治疗方法 甲状腺结节怎么治疗最好的方法