乐趣区

关于python:Python小游戏外星人入侵终于完成了

历时七天,终于做进去了(因为还要上网课,学习其余的货色,所以做的比较慢,如果每天能拿出五个小时做这个游戏的话,集体感觉三天差不多,当然了,这是对于小白来说)。我是依照买的材料书上来做的,在我代码外面呢,减少了一些材料上没有的性能,比如说外星人是随机产生的(材料书是创立整个外星人群),原本打算让每个外星人随机挪动的,然而试了一下发现,外星人挪动横七竖八,而后后就采纳了材料书上的做法,让它们作为整体挪动;还有就是在射中外星人的时候,可能会产生暴击(emmm, 其实是福利吧,bone,不是说挫伤有暴击,这个“暴击”得分会更高一些);再一个就是随着等级减少,飞船发射子弹的宽度以及每次发射子弹的数量都会有所增加,当然,外星人以及飞船挪动速度也会减少;再一个就是最高分(High score)、以后分数(score)、飞船残余生命、等级(level)的布局和材料书有所不同,改变就大抵这些吧,当前还会逐步改善,比方减少声音啊,让外星人也能发射子弹,飞船碰到外星人的子弹也会死亡之类的性能吧。OK,废话少说,上面上代码!
alien_invasion.py:

`"""该游戏主程序,尽量做到最简略"""
import pygame

from settings import Settings
from ship import Ship
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
from score_board import Scoreboard

import game_function as gf

def run_game():
       """初始化背景设置"""
       pygame.init()
       
       """"创立一个 Settings 实例"""
       ai_settings=Settings()
       
       """创立一个游戏窗口以及题目"""
       screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
       pygame.display.set_caption('Alien Invasion')
       
       """创立一艘飞船实例"""
       ship=Ship(ai_settings,screen)
       
       """创立一个用于存储子弹和外星人的编组"""
       bullets=Group()
       aliens=Group()

       """创立外星人群"""
       gf.creat_fleet(ai_settings,screen,ship,aliens)

       """创立存储游戏统计信息的实例, 并创立记分牌"""
       stats = GameStats(ai_settings)
       sb = Scoreboard(ai_settings,screen,stats)
       
       #创立 play 按钮
       play_button = Button(ai_settings,screen,"Play")
       
       """进入主循环"""  
       while True:
              """"监督用户的操作,键盘和鼠标"""
              gf.check_events(ai_settings,screen,ship,aliens,bullets,stats,play_button,sb)
              if stats.game_active:
                     """更新飞船"""
                     ship.update()
                     """更新子弹"""
                     gf.update_bullet(ai_settings,screen,ship,aliens,bullets,stats,sb)
                     """更新外星人"""
                     gf.update_aliens(ai_settings,screen,ship,aliens,stats,bullets,sb)
              """刷新屏幕"""
              gf.update_screen(ai_settings,screen,ship,aliens,bullets,
                               stats,play_button,sb)

run_game()` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41
*   42
*   43
*   44
*   45
*   46
*   47
*   48
*   49
*   50
*   51
*   52
*   53
*   54
*   55
*   56
*   57

game_function.py:
如果把这个游戏比作一个人,那么上一段代码是刚接触这个人,他给你留下的第一印象,那么上面这段代码能够说是这个人的灵魂了

`import sys
import pygame

from random import randint
from bullet import Bullet
from alien import Alien
from time import sleep

def fire_bullet(ai_settings,screen,ship,bullets,stats):
       """停火!"""
       #确保屏幕上的子弹数在限度范畴内
       for i in range(int(stats.level/5) + 1):
              if len(bullets) < ai_settings.bullet_allowed:
                     new_bullet=Bullet(ai_settings,screen,ship)
                     bullets.add(new_bullet)

def check_keydown(event,ai_settings,screen,ship,bullets,stats):
       """检查用户按键是否按下以及执行的工作"""
       if event.key  == pygame.K_RIGHT:
              ship.moving_right=True
                            
       elif event.key == pygame.K_LEFT:
              ship.moving_left=True

       elif event.key == pygame.K_UP:
              ship.moving_up=True

       elif event.key == pygame.K_DOWN:
              ship.moving_down=True

       elif event.key == pygame.K_SPACE:
              #发射一颗子弹,并且在限度范畴内
              fire_bullet(ai_settings,screen,ship,bullets,stats)
              
def check_keyup(event,ship):
       """检查用户开释按键"""
       if event.key == pygame.K_RIGHT:
              ship.moving_right=False
                            
       elif event.key == pygame.K_LEFT:
              ship.moving_left=False

       elif event.key == pygame.K_UP:
              ship.moving_up=False

       elif event.key == pygame.K_DOWN:
              ship.moving_down=False` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41
