-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalien.py
More file actions
92 lines (80 loc) · 3.04 KB
/
Copy pathalien.py
File metadata and controls
92 lines (80 loc) · 3.04 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
import pygame
from pygame import Vector2, Surface
import constants
import Text
import random
class Alien(pygame.sprite.Sprite):
def __init__(self, type_input: str, x: int, y: int, hitbox_x: int, hitbox_y: int) -> None:
pygame.sprite.Sprite.__init__(self)
self.type = type_input
self.hitbox: Vector2 = Vector2(hitbox_x, hitbox_y)
self.pos: Vector2 = Vector2(x, y)
self.images: list = get_alien_images(self.type, self.hitbox)
self.score: int = self.calculate_score()
# managing death animation
self.is_dead: bool = False
self.death_timer: float = 0.4 if self.type == "spaceship" else 0.25
def get_pos(self) -> Vector2:
return self.pos
def get_hitbox(self) -> Vector2:
return self.hitbox
def kill(self) -> None:
self.is_dead = True
def death_animation_over(self, dt: float) -> bool:
self.death_timer -= dt
return self.death_timer < 0
def has_died(self) -> bool:
return self.is_dead
def update_pos(self, new_x: int, new_y: int) -> None:
self.pos.x = new_x
self.pos.y = new_y
def calculate_score(self) -> int:
alien_score: int = 0
match self.type:
case "shooter":
alien_score = 40
case "brute":
alien_score = 20
case "tank":
alien_score = 10
case "spaceship":
alien_score = random.randint(1, 5) * 50
if alien_score == 250:
alien_score = 300
case _:
print("alien has no score")
return alien_score
def get_score(self) -> int:
return self.score
def draw(self, screen: Surface, swarm_tic: int) -> None:
idx: int = 0
if self.is_dead:
idx = len(self.images) - 1
else:
idx = swarm_tic % 2
if self.type == "spaceship" and self.is_dead:
Text.text_to_screen(screen, self.score, self.pos.x, self.pos.y, 30, (255, 255, 255))
return None
screen.blit(self.images[idx], (self.pos.x, self.pos.y))
def get_alien_images(alien_type: str, hitbox: Vector2) -> list:
images: list = []
# if spaceship only add image of spaceship
if alien_type == "spaceship":
image: Surface = pygame.image.load("alien_images/spaceship.png")
image = pygame.transform.scale(image, (80, 35))
image.convert_alpha()
images.append(image)
return images
# add images for alien to list
for i in range(1, 3):
total_file_path: str = "alien_images/"+alien_type+str(i)+".png"
image: Surface = pygame.image.load(total_file_path)
image = pygame.transform.scale(image, (hitbox.x, hitbox.y))
image.convert_alpha()
images.append(image)
# insert death image at end of list
image: Surface = pygame.image.load("alien_images/alien_dead.png")
image = pygame.transform.scale(image, (hitbox.x, hitbox.y))
image.convert_alpha()
images.append(image)
return images