-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalien.py
145 lines (131 loc) · 4.85 KB
/
alien.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
import pygame
from constants import *
class Alien(pygame.sprite.Sprite):
# Initialize the sound module
pygame.mixer.init()
alien_explosion_sound = pygame.mixer.Sound(ALIEN_EXPLOSION_SOUND)
def __init__(self, position_x, position_y, row, column, alien_paths, points, alien_color=WHITE):
super().__init__()
try:
self.alien_paths = alien_paths
self.surface = pygame.image.load(alien_paths[0]).convert_alpha()
self.surface.set_colorkey(BLACK, pygame.RLEACCEL)
except FileNotFoundError:
self.width = SCREEN_WIDTH // 11 - 5
self.height = SCREEN_HEIGHT // 20
self.surface = pygame.Surface((self.width, self.height))
self.surface.fill(alien_color)
# Top left corner position coordinates
self.corner = self.surface.get_rect(center=(position_x, position_y))
# coordinates in the fleet
self.row = row
self.column = column
# Alien movement speed
self.alien_movement = ALIEN_MOVEMENT
# Direction
self.direction = 1
self.step_down_amount = 0
# animation iterator
self.anim_iterator = 0
# destruction start time
self.destruct_start_time = None
# points received for alien destruction
self.points = points
# destruction fleet
self.fleet_group = []
def animate(self):
"""Animates alien by swapping alien sprites."""
if self.anim_iterator == 0:
self.anim_iterator = 1
else:
self.anim_iterator = 0
# change image path to create animation effect
self.surface = pygame.image.load(self.alien_paths[self.anim_iterator]).convert_alpha()
def hit_sound(self):
"""
Plays alien explosion sound.
:return:
"""
self.alien_explosion_sound.play()
def move(self):
"""Moves alien."""
# check whether alien is to be destroyed
if not self.update_destroyed():
# animate
self.animate()
# move
self.corner.move_ip(self.direction * self.alien_movement, self.step_down_amount)
def step_down(self):
"""Moves alien down one line closer to the player."""
# check whether alien is to be destroyed
if not self.update_destroyed():
# increase step_down_amount coordinates
self.step_down_amount += ALIEN_HEIGHT
# step down
self.corner.move_ip(self.direction * self.alien_movement, self.step_down_amount)
self.step_down_amount = 0
def update_destroyed(self):
"""
Check whether DESTRUCTION_TIME has elapsed.
:return: bool
"""
# check whether alien is to be destroyed
if self.destruct_start_time and (pygame.time.get_ticks() - self.destruct_start_time >= DESTRUCTION_TIME):
self.destroy()
return True
return False
def init_destruction(self, fleet):
"""
Initiates alien destruction.
:param fleet: list[alien.Alien]
:return:
"""
# show alien explosion
self.surface = pygame.image.load(ALIEN_EXPLOSION).convert_alpha()
self.surface.set_colorkey(BLACK, pygame.RLEACCEL)
# get current time in milliseconds
self.destruct_start_time = pygame.time.get_ticks()
# remember the row
self.fleet_group = fleet
# play explosion sound
self.hit_sound()
def destroy(self):
"""
Destroys alien.
:return:
"""
# calculate position in the fleet
position = self.row * COLUMNS + self.column
# remove alien - replace with None
self.fleet_group[position] = None
# reset destruction start time
self.destruct_start_time = None
# destroy alien
self.kill()
def wall_collision(self, wall_group_list):
"""
Detects alien collision with the walls in wall_group_list.
:param wall_group_list: list[pygame.sprite.Group()]
:return:
"""
for wall_group in wall_group_list:
for wall_piece in wall_group:
if self.corner.colliderect(wall_piece.corner):
# destroy wall_piece on collision with alien
wall_piece.kill()
def out_of_screen(self):
"""
Detects whether alien has left the screen.
:return: bool
"""
# If alien gets out of screen (reaches the bottom green HUD line)
if self.corner.bottom >= SCREEN_HEIGHT - 70:
return True
return False
def collision_detection(self, wall_group_list):
"""
Collision detection parent method. Calls collision detection methods.
:param wall_group_list: list[pygame.sprite.Group()]
:return:
"""
self.wall_collision(wall_group_list)