*   42
*   43
*   44
*   45
*   46
*   47
*   48

`def check_events(ai_settings,screen,ship,aliens,bullets,stats,play_button,sb):
       """响应键盘和鼠标事件"""
       for event in pygame.event.get():
              if event.type == pygame.K_q:
                     sys.exit()
              #如果始终按下右键或者左键,空格键,则向右或右挪动或者停火 
              elif event.type == pygame.KEYDOWN:
                     check_keydown(event,ai_settings,screen,ship,bullets,stats)
              
              #开释右键或左键,进行挪动
              elif event.type == pygame.KEYUP:
                     check_keyup(event,ship)

              #点击 Play 按钮,开始游戏
              elif event.type == pygame.MOUSEBUTTONDOWN:
                     mouse_x,mouse_y = pygame.mouse.get_pos()
                     check_play_button(ai_settings,screen,ship,aliens,bullets,
                       stats,play_button,mouse_x,mouse_y,sb)
              
def  check_play_button(ai_settings,screen,ship,aliens,bullets,
                       stats,play_button,mouse_x,mouse_y,sb):
       """在玩家单击 Play 按钮时开始游戏"""
       button_clicked = play_button.rect.collidepoint(mouse_x,mouse_y)
       if button_clicked and not stats.game_active:
              #重置游戏设置
              ai_settings.init_dynamic_settings()
              #暗藏光标
              pygame.mouse.set_visible(False)
              #重置游戏统计信息
              stats.reset_stats()
              stats.game_active = True

              #重置记分牌图像
              sb.prep_score()
              sb.prep_high_score(screen)
              sb.prep_level()
              sb.prep_ships(screen)

              #清空外星人和子弹列表
              aliens.empty()
              bullets.empty()

              #创立一群新的外星人,并让飞船居中
              creat_fleet(ai_settings,screen,ship,aliens)
              ship.center_ship(ai_settings)
                                                                        
def update_screen(ai_settings,screen,ship,aliens,bullets,stats,play_button,sb):
       """每次循环都重绘屏幕"""
       screen.fill(ai_settings.bg_color)
       for bullet in bullets.sprites():
              bullet.draw_bullet()
       ship.blitme()
       aliens.draw(screen)
       sb.show_score()
       
       #如果游戏处于非活动状态,绘制 Play 按钮
       if not stats.game_active:
              play_button.draw_button()
              
       """"刷新屏幕,擦去旧屏幕,显示新屏幕"""
       pygame.display.flip()` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41
*   42
*   43
*   44
*   45
*   46
*   47
*   48
*   49
*   50
*   51
*   52
*   53
*   54
*   55
*   56
*   57
*   58
*   59
*   60
*   61
*   62

