-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroid.py
51 lines (47 loc) · 2.38 KB
/
asteroid.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
from math import copysign
from random import randint
from const import Const
from enemy import Enemy
from time import perf_counter as clock
class Asteroid(Enemy):
def __init__(self, images, speed, acceleration, x=0, y=0):
super().__init__(images=images, speed=speed, x=x, y=y)
self.__set_asteroid_random_size()
self.current_image_set = self.images
self.width, self.height = self.images[0].get_rect().size
hitsize_delta = self.width // Const.ASTEROID_HITSIZE_COEFFICIENT
self.hitsize = (hitsize_delta, hitsize_delta,
self.width - hitsize_delta, self.height - hitsize_delta)
self.acceleration = acceleration
self.score = int(self.original_images[0].get_rect().size[0] - self.width) * 10 # More score for small ones
# Animation speed is affected by the asteroid's vertical speed
self.frame_time = (1 - self.speed[1] / Const.ASTEROID_SPEED_VERTICAL_MAX) / Const.ASTEROID_ANIMATE_COEFFICIENT
def __set_asteroid_random_size(self):
"""
Asteroids are being created with random sizes, which means we need to resize all asteroid images
"""
orig_width, _ = self.original_images[0].get_rect().size
# Alter the original size only if the current size is above the minimum
if Const.ASTEROID_MIN_SIZE < orig_width:
new_size = randint(Const.ASTEROID_MIN_SIZE, orig_width) # Assuming the asteroids are square images
self.images = self.rescale(images_source=self.original_images, scale_x=new_size, scale_y=new_size)
def get_current_pic(self):
"""
Return the current picture.
"""
# Change the picture
if clock() > self.next_frame:
if self.exploding:
if self.frame_num < self.num_of_explosion_frames:
self.frame_num += 1
else:
self.away = True
else:
# Chose animation direction by the asteroid's horizontal direction
self.frame_num += int(copysign(1, self.speed[0]+self.acceleration[0]))
if self.frame_num >= self.num_of_images:
self.frame_num = 0
elif self.frame_num < 0:
self.frame_num = self.num_of_images - 1
self.next_frame = clock() + self.frame_time
return self.current_image_set[self.frame_num]