编写一个游戏,将一艘飞船放在屏幕左侧,并允许玩家上下移动飞船。在玩家按空格键时,
让飞船发射一颗在屏幕中向右飞行的子弹,并在子弹从屏幕中消失后将其删除。
ship_shooting.py
import pygame
import sys
from leftship import LeftShip
from settings import Settings
from ship_bullet import Bulletclass 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:if event.key == pygame.K_UP:self.leftship.moving_up = Trueelif event.key == pygame.K_DOWN:self.leftship.moving_down = Trueelif event.key == pygame.K_q:sys.exit()elif event.key == pygame.K_SPACE:self._fire_bullet()elif event.type == pygame.KEYUP:if event.key == pygame.K_UP:self.leftship.moving_up = Falseelif event.key == pygame.K_DOWN:self.leftship.moving_down = Falsedef _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 pygameclass LeftShip:def __init__(self, shooting_game):self.screen = shooting_game.screenself.settings = shooting_game.settingsself.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.midleftself.y = float(self.rect.y)self.moving_up = Falseself.moving_down = Falsedef update(self):if self.moving_up and self.rect.top >= 0:self.y -= self.settings.ship_speedelif self.moving_down and self.rect.bottom <= self.screen_rect.bottom:self.y += self.settings.ship_speedself.rect.y = self.ydef blitme(self):self.screen.blit(self.image, self.rect)
ship_bullet.py
import pygame
from pygame.sprite import Spriteclass Bullet(Sprite):def __init__(self, shooting_game):'''在飞船当前位置创建一个子弹'''super().__init__()self.screen = shooting_game.screenself.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.midrightself.x = float(self.rect.x)def update(self):'''更新子弹的位置,向右移动'''self.x += self.settings.bullet_speedself.rect.x = self.xdef draw_bullet(self):'''在屏幕上绘制子弹'''pygame.draw.rect(self.screen, self.settings.bullet_color, self.rect) # rect(画在哪儿,线的颜色,矩形范围,线宽=0)
settings.py
import pygameclass Settings():def __init__(self):self.screen_width = 600self.screen_height = 800self.bg_color = (232, 232, 232)self.ship_speed = 1self.bullet_speed = 2self.bullet_width = 15self.bullet_height = 3self.bullet_color = (60, 60, 60)
运行后: