{"id":105,"date":"2024-04-16T13:16:17","date_gmt":"2024-04-16T13:16:17","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/a_carpenter\/?p=105"},"modified":"2024-04-16T13:16:17","modified_gmt":"2024-04-16T13:16:17","slug":"alien-invasion-code","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/a_carpenter\/2024\/04\/16\/alien-invasion-code\/","title":{"rendered":"Alien Invasion Code"},"content":{"rendered":"\n<p><strong>My main code<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nfrom time import sleep\nimport pygame\n\nfrom settings import Settings\nfrom gamestats import GameStats\nfrom button import Button\nfrom ship import Ship\nfrom bullet import Bullet\nfrom alien import Alien\n\n\nclass alieninvasion:\n\n    def __init__(self):\n        pygame.init\n\n        self.settings=Settings()\n        \n        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)\n        self.settings.screen_width = self.screen.get_rect().width\n        self.settings.screen_height = self.screen.get_rect().height\n        pygame.display.set_caption(\"Adventures with Orlnadao (NOT ORLANDO)\")\n\n\n        self.stats = GameStats(self)\n        self.ship = Ship(self)\n        self.bullets = pygame.sprite.Group()\n        self.aliens = pygame.sprite.Group()\n\n        self._create_fleet()\n        self.play_button = Button(self, \"Play\")\n\n\n    def run_game(self):\n        while True:\n            self._check_events()\n            if self.stats.game_active:\n                self.ship.update()\n                self._update_aliens()\n                self.bullets.update()\n                for bullet in self.bullets.copy():\n                 if bullet.rect.bottom &lt;= 0:\n                    self.bullets.remove(bullet)\n                print(len(self.bullets))\n\n\n            self._update_screen()\n\n    def _update_aliens(self):\n        self._check_fleet_edges()\n        self._check_aliens_bottom()\n        self._check_bullet_alien_collision()\n        self.aliens.update()\n\n\n        if pygame.sprite.spritecollideany(self.ship, self.aliens):\n            self._ship_hit()\n\n    def _ship_hit(self):\n        if self.stats.ships_left > 0:\n            self.stats.ships_left -= 1\n\n            self.aliens.empty()\n            self.bullets.empty()\n\n            self._create_fleet()\n            self.ship.center_ship()\n\n            sleep(0.5)\n        else:\n            self.stats.game_active = False\n    \n    \n    \n    def _check_bullet_alien_collision(self):\n        collisions = pygame.sprite.groupcollide(self.bullets,self.aliens, True, True)\n        if not self.aliens:\n            self.bullets.empty()\n            self._create_fleet()\n\n    \n        \n\n\n\n\n    def _check_keyup_events(self, event):\n        if event.key == pygame.K_RIGHT:\n            self.ship.moving_right = False\n        elif event.key == pygame.K_LEFT:\n            self.ship.moving_left = False\n\n\n    def _check_events(self):\n                for event in pygame.event.get():\n                    if event.type == pygame.QUIT:\n                        sys.exit()\n                    \n                    if event.type == pygame.KEYDOWN:\n                        \n                        if event.key == pygame.K_RIGHT:\n                            self.ship.moving_right = True\n                        if event.key == pygame.K_LEFT:\n                            self.ship.moving_left = True\n                        if event.key == pygame.K_q:\n                            pygame.QUIT()\n                        if event.key == pygame.K_SPACE:\n                            self.fire_bullet()\n                        \n                    \n                    elif event.type == pygame.KEYUP:\n                        if event.key == pygame.K_RIGHT:\n                             self.ship.moving_right = False\n                        if event.key == pygame.K_LEFT:\n                             self.ship.moving_left = False\n                    elif event.type == pygame.MOUSEBUTTONDOWN:\n                        mouse_pos = pygame.mouse.get_pos()\n                        self._check_play_button(mouse_pos)\n\n    def _check_play_button(self, mouse_pos):\n        button_clicked = self.play_button.rect.collidepoint(mouse_pos)\n        if button_clicked and not self.stats.game_active:\n            self.stats.game_active = True\n\n            self.aliens.empty()\n            self.bullets.empty()\n            self._create_fleet()\n            self.ship.center_ship()\n\n\n\n    def fire_bullet(self):\n        if len(self.bullets) &lt; self.settings.bullets_allowed:\n            new_bullet = Bullet(self)\n            self.bullets.add(new_bullet)\n\n    def _update_screen(self):\n        self.screen.fill(self.settings.bg_color)\n        self.ship.blitme()\n        for bullet in self.bullets.sprites():\n            bullet.draw_bullet()\n        self.aliens.draw(self.screen)\n\n        if not self.stats.game_active:\n            self.play_button.draw_button()\n \n        pygame.display.flip()\n\n    def _create_fleet(self):\n         \n        alien=Alien(self)\n        alien_width = alien.rect.width\n        alien_height = alien.rect.height\n        alien_width, alien_height = alien.rect.size\n        ship_height = self.ship.rect.height\n\n        \n        available_space_x = self.settings.screen_width - (2 * alien_width)\n        available_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height)\n        number_aliens_x = available_space_x \/\/ (2 * alien_width)\n        number_rows = available_space_y \/\/ (2*alien_height)\n\n        for row_number in range(number_rows):\n            for alien_number in range(number_aliens_x):\n                alien = Alien(self)\n                alien_width, alien_height = alien.rect.size\n                alien.x = alien_width+2 * alien_width * alien_number\n                alien.rect.y=alien_height +2 * alien.rect.height *row_number\n                alien.rect.x = alien.x\n\n                self.aliens.add(alien)\n\n    def _check_fleet_edges(self):\n        for alien in self.aliens.sprites():\n            if alien.check_edges():\n                self._change_fleet_direction()\n                break\n\n    def _check_aliens_bottom(self):\n        screen_rect = self.screen.get_rect()\n        for alien in self.aliens.sprites():\n            if alien.rect.bottom >= screen_rect.bottom:\n                self._ship_hit()\n                break\n\n    def _change_fleet_direction(self):\n        for alien in self.aliens.sprites():\n            alien.rect.y += self.settings.fleet_drop_speed\n        self.settings.fleet_direction *= -1\n\n\nif __name__ == '__main__':\n    ai = alieninvasion()\n    ai.run_game()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>My main code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-105","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/comments?post=105"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/105\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/media?parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/categories?post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/tags?post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}