点击空格开火后子弹直接出现在屏幕最上方如下图:
全部代码如下:
alien_invasion.py
import sys
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet
class AlienInvasion:
"""管理游戏资源和行为的类"""
def __init__(self):
"""初始化游戏并创建游戏资源"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("外星人入侵")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
def run_game(self):
"""开始游戏的主循环"""
while True:
self._check_events()
self.ship.update()
self.bullets.update()
self._update_screen()
def _check_events(self):
"""响应按键和鼠标时间"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
def _check_keydown_events(self,event):
"""响应按键"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_q:
sys.exit()
elif event.key == pygame.K_SPACE:
self._fire_bullet()
def _check_keyup_events(self,event):
"""响应松开"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _fire_bullet(self):
"""创建一颗子弹,并加入编组bullets中"""
new_bullet = Bullet(self)
self.bullets.add(new_bullet)
def _update_screen(self):
"""更新屏幕上的图像,并切换到新屏幕"""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
#让最近绘制屏幕可见
pygame.display.flip()
if __name__ == '__main__':
#创建游戏实例并运行游戏
ai = AlienInvasion()
ai.run_game()
ship.py
import pygame
class Ship:
"""管理飞船的类"""
def __init__(self, ai_game):
"""初始化飞船并设置初始位置"""
self.screen = ai_game.screen
self.settings = ai_game.settings
self.screen_rect = ai_game.screen.get_rect()
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
#对于每艘新飞船,都将其放在屏幕底部
self.rect.midbottom = self.screen_rect.midbottom
self.x = float(self.rect.x)
#移动标志
self.moving_right = False
self.moving_left = False
def update(self):
"""根据移动标志调整飞船"""
#更新飞船而不是rect对象的x值
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.ship_speed
if self.moving_left and self.rect.left > 0:
self.x -= self.settings.ship_speed
self.rect.x=self.x
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
settings.py
class Settings:#创建settings的类
"""储存外星人入侵所有的类"""
def __init__(self):
"""初始化游戏的设置"""
#屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
#飞船设置
self.ship_speed = 1.5
#子弹设置
self.bullet_speed = 1.5
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60,60,60)
bullet.py
import pygame#导入pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""管理飞船所发射子弹的类"""
def __init__(self,ai_game):
"""在飞船当前位置创建一个子弹对象"""
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
self.color = self.settings.bullet_colo
#在(0,0)的位置创建一个表示子弹的矩形并设置正确位置
self.rect = pygame.Rect(0, 0, self.settings.bullet_width,
self.settings.bullet_height)
self.rect.midtop = ai_game.ship.rect.midtop
#储存用小数表示的子弹位置
self.y = float(self.rect.y)
def update(self):
"""向上移动子弹"""
self.y = self.settings.bullet_speed
self.rect.y = self.y
def draw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(self.screen, self.color,self.rect)