-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalien_invasion.py
78 lines (61 loc) · 2.71 KB
/
alien_invasion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
import pygame
from settings import Settings # screen size and bg_color
from ship import Ship # ship location in the screen
from alien import Alien
import game_functions as gf # event check and screen update
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard
def run_game():
# initialize the game, screen object,setting
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height)) # define a tuple which indicates screen size, width:1200, height:800.
pygame.display.set_caption("Alien Invasion")
# create Play button
play_button = Button(ai_settings,screen,"Play")
# create an instance used to store game statistical information
stats = GameStats(ai_settings)
sb = Scoreboard(ai_settings,screen,stats)
# create a ship
ship = Ship(ai_settings,screen) # screen is the second location parameter
# create a group to contain bullets
bullets = Group()
aliens = Group()
# create aliens
gf.create_fleet(ai_settings,screen,ship,aliens)
# set background color
# bg_color = (230,230,230)
# start the main loop of game
# create an alien
alien = Alien(ai_settings, screen)
while True:
gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets)
# # monitor the keyboard and mouse
# for event in pygame.event.get(): # event loop
# if event.type == pygame.QUIT: # to capture keyboard or mouse state, use method pygame.event.get()
# sys.exit() # if the state is active, then it indicates the while loop is true.
# # use sys module to quit the game
if stats.game_active:
ship.update()
gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)
# bullets.update()
#
# # delete vanished bullet
# for bullet in bullets.copy(): # not delete bullet in Group but its copy group
# if bullet.rect.bottom <= 0:
# bullets.remove(bullet)
# # print(len(bullets)) # to check the left bullets in Group
gf.update_aliens(ai_settings, screen,stats, sb,ship, aliens, bullets)
gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button)
# # recreate screen once loop
# screen.fill(ai_settings.bg_color)
#
# ship.blitme()
#
# # let the newest created screen visible
# pygame.display.flip() # update the screen state
run_game()