|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 bluewind119 于 2022-1-25 14:28 编辑
import sys
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet
from alien import Alien
class AlienInvasion:
"""管理游戏资源和行为的类"""
def __init__(self):
"""初始化游戏"""
pygame.init()
self.settings = Settings()
# 绘制一个游戏窗口
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
# 全屏游戏
#self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
#self.settings.screen_width = self.screen.get_rect().width
#self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()
self._create_fleet()
# 设置背景图案
self.bg_color = (230, 230, 230)
def run_game(self):
"""开始游戏循环"""
while True:
self._check_events()
self.ship.update()
self._update_bullets()
self._update_screen()
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_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_UP:
self.ship.moving_up = True
elif event.key == pygame.K_DOWN:
self.ship.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_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
elif event.key == pygame.K_UP:
self.ship.moving_up = False
elif event.key == pygame.K_DOWN:
self.ship.moving_down = False
def _fire_bullet(self):
"""创建一颗子弹"""
if len(self.bullets) < self.settings.bullets_allowed:
new_bullet = Bullet(self)
self.bullets.add(new_bullet)
def _update_bullets(self):
"""更新子弹的位置并删除消失的子弹"""
self.bullets.update()
# 删除消失的子弹
for bullet in self.bullets.copy():
if bullet.rect.bottom <= 0:
self.bullets.remove(bullet)
def _create_fleet(self):
"""创建外星人群"""
alien = Alien(self)
alien_width = alien.rect.width
available_space_x = self.settings.screen_width - (2 * alien_width)
number_aliens_x = available_space_x // (2 * alien_width)
for alien_number in range(3):
self._create_alien(alien_number)
def _create_alien(self, alien_number):
"""创建一个外星人并将其放在当前行"""
alien = Alien(self)
alien_width = alien.rect.width
alien.x = alien_width + 2 * alien_width * alien_number
alien.rect.x = alien.x
self.aliens.add(alien)
for alien in self.aliens.sprites():
print(alien.rect)
def _update_screen(self):
"""更新屏幕上的图像,并刷新"""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.aliens.draw(self.screen)
# 刷新屏幕
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实例并运行
ai = AlienInvasion()
ai.run_game()
标黄色部分,往精灵集团中add精灵,会覆盖前面添加的精灵,导致最终只显示最后添加的精灵
|
-
|