编写一个游戏,将一艘飞船放在屏幕左侧,并允许玩家上下移动飞船。在玩家按空格键时,
让飞船发射一颗在屏幕中向右飞行的子弹,并在子弹从屏幕中消失后将其删除。
ship_shooting.py
import pygame
import sys
from leftship import LeftShip
from settings import Settings
from ship_bullet import Bullet
class ShipShooting:
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.bg_color = (232, 232, 232)
self.clock = pygame.time.Clock()
self.leftship = LeftShip(self)
self.bullets = pygame.sprite.Group()
def run_game(self):
while True:
self._check_events()
self.leftship.update()
self.bullets.update()
self._remove_offscreen_bullets()
self._update_screen()
self.clock.tick(60)
def _check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
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_UP:
self.leftship.moving_up = True
elif event.key == pygame.K_DOWN:
self.leftship.moving_down = 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_UP:
self.leftship.moving_up = False
elif event.key == pygame.K_DOWN:
self.leftship.moving_down = False
def _fire_bullet(self):
'''创建一颗子弹,并将其加入编组bullets'''
new_bullet = Bullet(self)
self.bullets.add(new_bullet)
def _remove_offscreen_bullets(self):
for bullet in self.bullets.copy():
if bullet.rect.left >= self.settings.screen_width:
self.bullets.remove(bullet)
def _update_screen(self):
'''更新屏幕上的图像,并切换到新屏幕'''
self.screen.fill(self.settings.bg_color)
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.leftship.blitme()
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实例并运行游戏
ss = ShipShooting()
ss.run_game()
left_ship.py
import pygame
class LeftShip:
def __init__(self, shooting_game):
self.screen = shooting_game.screen
self.settings = shooting_game.settings
self.screen_rect = shooting_game.screen.get_rect()
self.image = pygame.image.load('images/ship_left.bmp')
self.rect = self.image.get_rect()
self.rect.midleft = self.screen_rect.midleft
self.y = float(self.rect.y)
self.moving_up = False
self.moving_down = False
def update(self):
if self.moving_up and self.rect.top >= 0:
self.y -= self.settings.ship_speed
elif self.moving_down and self.rect.bottom <= self.screen_rect.bottom:
self.y += self.settings.ship_speed
self.rect.y = self.y
def blitme(self):
self.screen.blit(self.image, self.rect)
ship_bullet.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
def __init__(self, shooting_game):
'''在飞船当前位置创建一个子弹'''
super().__init__()
self.screen = shooting_game.screen
self.settings = shooting_game.settings
# self.color = shooting_game.color
# 先在(0,0)处创建一个表示子弹的矩形,在设置其位置
self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height)
self.rect.midright = shooting_game.leftship.rect.midright
self.x = float(self.rect.x)
def update(self):
'''更新子弹的位置,向右移动'''
self.x += self.settings.bullet_speed
self.rect.x = self.x
def draw_bullet(self):
'''在屏幕上绘制子弹'''
pygame.draw.rect(self.screen, self.settings.bullet_color, self.rect) # rect(画在哪儿,线的颜色,矩形范围,线宽=0)
settings.py
import pygame
class Settings():
def __init__(self):
self.screen_width = 600
self.screen_height = 800
self.bg_color = (232, 232, 232)
self.ship_speed = 1
self.bullet_speed = 2
self.bullet_width = 15
self.bullet_height = 3
self.bullet_color = (60, 60, 60)
运行后: