-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroid.py
75 lines (65 loc) · 2.21 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
################################################
# File: ship.py
# Writer: Asif Kagan, Dor Roter
# Login: asifka, dor.roter
# Exercise: ------
# More:
# Consulted: -----
# Internet:
# Notes:
# ################################################
from game_object import GameObject
import math
def get_radius(size):
"""
Private utility function to calculate the asteroids radius based on a given
size.
:param size: an asteroids size
:type size: int
:return: int
"""
return (size * 10) - 5
class Asteroid(GameObject):
"""
A class extending GameObject describing an asteroid in our game
"""
def __init__(self, coordinate, speed_vec, size):
"""
Constructor method to generate an asteroid
:param coordinate: a tuple representing the x,y initial coordinates
:type coordinate: Tuple: (int, int)
:param speed_vec: a tuple representing the x,y initial speed
:type speed_vec: Tuple: (int, int)
:param size: the size of the asteroid (1-3)
:type size: int
"""
# todo - validate size 1-3 and int
self.__size = size
super().__init__(coordinate, speed_vec, get_radius(size))
def get_size(self):
"""
Method to get the size of an asteroid
:return: int
"""
return self.__size
def has_intersection(self, game_object):
"""
Method to check if a given game object has crashed into this asteroid
:param game_object: the game object to preform the check against
:type game_object: GameObject
:return: bool
"""
distance = self.__get_distance(game_object)
return distance <= (self._radius + game_object.get_radius())
def __get_distance(self, game_object):
"""
Private function to calculate thr distance of a given object from this
asteroid.
:param game_object: a game object on to calculate its distance from this
:type game_object: GameObject
:return: float
"""
obj_x, obj_y = game_object.get_coordinates()
self_x, self_y = self._coordinates
inner = (obj_x-self_x)**2 + (obj_y-self_y)**2
return math.sqrt(inner)