-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
116 lines (99 loc) · 3.58 KB
/
main.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
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
import pygame
from player import Player
from enemy_wave import EnemyWave
from shot_manager import ShotManager
from pygame import mixer
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
mixer.music.load("assets/background.wav")
mixer.music.play(-1)
mixer.music.set_volume(0.08)
pygame.display.set_caption("Chris Space Invaders")
icon = pygame.image.load("assets/space_ship.png")
font = pygame.font.Font('assets/Formigas-Regular.otf', 30)
pygame.display.set_icon(icon)
player_move_speed = 10
enemy_move_speed_x = 6
enemy_move_speed_y = 0.5
player = Player()
enemyWave = EnemyWave()
shotManager = ShotManager()
lost = False
won = False
def reset():
global lost, won, player, enemyWave, shotManager, player_move_speed
lost = False
won = False
player = Player()
enemyWave = EnemyWave(number_of_enemies=30)
enemyWave.speed_x = enemy_move_speed_x
enemyWave.speed_y = enemy_move_speed_y
shotManager = ShotManager(speed=8, max_shots=8)
player_move_speed = 10
reset()
running = True
while running:
pygame.time.delay(100)
screen.fill((0, 0, 0))
dt = clock.tick(60)
if enemyWave.has_right_wall_collision():
enemyWave.speed_x = -enemy_move_speed_x
enemyWave.move_down()
elif enemyWave.has_left_wall_collision():
enemyWave.speed_x = enemy_move_speed_x
enemyWave.move_down()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.speed_x = -player_move_speed
elif event.key == pygame.K_RIGHT:
player.speed_x = player_move_speed
elif event.key == pygame.K_UP:
player.speed_y = -player_move_speed
elif event.key == pygame.K_DOWN:
player.speed_y = player_move_speed
elif event.key == pygame.K_SPACE:
if not lost:
shotManager.add_shot(for_player=player)
elif event.key == pygame.K_r:
reset()
elif event.type == pygame.KEYUP:
if (event.key == pygame.K_LEFT and player.speed_x < 0)\
or (event.key == pygame.K_RIGHT and player.speed_x > 0):
player.speed_x = 0
elif (event.key == pygame.K_UP and player.speed_y < 0)\
or (event.key == pygame.K_DOWN and player.speed_y > 0):
player.speed_y = 0
player.move()
enemyWave.move()
shotManager.move()
collided_shots = []
for shot in shotManager.shots:
did_collide = enemyWave.check_bullet_collision(shot)
if did_collide:
collided_shots.append(shot)
shotManager.remove(collided_shots)
enemyWave.remove_dead_enemies()
if len(enemyWave.enemies) == 0:
won = True
elif enemyWave.has_player_collision(player=player):
lost = True
enemyWave.speed_y = 0
enemyWave.speed_x = 0
player_move_speed = 0
if won:
won_text = font.render('You won!', True, (161, 220, 43))
screen.blit(won_text, (((screen.get_width() - won_text.get_width()) / 2),
(screen.get_height() - won_text.get_height()) / 2))
elif lost:
won_text = font.render('You lost! :(', True, (219, 66, 43))
screen.blit(won_text, (((screen.get_width() - won_text.get_width()) / 2),
(screen.get_height() - won_text.get_height()) / 2))
player.draw()
enemyWave.draw()
shotManager.draw()
pygame.display.update()
pygame.quit()