-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceInvaders.py
More file actions
125 lines (100 loc) · 3.87 KB
/
SpaceInvaders.py
File metadata and controls
125 lines (100 loc) · 3.87 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Ctrl + Shift + P, then select interpreter
# Choose an interpreter that works
import pygame
from hero import Hero
from enemy import Enemy
from fleet import Fleet
from game_object import Game_Object
# Game settings
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 600
GAME_SIDE_MARGIN = 20
GAME_TOP_MARGIN = 20
GAME_BOTTOM_MARGIN = 20
GAME_BORDER_WIDTH = 3
GAME_TOP_WALL = GAME_TOP_MARGIN + GAME_BORDER_WIDTH
GAME_RIGHT_WALL = WINDOW_WIDTH - GAME_SIDE_MARGIN - GAME_BORDER_WIDTH
GAME_BOTTOM_WALL = WINDOW_HEIGHT - GAME_BOTTOM_MARGIN - GAME_BORDER_WIDTH
GAME_LEFT_WALL = GAME_SIDE_MARGIN + GAME_BORDER_WIDTH
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
pygame.init()
# Media files
player_image = pygame.image.load('media/si-player.gif')
bullet_image = pygame.image.load('media/si-bullet.gif')
enemy_image = pygame.image.load('media/si-enemy.gif')
laser_sound = pygame.mixer.Sound('media/si-laser.wav')
explosion_sound = pygame.mixer.Sound('media/si-explode.wav')
clock = pygame.time.Clock()
game_display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
score_font = pygame.font.SysFont('Arial', 22, True)
title_font = pygame.font.SysFont('Arial', 26, True)
pygame.display.set_caption('SPACE INVADERS!')
def handle_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
hero.is_alive = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
hero.set_direction_left()
elif event.key == pygame.K_RIGHT:
hero.set_direction_right()
elif event.key == pygame.K_SPACE:
laser_sound.play()
hero.shoot(bullet_image)
elif event.key == pygame.K_p:
pause_game()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
hero.set_direction_none()
elif event.key == pygame.K_RIGHT:
hero.set_direction_none()
def show_background():
game_display.blit(game_display, (0, 0))
game_display.fill(BLACK)
pygame.draw.rect(game_display, WHITE,
(GAME_SIDE_MARGIN, GAME_TOP_MARGIN,
WINDOW_WIDTH - GAME_SIDE_MARGIN * 2,
WINDOW_HEIGHT - GAME_BOTTOM_MARGIN * 2))
pygame.draw.rect(game_display, BLACK,
(GAME_LEFT_WALL, GAME_TOP_WALL,
WINDOW_WIDTH - GAME_LEFT_WALL - GAME_SIDE_MARGIN - GAME_BORDER_WIDTH,
WINDOW_HEIGHT - GAME_TOP_WALL - GAME_BOTTOM_MARGIN - GAME_BORDER_WIDTH))
def pause_game():
is_paused = True
while is_paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_paused = False
hero.is_alive = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
is_paused = False
hero = Hero(player_image, 200, GAME_BOTTOM_WALL - player_image.get_height())
fleet = Fleet(3, 5, 4, enemy_image, GAME_LEFT_WALL + 1, GAME_TOP_WALL + 1)
# Main Game Loop
while hero.is_alive:
handle_events()
fleet.handle_wall_collision(GAME_LEFT_WALL, GAME_RIGHT_WALL)
hero.handle_wall_collision_for_bullets(GAME_TOP_WALL)
for bullet in hero.bullets_fired:
for ship in fleet.ships:
if bullet.has_collided_with(ship):
bullet.is_alive = False
ship.is_alive = False
explosion_sound.play()
fleet.remove_dead_ships()
hero.move_all_bullets()
hero.move(GAME_LEFT_WALL, GAME_RIGHT_WALL)
fleet.move_over(GAME_LEFT_WALL, GAME_RIGHT_WALL)
show_background()
hero.show(game_display)
fleet.show(game_display)
hero.show_all_bullets(game_display)
# score_text = score_font.render(str(snake.score), False, WHITE)
# game_display.blit(score_text, (0,0))
pygame.display.update()
clock.tick(30)
pygame.display.quit()
pygame.quit