-
Notifications
You must be signed in to change notification settings - Fork 0
/
ship.py
105 lines (92 loc) · 2.85 KB
/
ship.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
################################################
# File: ship.py
# Writer: Asif Kagan, Dor Roter
# Login: asifka, dor.roter
# Exercise: ------
# # More:
# Consulted: -----
# Internet:
# Notes:
# ################################################
from game_object import GameObject
from torpedo import Torpedo
import math
class Ship(GameObject):
"""
A class extending GameObject describing a spaceship in our game
"""
# public static constants
RADIUS = 1
TURN = 7
MAX_HEADING = 360
MIN_HEADING = 0
def __init__(self, coordinate, speed_vec, heading):
"""
Constructor method to generate a ship
: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 speeds
:type speed_vec: Tuple: (int, int)
:param heading: the initial heading in degrees of the torpedo
:type heading: int
"""
self.__heading = heading
super().__init__(coordinate, speed_vec, Ship.RADIUS)
def turn_right(self):
"""
Method to turn ship right by Ship.TURN degrees
"""
turn = self.__heading - Ship.TURN
if turn < Ship.MIN_HEADING:
turn += Ship.MAX_HEADING
self.__heading = turn
def turn_left(self):
"""
Method to turn ship left by Ship.TURN degrees
"""
turn = self.__heading + Ship.TURN
if turn >= Ship.MAX_HEADING:
turn -= Ship.MAX_HEADING
self.__heading = turn
def accelerate(self):
"""
Method to accelerate the ship
"""
x_speed = self.__calc_speed(Ship._X)
y_speed = self.__calc_speed(Ship._Y)
self._speed_vect = (x_speed, y_speed)
def fire_torpedo(self):
"""
Method to fire a torpedo
:return: Torpedo
"""
return Torpedo(self)
def get_draw_data(self):
"""
Method to get a tuple representing all necessary data for a draw.
:return: Tuple (int, int, int)
"""
x, y = self._coordinates
return x, y, self.__heading
def get_heading(self):
"""
Method to get the current ship heading
:return: int
"""
return self.__heading
def __calc_speed(self, axis):
"""
Private function to calculate and return the new speed of the ship after
after accelerating.
:param axis: 0/1 representing x or y axis
:type axis: int
:return: float
"""
old_speed = self._speed_vect[axis]
radian = math.radians(self.__heading)
if axis == Ship._X:
heading_factor = math.cos(radian)
else:
# axis == Ship.Y
heading_factor = math.sin(radian)
return old_speed + heading_factor