-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
224 lines (187 loc) · 7.09 KB
/
Copy pathmain.py
File metadata and controls
224 lines (187 loc) · 7.09 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import pygame, sys
from player import Player
import obstacle
from alien import Alien, Extra
from random import choice, randint
from laser import Laser
class Game:
def __init__(self):
# Player setup
player_sprite = Player((screen_width / 2,screen_height),screen_width,5)
self.player = pygame.sprite.GroupSingle(player_sprite)
# health and score setup
self.lives = 3
self.live_surf = pygame.image.load('C:\\New E Drive Files\\Web Development Files\\Python Projects\\Space Invaders\\Space-invaders-main\\graphics\\player.png').convert_alpha()
self.live_x_start_pos = screen_width - (self.live_surf.get_size()[0] * 2 + 20)
self.score = 0
self.font = pygame.font.Font('C:\\New E Drive Files\\Web Development Files\\Python Projects\\Space Invaders\\Space-invaders-main\\font\\Pixeled.ttf',20)
# Obstacle setup
self.shape = obstacle.shape
self.block_size = 6
self.blocks = pygame.sprite.Group()
self.obstacle_amount = 4
self.obstacle_x_positions = [num * (screen_width / self.obstacle_amount) for num in range(self.obstacle_amount)]
self.create_multiple_obstacles(*self.obstacle_x_positions, x_start = screen_width / 15, y_start = 480)
# Alien setup
self.aliens = pygame.sprite.Group()
self.alien_lasers = pygame.sprite.Group()
self.alien_setup(rows = 6, cols = 8)
self.alien_direction = 1
# Extra setup
self.extra = pygame.sprite.GroupSingle()
self.extra_spawn_time = randint(40,80)
# Audio
music = pygame.mixer.Sound('C:\\New E Drive Files\\Web Development Files\\Python Projects\\Space Invaders\\Space-invaders-main\\audio\\music.wav')
music.set_volume(0.2)
music.play(loops = -1)
self.laser_sound = pygame.mixer.Sound('C:\\New E Drive Files\\Web Development Files\\Python Projects\\Space Invaders\\Space-invaders-main\\audio\\laser.wav')
self.laser_sound.set_volume(0.5)
self.explosion_sound = pygame.mixer.Sound('C:\\New E Drive Files\\Web Development Files\\Python Projects\\Space Invaders\\Space-invaders-main\\audio\\explosion.wav')
self.explosion_sound.set_volume(0.3)
def create_obstacle(self, x_start, y_start,offset_x):
for row_index, row in enumerate(self.shape):
for col_index,col in enumerate(row):
if col == 'x':
x = x_start + col_index * self.block_size + offset_x
y = y_start + row_index * self.block_size
block = obstacle.Block(self.block_size,(241,79,80),x,y)
self.blocks.add(block)
def create_multiple_obstacles(self,*offset,x_start,y_start):
for offset_x in offset:
self.create_obstacle(x_start,y_start,offset_x)
def alien_setup(self,rows,cols,x_distance = 60,y_distance = 48,x_offset = 70, y_offset = 100):
for row_index, row in enumerate(range(rows)):
for col_index, col in enumerate(range(cols)):
x = col_index * x_distance + x_offset
y = row_index * y_distance + y_offset
if row_index == 0: alien_sprite = Alien('yellow',x,y)
elif 1 <= row_index <= 2: alien_sprite = Alien('green',x,y)
else: alien_sprite = Alien('red',x,y)
self.aliens.add(alien_sprite)
def alien_position_checker(self):
all_aliens = self.aliens.sprites()
for alien in all_aliens:
if alien.rect.right >= screen_width:
self.alien_direction = -1
self.alien_move_down(2)
elif alien.rect.left <= 0:
self.alien_direction = 1
self.alien_move_down(2)
def alien_move_down(self,distance):
if self.aliens:
for alien in self.aliens.sprites():
alien.rect.y += distance
def alien_shoot(self):
if self.aliens.sprites():
random_alien = choice(self.aliens.sprites())
laser_sprite = Laser(random_alien.rect.center,6,screen_height)
self.alien_lasers.add(laser_sprite)
self.laser_sound.play()
def extra_alien_timer(self):
self.extra_spawn_time -= 1
if self.extra_spawn_time <= 0:
self.extra.add(Extra(choice(['right','left']),screen_width))
self.extra_spawn_time = randint(400,800)
def collision_checks(self):
# player lasers
if self.player.sprite.lasers:
for laser in self.player.sprite.lasers:
# obstacle collisions
if pygame.sprite.spritecollide(laser,self.blocks,True):
laser.kill()
# alien collisions
aliens_hit = pygame.sprite.spritecollide(laser,self.aliens,True)
if aliens_hit:
for alien in aliens_hit:
self.score += alien.value
laser.kill()
self.explosion_sound.play()
# extra collision
if pygame.sprite.spritecollide(laser,self.extra,True):
self.score += 500
laser.kill()
# alien lasers
if self.alien_lasers:
for laser in self.alien_lasers:
# obstacle collisions
if pygame.sprite.spritecollide(laser,self.blocks,True):
laser.kill()
if pygame.sprite.spritecollide(laser,self.player,False):
laser.kill()
self.lives -= 1
if self.lives <= 0:
pygame.quit()
sys.exit()
# aliens
if self.aliens:
for alien in self.aliens:
pygame.sprite.spritecollide(alien,self.blocks,True)
if pygame.sprite.spritecollide(alien,self.player,False):
pygame.quit()
sys.exit()
def display_lives(self):
for live in range(self.lives - 1):
x = self.live_x_start_pos + (live * (self.live_surf.get_size()[0] + 10))
screen.blit(self.live_surf,(x,8))
def display_score(self):
score_surf = self.font.render(f'score: {self.score}',False,'white')
score_rect = score_surf.get_rect(topleft = (10,-10))
screen.blit(score_surf,score_rect)
def victory_message(self):
if not self.aliens.sprites():
victory_surf = self.font.render('You won',False,'white')
victory_rect = victory_surf.get_rect(center = (screen_width / 2, screen_height / 2))
screen.blit(victory_surf,victory_rect)
def run(self):
self.player.update()
self.alien_lasers.update()
self.extra.update()
self.aliens.update(self.alien_direction)
self.alien_position_checker()
self.extra_alien_timer()
self.collision_checks()
self.player.sprite.lasers.draw(screen)
self.player.draw(screen)
self.blocks.draw(screen)
self.aliens.draw(screen)
self.alien_lasers.draw(screen)
self.extra.draw(screen)
self.display_lives()
self.display_score()
self.victory_message()
class CRT:
def __init__(self):
self.tv = pygame.image.load('C:\\New E Drive Files\\Web Development Files\\Python Projects\\Space Invaders\\Space-invaders-main\\graphics\\tv.png').convert_alpha()
self.tv = pygame.transform.scale(self.tv,(screen_width,screen_height))
def create_crt_lines(self):
line_height = 3
line_amount = int(screen_height / line_height)
for line in range(line_amount):
y_pos = line * line_height
pygame.draw.line(self.tv,'black',(0,y_pos),(screen_width,y_pos),1)
def draw(self):
self.tv.set_alpha(randint(75,90))
self.create_crt_lines()
screen.blit(self.tv,(0,0))
if __name__ == '__main__':
pygame.init()
screen_width = 600
screen_height = 600
screen = pygame.display.set_mode((screen_width,screen_height))
clock = pygame.time.Clock()
game = Game()
crt = CRT()
ALIENLASER = pygame.USEREVENT + 1
pygame.time.set_timer(ALIENLASER,800)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == ALIENLASER:
game.alien_shoot()
screen.fill((30,30,30))
game.run()
#crt.draw()
pygame.display.flip()
clock.tick(60)