`def update_bullet(ai_settings,screen,ship,aliens,bullets,stats,sb):
       """更新子弹地位,并删除曾经隐没的子弹"""
       #更新子弹地位
       bullets.update()
       #删除隐没的子弹
       for bullet in bullets.copy():
              if bullet.rect.bottom<=0:
                     bullets.remove(bullet)
       #查看是否有子弹击中外星人
       check_bullet_alien_colide(ai_settings,screen,ship,aliens,bullets,stats,sb)
       
def get_number_aliens_x(ai_settings,alien_width):
       """取得程度方向上外星人个数"""
       available_space_x = ai_settings.screen_width-2 * alien_width
       number_aliens_x = int(available_space_x/(2 * alien_width))
       return number_aliens_x

def get_space_rows(ai_settings,ship_height,alien_height):
       """取得垂直方向上外星人的行数"""
       available_space_y = (ai_settings.screen_height-(3 * alien_height)-
                          ship_height)                            
       number_aliens_rows = int(available_space_y/(2 * alien_height))
       return number_aliens_rows

def creat_alien(ai_settings,screen,aliens,alien_number,row_number):
       """依据传入的数据在某个地位创立一个外星人"""
       alien = Alien(ai_settings,screen)
       alien_width = alien.rect.width
       alien.x = alien_width + 2 * alien_width * alien_number
       alien.rect.x = alien.x
       alien.rect.y = (alien.rect.height +20) + 2 * alien.rect.height * row_number 
       aliens.add(alien)

def creat_random_alien_x(number_aliens_x,ai_settings,screen,aliens,row_number):
       """在第一行随机创立若干个外星人"""
       for i in range(2,number_aliens_x+2):
              random_number_x=randint(2,i)
              for alien_number in range(random_number_x - 1,random_number_x):
                     creat_alien(ai_settings,screen,aliens,alien_number,row_number)` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40

`def creat_fleet(ai_settings,screen,ship,aliens):
       """创立外星人群以及获取屏幕上最大的行数和每行最多个数"""
       alien=Alien(ai_settings,screen)
       number_aliens_x = get_number_aliens_x(ai_settings,alien.rect.width)
       number_rows = get_space_rows(ai_settings,ship.rect.height,
                                    alien.rect.height)
       """随机创立行数"""
       for j in range(0,number_rows):
              random_number_y = randint(0,j)
              for row_number in range(random_number_y):
                     creat_random_alien_x(number_aliens_x,ai_settings,
                                                 screen,aliens,row_number)

def check_fleet_edges(ai_settings,aliens):
       """有外星人达到屏幕边缘"""
       for alien in aliens.sprites():
              if alien.check_edges():
                     change_fleet_direction(ai_settings,aliens)
                     break
              
def change_fleet_direction(ai_settings,aliens):
       """将外星人下移,并扭转它们的方向"""
       for alien in aliens.sprites():
              alien.rect.y += ai_settings.fleet_drop_speed
       ai_settings.fleet_direction *= -1
       
def update_aliens(ai_settings,screen,ship,aliens,stats,bullets,sb):
       """检测有外星人位于屏幕边缘或者相撞或者外星人达到底部,更新外星人的地位"""       
       check_fleet_edges(ai_settings,aliens)
       aliens.update()

       #检测飞船与外星人的撞击
       if pygame.sprite.spritecollideany(ship,aliens):
              ship_hit(ai_settings,screen,ship,aliens,stats,bullets,sb)
       #查看是否有外星人达到屏幕底部
       check_aliens_bottom(ai_settings,screen,ship,aliens,stats,bullets,sb)

def check_bullet_alien_colide(ai_settings,screen,ship,aliens,bullets,stats,sb):
       """检测子弹和外星人的碰撞"""
       collisions = pygame.sprite.groupcollide(bullets,aliens,True,True)
       #两个 True 可使得子弹与外星人碰撞后隐没,并返回一个字典
       #碰撞之后加分
       if collisions:
              for aliens in collisions.values():
                     i = randint(0,10)
                     if i>8:
                            stats.score += (ai_settings.alien_points + 10) * len(aliens)
                            sb.prep_score()
                     else:
                            stats.score += ai_settings.alien_points * len(aliens)
                            sb.prep_score()
              #查看是否刷新最高分
              check_high_score(stats,sb,screen)
              
       if len(aliens) == 0:
              #删除现有的子弹,放慢游戏速度, 创立新的外星人
              ship.center_ship(ai_settings)
              bullets.empty()
              ai_settings.increase_speed()
              #进步等级
              stats.level += 1
              ai_settings.increase_bullet_size()
              sb.prep_level()
              
              creat_fleet(ai_settings,screen,ship,aliens)` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41
