< H1 > I wrote about the alien crowd movement according to the content in the book. I found that def update_aliens (aliens): aliens.update () in game_function.py could not read the alien_speed value in setting.py normally. I changed the def update (self): self.x + = self.ai_screen.alien_speed in alien.py to self.x + = 1, so that the alien crowd could move to the right normally. What"s the problem? The newcomer has been bothered for a long time by asking for a lot of advice. < / H1 >
< H1 > alien_invasion.py < / H1 >
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as game_expand
from pygame.sprite import Group
from alien import Alien
from bull import Bullet
def run_game():
pygame.init()-sharppygamepygame
ai_screen = Settings()
-sharpsettingSettings
screen = pygame.display.set_mode((ai_screen.width,ai_screen.height))
-sharpsettingself.widthself.height
pygame.display.set_caption("Alien Invasion")
-sharp
ship = Ship(screen,ai_screen)
-sharpshipShip,
black_color = ai_screen.black_color
-sharpRGB0-255
bullets = Group()
aliens = Group()
-sharp
game_expand.create_fleet(ai_screen,screen,aliens)
-sharp
while True:-sharp
ship.update()
-sharp update
game_expand.check_events(ai_screen, screen, ship, bullets)
-sharpshipgame_functionscheck_events
game_expand.update_bullets(aliens,bullets,ai_screen,screen,ship)
game_expand.update_aliens(aliens)
game_expand.update_screen(ai_screen,screen,ship,bullets,aliens)
run_game()
< hr >
< H1 > game_function.py < / H1 >
import sys
import pygame
from bull import Bullet
from alien import Alien
-sharp
def check_keydown_events(event,ship,ai_screen,screen,bullets):
if event.key == pygame.K_RIGHT:
ship.move_right = True
-sharp true
elif event.key == pygame.K_LEFT:
ship.move_left = True
elif event.key == pygame.K_UP:
ship.move_up = True
elif event.key == pygame.K_DOWN:
ship.move_down = True
elif event.key == pygame.K_SPACE:
-sharpbullets
fire_bullets(bullets,ai_screen,screen,ship)
elif event.key == pygame.K_q:
-sharpbullets
sys.exit()
def check_keyup_events(event,ship):
if event.key == pygame.K_RIGHT:
ship.move_right = False
-sharp false
if event.key == pygame.K_LEFT:
ship.move_left = False
if event.key == pygame.K_UP:
ship.move_up = False
if event.key == pygame.K_DOWN:
ship.move_down = False
def check_events(ai_screen, screen, ship, bullets):
for event in pygame.event.get():
-sharp
if event.type == pygame.QUIT:
sys.exit() -sharp
elif event.type == pygame.KEYDOWN:
-sharppygameKEYDOWN
check_keydown_events(event,ship,ai_screen,screen,bullets)
elif event.type == pygame.KEYUP:
-sharppygameKEYUP
check_keyup_events(event,ship)
def fire_bullets(bullets,ai_screen,screen,ship):
if len(bullets) <= ai_screen.bull_allow:
-sharp setting.pybullets
new_bullet = Bullet(ai_screen, screen, ship)
bullets.add(new_bullet)
def update_bullets(aliens,bullets,ai_screen,screen,ship):
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
bullets.update()
def get_number_aliens_x(ai_screen,alien_width):
-sharp
available_space_x = ai_screen.width - 2 * alien_width
-sharp
number_aliens_x = int(available_space_x / (2 * alien_width))
-sharp
return number_aliens_x
def create_alien(ai_screen,screen,aliens,alien_number):
alien = Alien(ai_screen,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_screen,screen,aliens):
-sharp
alien = Alien(ai_screen,screen)
-sharp
number_aliens_x = get_number_aliens_x(ai_screen,alien.rect.width)
for alien_number in range(number_aliens_x):
create_alien(ai_screen,screen,aliens,alien_number)
def update_aliens(aliens):
aliens.update()
def update_screen(ai_screen,screen,ship,bullets,aliens):
-sharp
screen.fill(ai_screen.black_color)
-sharp
for bull in bullets.sprites():
-sharp,pygame.display.flip()
bull.draw_bull()
ship.blitme()
-sharp
aliens.draw(screen)
-sharp
pygame.display.flip()
-sharp
< hr >
< H1 > aline.py < / H1 >
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
def __init__(self,screen,ai_screen):
super(Alien,self).__init__()
self.screen = screen
self.ai_screen = ai_screen
self.image = pygame.image.load("C:\\Users\\\\PycharmProjects\\Game\\Game_Windows\\aliens.bmp")
-sharp
self.rect = self.image.get_rect()
self.rect.x = self.rect.width
self.rect.y = self.rect.height
-sharp
self.x = float(self.rect.x)
def update(self):
self.x += self.ai_screen.alien_speed
self.rect.x = self.x
def blitme(self):
self.screen.blit(self.image, self.rect)
< hr >
< H1 > setting.py < / H1 >
class Settings():
def __init__(self):
self.width = 960-sharp
self.height = 600-sharp
self.black_color = (255,255,255)-sharp
self.ship_speed = 1.5-sharp
self.bull_speed = 1
self.bull_width = 3
self.bull_height = 15
self.bull_color = (10,10,10)
self.bull_allow = 5-sharp
self.alien_speed = 1
self.fleet_drop_speed = 2
self.fleet_direction = 1