Alien Invasion Code

My main code

import sys
from time import sleep
import pygame

from settings import Settings
from gamestats import GameStats
from button import Button
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
        pygame.display.set_caption("Adventures with Orlnadao (NOT ORLANDO)")


        self.stats = GameStats(self)
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.aliens = pygame.sprite.Group()

        self._create_fleet()
        self.play_button = Button(self, "Play")


    def run_game(self):
        while True:
            self._check_events()
            if self.stats.game_active:
                self.ship.update()
                self._update_aliens()
                self.bullets.update()
                for bullet in self.bullets.copy():
                 if bullet.rect.bottom <= 0:
                    self.bullets.remove(bullet)
                print(len(self.bullets))


            self._update_screen()

    def _update_aliens(self):
        self._check_fleet_edges()
        self._check_aliens_bottom()
        self._check_bullet_alien_collision()
        self.aliens.update()


        if pygame.sprite.spritecollideany(self.ship, self.aliens):
            self._ship_hit()

    def _ship_hit(self):
        if self.stats.ships_left > 0:
            self.stats.ships_left -= 1

            self.aliens.empty()
            self.bullets.empty()

            self._create_fleet()
            self.ship.center_ship()

            sleep(0.5)
        else:
            self.stats.game_active = False
    
    
    
    def _check_bullet_alien_collision(self):
        collisions = pygame.sprite.groupcollide(self.bullets,self.aliens, True, True)
        if not self.aliens:
            self.bullets.empty()
            self._create_fleet()

    
        




    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 _check_events(self):
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
                    
                    if event.type == pygame.KEYDOWN:
                        
                        if event.key == pygame.K_RIGHT:
                            self.ship.moving_right = True
                        if event.key == pygame.K_LEFT:
                            self.ship.moving_left = True
                        if event.key == pygame.K_q:
                            pygame.QUIT()
                        if event.key == pygame.K_SPACE:
                            self.fire_bullet()
                        
                    
                    elif event.type == pygame.KEYUP:
                        if event.key == pygame.K_RIGHT:
                             self.ship.moving_right = False
                        if event.key == pygame.K_LEFT:
                             self.ship.moving_left = False
                    elif event.type == pygame.MOUSEBUTTONDOWN:
                        mouse_pos = pygame.mouse.get_pos()
                        self._check_play_button(mouse_pos)

    def _check_play_button(self, mouse_pos):
        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:
            self.stats.game_active = True

            self.aliens.empty()
            self.bullets.empty()
            self._create_fleet()
            self.ship.center_ship()



    def fire_bullet(self):
        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    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)

        if not self.stats.game_active:
            self.play_button.draw_button()
 
        pygame.display.flip()

    def _create_fleet(self):
         
        alien=Alien(self)
        alien_width = alien.rect.width
        alien_height = alien.rect.height
        alien_width, alien_height = alien.rect.size
        ship_height = self.ship.rect.height

        
        available_space_x = self.settings.screen_width - (2 * alien_width)
        available_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height)
        number_aliens_x = available_space_x // (2 * alien_width)
        number_rows = available_space_y // (2*alien_height)

        for row_number in range(number_rows):
            for alien_number in range(number_aliens_x):
                alien = Alien(self)
                alien_width, alien_height = alien.rect.size
                alien.x = alien_width+2 * alien_width * alien_number
                alien.rect.y=alien_height +2 * alien.rect.height *row_number
                alien.rect.x = alien.x

                self.aliens.add(alien)

    def _check_fleet_edges(self):
        for alien in self.aliens.sprites():
            if alien.check_edges():
                self._change_fleet_direction()
                break

    def _check_aliens_bottom(self):
        screen_rect = self.screen.get_rect()
        for alien in self.aliens.sprites():
            if alien.rect.bottom >= screen_rect.bottom:
                self._ship_hit()
                break

    def _change_fleet_direction(self):
        for alien in self.aliens.sprites():
            alien.rect.y += self.settings.fleet_drop_speed
        self.settings.fleet_direction *= -1


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

Posted

in

by

Tags:

Comments

One response to “Alien Invasion Code”

Leave a Reply

Your email address will not be published. Required fields are marked *