*   42
*   43
*   44
*   45
*   46
*   47
*   48
*   49
*   50
*   51
*   52
*   53
*   54
*   55
*   56
*   57
*   58
*   59
*   60
*   61
*   62
*   63
*   64
*   65
*   66

`def ship_hit(ai_settings,screen,ship,aliens,stats,bullets,sb):
       """ 飞船与外星人相撞,生命减 1,革除外星人和子弹列表
       并创立新的外星人,飞船放在屏幕底部地方地位 """
       if stats.ship_left > 0:
              stats.ship_left -= 1
              sb.prep_ships(screen)
              #清空外星人和子弹列表
              aliens.empty()
              bullets.empty()
              #新建一个飞船和外星人群
              creat_fleet(ai_settings,screen,ship,aliens)
              ship.center_ship(ai_settings)
              #暂停 0.5 秒
              sleep(0.5)
       else:
              ai_settings.bullet_width = 3 
              stats.game_active = False
              pygame.mouse.set_visible(True)
             
def check_aliens_bottom(ai_settings,screen,ship,aliens,stats,bullets,sb):
       """查看是否有外星人达到底部"""
       screen_rect = screen.get_rect()
       for alien in aliens.sprites():
              if alien.rect.bottom >= screen_rect.bottom:
                   ship_hit(ai_settings,screen,ship,aliens,stats,bullets,sb)
                   break
       
def check_high_score(stats,sb,screen):
       """查看是否产生了新的最高分"""
       if stats.score > stats.high_score:
              stats.high_score = stats.score
              sb.prep_high_score(screen)` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32

前面的文件根本是作为分支吧,一些属性还有一些初始化的数据,都是为 game_function 文件提供资源的
settings.py:

`class Settings():
       def __init__(self):
              """设置长度和宽度以及背景色属性"""
              self.screen_width = 800
              self.screen_height = 700
              self.bg_color=(255,255,255)
              self.ship_limit = 2

              """子弹设置""" 
              self.bullet_width = 3
              self.bullet_height=15
              self.bullet_color=(60,60,60)
              self.bullet_allowed=8

              """外星人挪动设置"""
              self.fleet_drop_speed = 10
              """以什么样的速度放慢游戏节奏"""
              self.speed_up_scale = 1.1
              self.init_dynamic_settings()

              """外星人点数的进步"""
              self.score_scale = 1.5

              """子弹大小进步"""
              self.bullet_scale = 10

       def init_dynamic_settings(self):
              self.speed = 1.5
              self.bullet_speed = 3
              self.alien_speed = 0.2
              #fleet_direction 为 1 示意向右挪动,- 1 示意向左挪动
              self.fleet_direction = -1

              #计分
              self.alien_points = 10

       def increase_speed(self):
              """进步速度设置和外星人点数设置"""
              self.speed *= self.speed_up_scale
              self.bullet_speed *= self.speed_up_scale
              self.alien_speed *= self.speed_up_scale

              self.alien_points = int(self.alien_points * self.score_scale)

       def increase_bullet_size(self):
              self.bullet_width += self.bullet_scale` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41
*   42
*   43
*   44
*   45
*   46

ship.py:

