python alien project, aliens can only move one line, other lines can not move, solve?
main function
import pygame
from pygame.sprite import Group
from setting import Settings
from ship import Ship
import game_functions as gf
from alien import Alien
def run_game():
pygame.init()
pygame.display.set_caption("Alien Invasion")
game_settings = Settings()
screen = pygame.display.set_mode((game_settings.screen_width,game_settings.screen_height))
game_ship = Ship(screen,game_settings) -sharpgame_alien = Alien(screen)game_shipgf.screen_update()bilt_ship()-sharpwhileshipship__init__ ,
bullets = Group()
aliens = Group()
gf.alien_number(game_settings,screen,game_ship,aliens) -sharpaliens
while True:
gf.check_events(game_settings,game_ship,screen,bullets)
game_ship.update_move()
gf.update_bullet(bullets)
print("\n")
for alien in aliens.sprites():
print((alien.rect.x,alien.rect.y) ,end="")
gf.update_alien(aliens)
print("\n")
for alien in aliens.sprites():
print((alien.rect.x,alien.rect.y) ,end="")
gf.screen_update(screen,game_settings,game_ship,bullets,aliens)
run_game()
alien.py
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
def __init__(self,screen,game_settings):
super(Alien,self).__init__()
self.image = pygame.image.load("images/alien.png")
self.screen = screen
self.game_settings = game_settings
self.rect = self.image.get_rect()
"""
self.rect.x = self.rect.width
self.rect.y = self.rect.height
"""
self.x = float(self.rect.x)
self.y = float(self.rect.y)
def update(self):
self.y += self.game_settings.alien_speed
self.rect.y= self.y
"""
self.x += self.game_settings.alien_speed
self.rect.x = self.x
"""
def blit_me(self):
self.screen.blit(self.image,self.rect)
game_function.py
-sharp coding=gbk
import sys
import pygame
from bullet import Bullet
from alien import Alien
def check_events(game_settings,game_ship,screen,bullets):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event,game_settings,game_ship,screen,bullets)
elif event.type == pygame.KEYUP:
check_keyup_events(event,game_ship)
def check_keydown_events(event,game_settings,game_ship,screen,bullets):
if event.key == pygame.K_RIGHT:
game_ship.move_right = True
elif event.key == pygame.K_LEFT:
game_ship.move_left = True
elif event.key == pygame.K_SPACE:
fire_bullet(game_settings,game_ship,screen,bullets)
def fire_bullet(game_settings,game_ship,screen,bullets):
if len(bullets) < game_settings.bullet_allow:
new_bullet = Bullet(game_settings,game_ship,screen)
bullets.add(new_bullet)
def check_keyup_events(event,game_ship):
if event.key == pygame.K_RIGHT:
game_ship.move_right = False
elif event.key == pygame.K_LEFT:
game_ship.move_left = False
def screen_update(screen,game_settings,game_ship,bullets,aliens):
screen.fill(game_settings.bg_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
game_ship.bilt_ship()
"""
print("\n")
for alien in aliens.sprites():
print((alien.rect.x,alien.rect.y) ,end="")
"""
aliens.draw(screen)
pygame.display.flip()
def update_bullet(bullets):
bullets.update()
for bullet in bullets.copy():
if bullet.bullet_rect.bottom <= 0:
bullets.remove(bullet)
-sharpprint(len(bullets))
def alien_number(game_settings,screen,game_ship,aliens):
game_alien = Alien(screen, game_settings)
alien_width =game_alien.rect.width
screen_row_space = game_settings.screen_width - alien_width
alien_row_No = int(screen_row_space/(2*alien_width))
alien_height =game_alien.rect.height
screen_col_space = game_settings.screen_height - 3*alien_height - game_ship.image_rect.height
alien_col_No = int(screen_col_space/(3*alien_height))
-sharpprint(alien_col_No)
for sub_alien_col_No in range(alien_col_No):
for sub_alien_row_No in range(alien_row_No):
new_alien= Alien(screen,game_settings)
new_alien.rect.x = sub_alien_row_No*2*alien_width + alien_width
new_alien.rect.y = sub_alien_col_No*2*alien_height +alien_height
aliens.add(new_alien)
"""
print("\n")
for alien in aliens.sprites():
print((alien.rect.x,alien.rect.y) ,end="")
"""
def update_alien(aliens):
aliens.update()
setting.py
class Settings():
def __init__(self):
self.screen_width = 800
self.screen_height = 650
self.bg_color = (255,255,255)
self.move_speed = 1
self.bullet_speed = 10 -sharp
self.alien_speed = 0.01
self.bullet_color = 0,0,0 -sharp
self.bullet_width = 2 -sharp
self.bullet_height = 5-sharp
self.bullet_allow = 80-sharp