0

I was trying to get many rows of alien to appear, I could add one row and also noticed many aliens being printed on top of each other. now the aliens are not on the screen at all. the aliens dissapeared after adding to the for loop in "def _create_fleet". I would like to know what is wrong with the code and how to make rows of aliens appear.

alien_invasion.py

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((0, 0), pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        #self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        self.bg_color = (239, 136, 62)
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.aliens = pygame.sprite.Group()

        self._create_fleet()

    def _create_fleet(self):
        """ Create the fleet of aliens."""
        #make an alien
        # Create an alien and find the number of aliens in a row.
        # Spacing between each alien is equal to one alien width.
        alien = Alien(self)
        alien_width = alien.rect.width
        alien_width, alien_height = alien.rect.size
        available_space_x = self.settings.screen_width - (1 * alien_width)
        number_aliens_x = available_space_x // (2 * alien_width)

        # Determine the number of rows of aliens that fit on the screen.
        ship_height = self.ship.rect.height
        available_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height)
        number_rows = available_space_y // (2 * alien_height)

        #create the full fleet of aliens
        for row_number in range(number_rows):
            for alien_number in range(number_aliens_x):
                self._create_alien(alien_number, row_number)

    def _create_alien(self, alien_number, row_number):
        """ Create an alien and place it in the row. """
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        alien_width = alien.rect.width
        alien.x = alien_width + 2 *alien_width * alien_number
        alien.rect.x = alien.x
        self.aliens.add(alien)  
        


    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


    def _fire_bullet(self):
        """ Create a new bullet and add it to the bullets group."""
        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)



    def _check_keydown_events(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.rect2.x += 10
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_ESCAPE:
            sys.exit()
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()




    def _update_screen(self):
        """update images on the screen, and flip to the new screen"""
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.aliens.draw(self.screen)

    def _update_bullets(self):
        self.bullets.update()
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)
        


    def _check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()


            if event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)


            if event.type == pygame.KEYUP:
                self._check_keyup_events(event)

        



    def run_game(self):
         while True:

            self._check_events()
            self.ship.update()
         

            self.screen.fill(self.bg_color)
            self.ship.blitme()
            self.ship.blitme2()
            self._update_bullets()
            self._update_screen()
            pygame.display.flip()









                    

            
        
            
    
    
        






if __name__ == '__main__':
    ai = AlienInvasion()
    ai.run_game()

alien.py


import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
    """ A class to represent a single alien in the fleet."""

    def __init__(self, ai_game):
        """Initialize the alien and set its starting position."""
        super().__init__()
        self.screen = ai_game.screen

        # Load the alien image and select its rect attribute.
        self.image = pygame.image.load("C:/Users/Documents/Python Learning/python crash course lessons/Alien Invasion/alien.bmp")
        self.rect = self.image.get_rect()

        # Start each new alien near the top left of the screen.
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        #Store the aliens exact horizontal position.
        self.x = float(self.rect.x)
student789
  • 23
  • 2

0 Answers0