`import pygame

from pygame.sprite import Sprite

class Ship(Sprite):
       def __init__(self,ai_settings,screen):
              """初始化飞船并设置其初始地位"""
              super(Ship,self).__init__()
              self.screen=screen

              #加载飞船图像并获取其外接矩形
              self.image=pygame.image.load('images/ship.bmp')
              self.rect=self.image.get_rect()# 获取飞船的矩形
              self.screen_rect=screen.get_rect()# 获取屏幕矩形

              self.ai_settings=ai_settings

              #将每艘新飞船放在屏幕底部地方地位
              self.rect.centerx=self.screen_rect.centerx
              self.rect.centery=self.screen_rect.centery
              self.rect.bottom=self.screen_rect.bottom

              self.center_x=float(self.rect.centerx)
              self.center_y=float(self.rect.centery)
              """间断检测按键,设置未按下右键为 False"""
              self.moving_right=False
              self.moving_left=False
              self.moving_up=False
              self.moving_down=False

       def update(self):
              """如果间断按方向键,则始终挪动, 并且不超过边界"""
              if self.moving_right and self.rect.right < self.screen_rect.right:
                     self.center_x+=self.ai_settings.speed
                     
             #应用两个 if,这样玩家同时按下两个键,#将先增大 rect.centerx 值,再升高,则飞船地位不变 
              if self.moving_left and self.rect.left > 0:
                     self.center_x-=self.ai_settings.speed

              if self.moving_up and self.rect.top > 0:
                     self.center_y-=self.ai_settings.speed
                     
              if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
                     self.center_y+=self.ai_settings.speed

              self.rect.centerx=self.center_x
              self.rect.centery=self.center_y
              
       """在指定地位绘制飞船"""
       def blitme(self):
              self.screen.blit(self.image,self.rect)

       def center_ship(self,ai_settings):
              self.center_x = ai_settings.screen_width/2
              self.center_y = ai_settings.screen_height - 28` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41
*   42
*   43
*   44
*   45
*   46
*   47
*   48
*   49
*   50
*   51
*   52
*   53
*   54
*   55
*   56

alien.py:

`import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
       def __init__(self,ai_settings,screen):
              super().__init__()
              """初始化外星人并设置其初始地位"""
              self.screen=screen
              self.ai_settings=ai_settings

              #加载外星人图像并设置 rect 属性
              self.image=pygame.image.load('images/alien.bmp')
              self.rect=self.image.get_rect()

              #每个外星人都在屏幕左上角左近
              self.rect.x=self.rect.width
              self.rect.y=self.rect.height 

              #存储外星人精确地位
              self.x=float(self.rect.x)

       def check_edges(self):
              """如果外星人碰到屏幕边缘,则返回 True"""
              screen_rect = self.screen.get_rect()
              if self.rect.right >= screen_rect.right:
                     return True
              elif self.rect.left <= 0:
                     return True

       def update(self):
              """挪动外星人"""
              self.x += (self.ai_settings.alien_speed *
                         (self.ai_settings.fleet_direction))
              self.rect.x=self.x

       def blitme(self):
              """在指定地位绘制飞船"""
              self.screen.blit(self.image,self.rect)` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38

bullet.py:

`import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
       def __init__(self,ai_settings,screen,ship):
              #在飞船地位创立一个子弹对象
              super(Bullet,self).__init__()
              self.screen=screen

              self.rect=pygame.Rect(0,0,ai_settings.bullet_width
                                    ,ai_settings.bullet_height)
              self.rect.centerx = ship.rect.centerx
              self.rect.top = ship.rect.top
              self.y=float(self.rect.y)
              self.color=ai_settings.bullet_color
              self.speed=ai_settings.bullet_speed

       def update(self):
              """向上挪动子弹"""
              #更新子弹的小数值
              self.y -= self.speed
              self.rect.y=self.y

       def draw_bullet(self):
              """在屏幕上绘制子弹"""
              pygame.draw.rect(self.screen,self.color,self.rect)` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26

button.py:

`import pygame.font

class Button():
       def __init__(self,ai_settings,screen,msg):
              """初始化按钮属性"""
              self.screen = screen
              self.screen_rect = screen.get_rect()

              #设置按钮大小以及其余属性
              self.width,self.height = 200,50
              self.button_color = (0, 255, 0)
              self.text_color = (60,60,60)
              self.font = pygame.font.SysFont(None,48)

              #创立按钮的 rect 对象,并使其居中
              self.rect = pygame.Rect(0, 0, self.width, self.height)
              self.rect.center = self.screen_rect.center

              #按钮的标签只须要创立一次
              self.prep_msg(msg)

       def prep_msg(self,msg):
              """将 msg 渲染为图像,并使其在按钮上居中"""
              self.msg_image = self.font.render(msg,True,self.text_color,
                                                 self.button_color)
              self.msg_image_rect = self.msg_image.get_rect()
              self.msg_image_rect.center = self.rect.center

       def draw_button(self):
              #绘制一个用色彩填充的按钮,再绘制文本
              self.screen.fill(self.button_color,self.rect)
              self.screen.blit(self.msg_image,self.msg_image_rect)` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32

