This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.py
133 lines (124 loc) · 5.04 KB
/
element.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
from pygame import Surface
from math import (sqrt)
class Element():
def __init__(self, surface: Surface, x: int, y: int, visible: bool = True, allow_override: bool = False) -> None:
"""The base class for all the elements in the game.
Args:
surface (Surface): The surface where the element will be drawn.
x (int): The x coordinate of the element.
y (int): The y coordinate of the element.
visible (bool, optional): If the element is visble or not . Defaults to True.
allow_override (bool, optional): If the element can be override. Defaults to False.
"""
self.__surface = surface
# those are the inital x and y coordinates. (those coordinates are immutable)
self.__ix, self.__iy = x, y
# those are the mutable x and y coordinates.
self.__x, self.__y = x, y
self.__visible = visible
self.__allow_override = allow_override
pass
def set_surface(self, surface:Surface):
self.__surface = surface
def get(self) -> tuple:
"""
:return: the whole element.
"""
return (self.__surface, (self.__x, self.__y))
def get_position(self) -> tuple:
"""
:return: the x & y coordinates.
"""
return self.__x, self.__y
def get_surface(self) -> Surface:
"""
:return: the surface.
"""
return self.__surface
def get_initial_x(self, relative:bool):
"""
:param relative bool: whether whe should get it from the center or not.
:return: the initial immutable x coordinate of the element.
"""
return self.__ix if not relative else (self.get_surface().get_size()[0] / 2) + self.__ix
def get_initial_y(self, relative:bool):
"""
:param relative bool: whether we should get it from the center or not.
:return: the initial immutable x coordinate of the element.
"""
return self.__iy if not relative else (self.get_surface().get_size()[0] / 2) + self.__iy
def get_x(self):
"""
:return: the live x position of the element.
"""
return self.__x
def get_y(self):
"""
:return: the live y position of the element.
"""
return self.__y
def allow_override(self):
"""
:return: whether the element can be overrident or not.
(another element can erase this one.)
"""
return self.__allow_override
def is_visible(self):
"""
:return: whether the element is visible or not. (Note it can still interact.)
"""
return self.__visible
def set_visible(self, visible: bool):
"""
Set the visibility of the element.
:param: bool visible: whether the element is visible or not.
"""
self.__visible = visible
def set_x(self, x: int):
"""
Set the x position of the element.
:param int x: the x position of the element.
"""
self.__x = x
def set_y(self, y: int):
"""
Set the y position of the element.
:param int y: the y position of the element.
"""
self.__y = y
def center_x(self):
"""
:return: the x center of image.
"""
return (self.get_surface().get_size()[0] / 2) + self.get_x()
def center_y(self):
"""
:return: the y center of image.
"""
return (self.get_surface().get_size()[0] / 2) + self.get_y()
def distance_two_points(self, x, y, x1, y1):
"""
:return: the euclidian distance between 4 points in the space.
:param int x: the x position to compare with.
:param int y: the y position to compare with.
:param int x1: the x' position to compare with.
:param int y1: the y' position to compare with.
"""
return sqrt(((x - x1) ** 2) + ((y - y1) ** 2))
def distance(self, x, y, from_center:bool):
"""
:return: the euclidian distance between this element and x, y coordinates
:param int x: the x position to compare with.
:param int y: the y position to compare with.
:param bool from_center: whether we should calculate the euclidian distance
from the center of the surface or not.
"""
return sqrt(((self.center_x() - x) ** 2) + ((self.center_y() - y) ** 2)) if from_center else sqrt((self.get_x() - x) ** 2) + ((self.get_y() - y) ** 2)
def distance_element(self, element, from_center:bool):
"""
:return: the euclidian distance between this element and another one.
:param Element element: the element to compare with.
:param bool from_center: whether we should calculate the euclidian distance
from the center of the surface or not.
"""
return self.distance(element.get_x(), element.get_y()) if not from_center else self.distance(element.center_x(), element.center_y())