|
1 | 1 | import pygame
|
2 |
| -import game_functions as gf |
3 |
| -from game_stats import GameStats |
4 | 2 | from pygame.sprite import Group
|
5 |
| -from settings import Settings |
6 |
| -from ship import Ship |
7 |
| -from button import Button |
8 |
| -from scoreboard import Scoreboard |
| 3 | +import src.game_functions as gf |
| 4 | +from src.game_stats import GameStats |
| 5 | +from src.settings import Settings |
| 6 | +from src.ship import Ship |
| 7 | +from src.button import Button |
| 8 | +from src.scoreboard import Scoreboard |
| 9 | +from src.health import Health |
| 10 | +from src.input import Input |
9 | 11 |
|
10 | 12 |
|
11 | 13 | def run_game():
|
12 |
| - # Credit for the assets |
13 |
| - print(""" |
14 |
| - Art assets used in this game were created by Skorpio and are licensed under CC-BY-SA 3.0. |
15 |
| - You can view and download them here: [https://opengameart.org/content/space-ship-construction-kit].\n |
16 |
| - Fire sound effect by K.L.Jonasson, Winnipeg, Canada. Triki Minut Interactive www.trikiminut.com |
17 |
| - You can view and download them here: [https://opengameart.org/content/sci-fi-laser-fire-sfx].\n |
18 |
| - Explosion sound effect by by hosch |
19 |
| - You can view and download them here: https://opengameart.org/content/8-bit-sound-effects-2 |
20 |
| - """) |
21 | 14 | # Initialize pygame, settings, screen object and assets.
|
22 | 15 | pygame.init()
|
23 | 16 | ai_settings = Settings()
|
| 17 | + input = Input() |
24 | 18 | screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
|
25 | 19 | pygame.display.set_caption("Alien Invasion")
|
26 |
| - screen_bg = pygame.image.load("images/space.jpg") |
| 20 | + screen_bg = pygame.image.load("data/assets/images/space3.png") |
27 | 21 | screen_bg = pygame.transform.scale(screen_bg, (ai_settings.screen_width*2, ai_settings.screen_width*2))
|
28 | 22 | screen_bg_2 = pygame.transform.rotate(screen_bg, 180)
|
29 | 23 | clock = pygame.time.Clock()
|
30 |
| - |
31 |
| - # Make the play button. |
32 |
| - play_button = Button(ai_settings, screen, "Play") |
| 24 | + alien_spawn_timer = pygame.time.get_ticks() |
33 | 25 |
|
34 | 26 | # Create an instance to store game statistics and create scoreboard.
|
35 | 27 | stats = GameStats(ai_settings)
|
36 | 28 | sb = Scoreboard(ai_settings, screen, stats)
|
37 | 29 |
|
38 |
| - # Make a ship, a group of bullets, and a group of aliens. |
39 |
| - ship = Ship(ai_settings, screen) |
| 30 | + health = Health(ai_settings, screen) |
| 31 | + health.init_health() |
| 32 | + |
| 33 | + # Make a ship, and a group for each game sprite. |
| 34 | + ship = Ship(ai_settings, input, screen) |
40 | 35 | bullets = Group()
|
41 | 36 | aliens = Group()
|
42 | 37 | cargoes = Group()
|
| 38 | + alien_bullets = Group() |
| 39 | + hearts = Group() |
| 40 | + shields = Group() |
| 41 | + |
| 42 | + # Make the play button. |
| 43 | + play_button = Button( |
| 44 | + screen, |
| 45 | + input, |
| 46 | + position=(screen.get_rect().centerx - 100, screen.get_rect().centery + 25), |
| 47 | + size=(200, 50), |
| 48 | + text="Play", |
| 49 | + foreground_color=(255, 255, 255), |
| 50 | + background_color=(0, 225, 0), |
| 51 | + border_width=0, |
| 52 | + display_condition=lambda: not stats.game_active and not stats.credits_active, |
| 53 | + on_clicked=lambda: gf.run_play_button(ai_settings, stats, ship, aliens, cargoes, bullets, health)) |
| 54 | + |
| 55 | + credits_button = Button( |
| 56 | + screen, |
| 57 | + input, |
| 58 | + position=(screen.get_rect().centerx - 100, screen.get_rect().centery + 100), |
| 59 | + size=(200, 50), |
| 60 | + text="Credits", |
| 61 | + foreground_color=(255, 255, 255), |
| 62 | + background_color=(0, 225, 0), |
| 63 | + border_width=0, |
| 64 | + display_condition=lambda: not stats.credits_active and not stats.game_active, |
| 65 | + on_clicked=lambda: gf.run_credit_button(stats)) |
| 66 | + |
| 67 | + back_button = Button( |
| 68 | + screen, |
| 69 | + input, |
| 70 | + position=(10, 50), |
| 71 | + size=(200, 50), |
| 72 | + text="Back", |
| 73 | + foreground_color=(255, 255, 255), |
| 74 | + background_color=(0, 225, 0), |
| 75 | + border_width=0, |
| 76 | + display_condition=lambda: stats.credits_active, |
| 77 | + on_clicked=lambda: gf.run_back_button(stats)) |
| 78 | + |
| 79 | + alien_spawn_counter = 0 |
43 | 80 |
|
44 |
| - # Create the fleet of aliens. |
45 |
| - gf.create_fleet(ai_settings, screen, ship, aliens, cargoes) |
| 81 | + gf.load_animations(screen, ai_settings) |
| 82 | + gf.load_credits() |
46 | 83 |
|
47 | 84 | # Start the main loop for the game.
|
48 | 85 | while True:
|
49 |
| - gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets, cargoes) |
| 86 | + input.update() |
| 87 | + gf.check_events(ai_settings, input, screen, stats, ship, bullets) |
50 | 88 | if stats.game_active:
|
51 |
| - ship.update() |
52 |
| - gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, cargoes) |
53 |
| - gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets, cargoes, sb) |
| 89 | + # Prevent mouse from going out of screen. |
| 90 | + pygame.event.set_grab(True) |
| 91 | + |
| 92 | + # Update game sprites |
| 93 | + gf.update_game_sprites(ai_settings, screen, stats, sb, ship, aliens, bullets, cargoes, alien_bullets, |
| 94 | + health, hearts, shields) |
| 95 | + else: |
| 96 | + pygame.event.set_grab(False) |
| 97 | + |
| 98 | + gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, credits_button, |
| 99 | + back_button, screen_bg, screen_bg_2, cargoes, alien_bullets, health, hearts, shields) |
54 | 100 |
|
55 |
| - gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, screen_bg, |
56 |
| - screen_bg_2, cargoes) |
57 | 101 | clock.tick(ai_settings.fps)
|
58 | 102 |
|
| 103 | + # Aliens fire timer |
| 104 | + current_time = pygame.time.get_ticks() |
| 105 | + |
| 106 | + if current_time - alien_spawn_timer > 100: |
| 107 | + gf.alien_fire(ai_settings, stats, screen, aliens, alien_bullets, ship) |
| 108 | + |
| 109 | + gf.generate_heart(ai_settings, stats, screen, hearts) |
| 110 | + gf.generate_shields(screen, ai_settings, stats, shields) |
| 111 | + |
| 112 | + if alien_spawn_counter % 10 == 0: |
| 113 | + gf.spawn_random_alien(ai_settings, screen, aliens) |
| 114 | + |
| 115 | + alien_spawn_counter += 1 |
| 116 | + alien_spawn_timer = current_time |
| 117 | + |
59 | 118 |
|
60 | 119 | run_game()
|
0 commit comments