python飞船游戏(三)
#ship.py
class Ship(Sprite):
?? ?
??? def __init__(self,ai_settings,screen):
??????? """初始化飛船并設置其初始位置"""
??????? super(Ship,self).__init__()
??????? self.screen = screen
??????? self.ai_settings = ai_settings
??????? ?
??????? #加載飛船圖像并獲取其外接矩形
??????? self.image = pygame.image.load('images/ship.png')
??????? self.rect = self.image.get_rect()
??????? self.screen_rect = screen.get_rect()
?????? ?
??????? #將每艘新飛船放在屏幕底部中央
??????? self.rect.centerx = self.screen_rect.centerx
??????? self.rect.bottom = self.screen_rect.bottom
?????? ?
???????? #在飛船的屬性center中存儲小數值
??????? self.center = float(self.rect.centerx)
?????? ?
??????? #移動標志
??????? self.moving_right = False
??????? self.moving_left = False
?? ?
??? def update(self):
??????? """根據移動標志調整飛船的位置"""
??????? #更新飛船的center值而不是rect
??????? if self.moving_right and self.rect.right < self.screen_rect.right:
??????????? self.center += self.ai_settings.ship_speed_factor
?????????? ?
??????? if self.moving_left and self.rect.left > 0:
?????????? ?
??????????? self.center -= self.ai_settings.ship_speed_factor
?????????? ?
??????? #根據self.center更新rect對象
??????? self.rect.centerx = self.center
?????? ?
??? def center_ship(self):
??????? """讓飛船在屏幕上居中"""
??????? self.center = self.screen_rect.centerx
?????? ?
?????? ?
??? def blitme(self):
??????? """在指定位置繪制飛船"""
??????? self.screen.blit(self.image,self.rect)
#bullet.py
class Bullet(Sprite):
??? """一個對飛船發射的子彈進行管理的類"""
?? ?
??? def __init__(self,ai_settings,screen,ship):
??????? """在飛船所處的位置創建一個子彈對象"""
??????? super().__init__()
??????? self.screen = screen
?????? ?
??????? #在(0,0)處創建一個表示子彈的矩形,再設置正確的位置
??????? 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_factor = ai_settings.bullet_speed_factor
??? def update(self):
??????? """向上移動子彈"""
??????? #更新表示子彈位置的小數值
??????? self.y -= self.speed_factor
??????? #更新表示子彈的rect的位置
??????? self.rect.y = self.y
?????? ?
??? def draw_bullet(self):
??????? """在屏幕上繪制子彈"""
??????? pygame.draw.rect(self.screen,self.color,self.rect)
?????? ?
???????
#alien.py
class Alien(Sprite):
??? """表示單個外星人的類"""
?? ?
??? def __init__(self,ai_settings,screen):
??????? """初始化外星人并設置其起始位置"""
??????? super(Alien,self).__init__()
??????? self.screen = screen
??????? self.ai_settings = ai_settings
?????? ?
??????? #加載外星人圖像 并設置其rect屬性
??????? self.image = pygame.image.load('images/alien.png')
??????? 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 blitme(self):
??????? """在指定位置繪制外星"""
??????? self.screen.blit(self.image,self.rect)
?????? ?
??? 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_factor *
?????????????????? self.ai_settings.fleet_direction)
??????? self.rect.x = self.x
#gamestats
class GameStats():
??? """跟蹤游戲的信息"""
?? ?
??? def __init__(self,ai_settings):
??????? """初始化統計信息"""
??????? self.ai_settings = ai_settings
??????? self.reset_stats()
?????? ?
??????? #游戲剛啟動時處于 非 活動狀態
??????? self.game_active = False
?????? ?
??????? #在任何情況下都不應重置最高得分
??????? self.high_score = 0
?????? ?
??? def reset_stats(self):
??????? """初始化在游戲運行期間可能變化的統計信息"""
??????? self.ships_left = self.ai_settings.ship_limit
??????? self.score = 0
??????? self.level = 1
#button.py
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 = (255,220,0)
??????? self.text_color = (255,255,255)
??????? 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
總結
以上是生活随笔為你收集整理的python飞船游戏(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu18系统下采集点云数据(速腾
- 下一篇: root权限是