game_stats.py:

`class GameStats():
       """跟踪游戏的统计信息"""
       def __init__(self,ai_settings):
              self.ai_settings = ai_settings
              self.game_active = False
              self.reset_stats()

              #任何状况下都不能重置最高分
              self.high_score = 0

       def reset_stats(self):
              """初始化在游戏运行过程中可能变动的统计信息"""
              self.ship_left = self.ai_settings.ship_limit
              self.score = 0
              self.level = 1
              self.bullet_width = 3` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16

scord_board.py:

`import pygame.font

from pygame.sprite import Group
from ship import Ship

class Scoreboard():
       """显示得分的类"""
       def __init__(self,ai_settings,screen,stats):
              """初始化显示得分的属性"""
              self.screen = screen
              self.screen_rect = screen.get_rect()
              self.ai_settings = ai_settings
              self.stats = stats

              #显示得分时的字体设置
              self.text_color = (30,30,30)
              self.font = pygame.font.SysFont(None,48)

              #筹备初始得分图像以及以后最高得分
              self.prep_score()
              self.prep_high_score(screen)
              self.prep_level()
              self.prep_ships(screen)

       def prep_score(self):
              """将得分转化为可渲染的图像"""
              rounded_score = int(round(self.stats.score,-1))
              score_str ="Score:"+"{:,}".format(rounded_score)
              self.score_image = self.font.render(score_str,True,self.text_color,
                                                  self.ai_settings.bg_color)

              #将得分放在屏幕右上角
              self.score_rect = self.score_image.get_rect()
              self.score_rect.right = self.screen_rect.right - 20
              self.score_rect.top = 20

       def prep_high_score(self,screen):
              """将最高得分渲染为图片"""
              high_score = int(round(self.stats.high_score,-1))
              high_score_str = "High Score:"+"{:,}".format(high_score)
              self.high_score_image = self.font.render(high_score_str,True,self.text_color,
                                                  self.ai_settings.bg_color)

              """将最高得分放在屏幕左上角"""
              self.high_score_rect = self.high_score_image.get_rect()
              self.high_score_rect.left = self.screen_rect.left + 20
              self.high_score_rect.top = 20

       def prep_level(self):
              """将等级渲染为图像"""
              self.level_image = self.font.render("Level:"+str(self.stats.level),True,
                                                  self.text_color,self.ai_settings.bg_color)

              #将等级放在右下角
              self.level_rect = self.level_image.get_rect()
              self.level_rect.right = self.score_rect.right
              self.level_rect.bottom = self.score_rect.bottom + 650

       def show_score(self):
              """在屏幕上显示得分, 最高分,等级"""
              self.screen.blit(self.score_image,self.score_rect)
              self.screen.blit(self.high_score_image,self.high_score_rect)
              self.screen.blit(self.level_image,self.level_rect)
              #绘制残余飞船
              self.ships.draw(self.screen)

       def prep_ships(self,screen):
              """显示残余的飞机"""
              self.ships = Group()
              for ship_number in range(self.stats.ship_left + 1):
                     ship =Ship(self.ai_settings,self.screen)
                     ship.rect.x = 10 + ship_number * ship.rect.width
                     ship.rect.y = self.screen_rect.top + 640
                     self.ships.add(ship)` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41
*   42
*   43
*   44
*   45
*   46
*   47
*   48
*   49
*   50
*   51
*   52
*   53
*   54
*   55
*   56
*   57
*   58
*   59
*   60
*   61
*   62
*   63
*   64
*   65
*   66
*   67
*   68
*   69
*   70
*   71
*   72
*   73
*   74

代码的话,我感觉正文曾经比较清楚了,而后就没有多费口舌去解释

上面是运行之后的截图:

当三艘飞船用光后,完结游戏

再次点击“Play”按钮,再次开始

退出移动版