其他部分之前都是正常运行的,之前绘制一个也绘制上了,现在绘制一排的时候就出现问题了
**alien_invasion
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
ship = Ship(ai_settings,screen)
bullets = Group()
aliens = Group()
gf.create_fleet(ai_settings,screen,aliens)
while True:
gf.check_events(ai_settings,screen,ship,bullets)
ship.update()
gf.update_buttlets(bullets)
gf.update_screen(ai_settings,screen,ship,aliens,bullets)
run_game()
```**
**game_functions
*
```python
import sys
import pygame
from bullet import Bullet
from alien import Alien
def check_keydown_events(event,ship,ai_settings,screen,bullets):
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_SPACE:
fire_bullets(ai_settings,screen,ship,bullets)
elif event.key == pygame.K_q:
sys.exit()
def check_keyup_events(event,ship):
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def check_events(ai_settings,screen,ship,bullets):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event,ship,ai_settings,screen,bullets)
elif event.type == pygame.KEYUP:
check_keyup_events(event,ship)
def update_screen(ai_settings,screen,ship,aliens,bullets):
screen.fill(ai_settings.bg_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
alien.blitme()
aliens.draw(screen)
pygame.display.flip()
def update_buttlets(bullets):
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
def fire_bullets(ai_settings,screen,ship,bullets):
if len(bullets) < ai_settings.bullet_allowed:
new_bullet = Bullet(ai_settings,screen,ship)
bullets.add(new_bullet)
def get_number_aliens_x(ai_settings,alien_width):
available_space_x = ai_settings.screen_width - 2 * alien_width
number_aliens_x = int(available_space_x / (2 * alien_width))
return number_aliens_x
def creat_alien(ai_settings,screen,aliens,alien_number):
alien = Alien(ai_settings,screen)
alien_width = alien.rect.width
alien.x = alien_width + 2 * alien_width * alien_number
alien.rect.x = alien.x
aliens.add(alien)
def create_fleet(ai_settings,screen,aliens):
alien = Alien(ai_settings,screen)
number_aliens_x = get_number_aliens_x(ai_settings,alien.rect.width)
for alien_number in range(number_aliens_x):
creat_alien(ai_settings,screen,aliens,alien_number)
alien
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
def __init__(self,ai_settings,screen):
super(Alien,self).__init__()
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load('images/alien.bmp')
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)