-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
184 lines (144 loc) · 5.93 KB
/
game.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
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
import pygame
from ntpath import join
from random import randint, uniform
class Player(pygame.sprite.Sprite):
def __init__(self, groups):
super().__init__(groups)
self.image = pygame.image.load('player.png').convert_alpha()
self.rect = self.image.get_frect(center = (WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2))
self.direction = pygame.Vector2()
self.speed = 300
# cooldown
self.can_shoot = True
self.laser_shoot_time = 0
self.cooldown_duration = 400
# mask
self.mask = pygame.mask.from_surface(self.image)
def laser_timer(self):
if not self.can_shoot:
current_time = pygame.time.get_ticks()
if current_time - self.laser_shoot_time >= self.cooldown_duration:
self.can_shoot = True
def update(self, dt):
keys = pygame.key.get_pressed()
self.direction.x = int(keys[pygame.K_RIGHT]) - int(keys[pygame.K_LEFT])
self.direction.y = int(keys[pygame.K_DOWN]) - int(keys[pygame.K_UP])
self.direction = self.direction.normalize() if self.direction else self.direction
self.rect.center += self.direction * self.speed * dt
recent_keys = pygame.key.get_just_pressed()
if recent_keys[pygame.K_SPACE] and self.can_shoot:
Laser(laser_surf, self.rect.midtop, (all_sprites, laser_sprites))
self.can_shoot = False
self.laser_shoot_time = pygame.time.get_ticks()
laser_sound.play()
self.laser_timer()
class Star(pygame.sprite.Sprite):
def __init__(self, groups, surf):
super().__init__(groups)
self.image = surf
self.rect = self.image.get_frect(center = (randint(0, WINDOW_WIDTH),randint(0, WINDOW_HEIGHT)))
class Laser(pygame.sprite.Sprite):
def __init__(self, surf, pos, groups):
super().__init__(groups)
self.image = surf
self.rect = self.image.get_frect(midbottom = pos)
def update(self, dt):
self.rect.centery -= 400 * dt
if self.rect.bottom < 0:
self.kill()
class Meteor(pygame.sprite.Sprite):
def __init__(self, surf, pos, groups):
super().__init__(groups)
self.original_surf = surf
self.image = surf
self.rect = self.image.get_frect(center = pos)
self.start_time = pygame.time.get_ticks()
self.lifetime = 3000
self.direction = pygame.Vector2(uniform(-0.5, 0.5),1)
self.speed = randint(400,500)
self.rotation_speed = randint(40,80)
self.rotation = 0
def update(self, dt):
self.rect.center += self.direction * self.speed * dt
if pygame.time.get_ticks() - self.start_time >= self.lifetime:
self.kill()
self.rotation += self.rotation_speed * dt
self.image = pygame.transform.rotozoom(self.original_surf, self.rotation, 1)
self.rect = self.image.get_frect(center = self.rect.center)
class AnimatedExplosion(pygame.sprite.Sprite):
def __init__(self, frames, pos, groups):
super().__init__(groups)
self.frames = frames
self.frame_index = 0
self.image = self.frames[self.frame_index]
self.rect = self.image.get_frect(center = pos)
explosion_sound.play()
def update(self, dt):
self.frame_index += 20 * dt
if self.frame_index < len(self.frames):
self.image = self.frames[int(self.frame_index)]
else:
self.kill()
def collisions():
global running
collision_sprites = pygame.sprite.spritecollide(player, meteor_sprites, True, pygame.sprite.collide_mask)
if collision_sprites:
running = False
for laser in laser_sprites:
collided_sprites = pygame.sprite.spritecollide(laser, meteor_sprites, True)
if collided_sprites:
laser.kill()
AnimatedExplosion(explosion_frames, laser.rect.midtop, all_sprites)
def display_score():
current_time = pygame.time.get_ticks() // 100
text_surf = font.render(str(current_time), True, (240,240,240))
text_rect = text_surf.get_frect(midbottom = (WINDOW_WIDTH / 2,WINDOW_HEIGHT - 50))
display_surface.blit(text_surf, text_rect)
pygame.draw.rect(display_surface, (240,240,240), text_rect.inflate(20,10).move(0,-8), 5, 10)
# general setup
pygame.init()
WINDOW_WIDTH, WINDOW_HEIGHT = 1280, 720
display_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Space Shooter')
running = True
clock = pygame.time.Clock()
# import
star_surf = pygame.image.load('star.png').convert_alpha()
meteor_surf = pygame.image.load('meteor.png').convert_alpha()
laser_surf = pygame.image.load('laser.png').convert_alpha()
font = pygame.font.Font('Oxanium-Bold.ttf', 40)
explosion_frames = [pygame.image.load(join('explosion', f'{i}.png')).convert_alpha() for i in range(21)]
laser_sound = pygame.mixer.Sound('laser.wav')
laser_sound.set_volume(0.5)
explosion_sound = pygame.mixer.Sound('explosion.wav')
game_music = pygame.mixer.Sound('game_music.wav')
game_music.set_volume(0.4)
# game_music.play(loops= -1)
# sprites
all_sprites = pygame.sprite.Group()
meteor_sprites = pygame.sprite.Group()
laser_sprites = pygame.sprite.Group()
for i in range(20):
Star(all_sprites, star_surf)
player = Player(all_sprites)
# custom events -> meteor event
meteor_event = pygame.event.custom_type()
pygame.time.set_timer(meteor_event, 200)
while running:
dt = clock.tick() / 1000
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == meteor_event:
x, y = randint(0, WINDOW_WIDTH), randint(-200, -100)
Meteor(meteor_surf, (x, y), (all_sprites, meteor_sprites))
# update
all_sprites.update(dt)
collisions()
# draw the game
display_surface.fill('#3a2e3f')
display_score()
all_sprites.draw(display_surface)
pygame.display.update()
pygame.quit()