-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities.py
231 lines (189 loc) · 6.95 KB
/
entities.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import math
from random import random
from typing import Tuple, List
import numpy as np
from pygame.surface import Surface
import resourceloader
import util
from config import *
from util import Vector2D
class Entity(pygame.sprite.Sprite):
"""
Base Class for all Entities
self.image and self.rect should be overwritten
"""
MAX_SPEED = 5.0
def __init__(self, pos, size=(10, 10), color=GREY, image=None):
super().__init__()
if size is not None:
if not image:
self.image = pygame.Surface(size)
self.image.fill(color)
else:
self.image = image
self.orig_image = self.image.copy()
self.rect = self.image.get_rect()
assert type(pos) is np.ndarray
self.pos = pos
self.acceleration: Tuple[float, float] = np.array((0.0, 0.0))
self.speed: Tuple[float, float] = np.array((0.0, 0.0))
@property
def pos(self):
return self.real_pos
@pos.setter
def pos(self, pos):
self.real_pos = pos
x, y = pos
self.rect.x = round(x)
self.rect.y = round(y)
@property
def speed(self):
return self.__speed
@speed.setter
def speed(self, speed):
# Max speed check
if np.linalg.norm(speed) > self.MAX_SPEED:
normed_speed = speed / np.linalg.norm(speed)
speed = normed_speed * self.MAX_SPEED
self.__speed = speed
self.rotate_img()
def move(self):
if not np.all(self.acceleration == np.zeros((2, 1))):
self.speed += self.acceleration
self.pos = np.add(self.pos, self.speed)
self.check_for_walkout()
def rotate_img(self):
x, y = self.speed / np.linalg.norm(self.pos)
self.image = pygame.transform.rotate(self.orig_image, math.atan2(-y, x) * 180 / math.pi)
def check_for_walkout(self):
for axis in (0, 1):
if 0 > self.pos[axis] or self.pos[axis] > GAMESIZE[axis]:
self.pos[axis] = self.pos[axis] % GAMESIZE[axis]
def update(self):
self.move()
class Player(Entity):
MAX_SPEED = 5.0
KEY_MAPPING = KEY_MAPPING
def __init__(self, pos):
self.image_still = resourceloader.get_image_scaled("spaceship", (48, 24))
self.animation = Animation(10,
(resourceloader.get_image_scaled("spaceship_move_0", (48, 24)),
resourceloader.get_image_scaled("spaceship_move_1", (48, 24))))
image = self.image_still
self.rotation = 0
super().__init__(pos, image=image)
self.mouse_position = np.array((0, 0))
self.reload_counter = 0
self.accelerating = False
def update(self):
if not self.accelerating:
self.animation.reset()
self.image = self.image_still
self.orig_image = self.image.copy()
else:
self.image = self.animation.update()
self.orig_image = self.image.copy()
self.rotate_img()
self.move()
self.accelerating = False
def rotate_img(self):
self.image = pygame.transform.rotate(self.orig_image, -self.rotation)
@property
def direction(self):
return util.rotate(np.array((1.0, 0.0)), self.rotation)
@direction.setter
def direction(self, dir):
x, y = dir[0], dir[1]
self.rotation = math.atan2(-y, x) * 180 / math.pi
def turn_left(self):
self.rotation -= TURN_SPEED
self.rotate_img()
def turn_right(self):
self.rotation += TURN_SPEED
self.rotate_img()
def accelerate(self):
self.speed += util.rotate(ACCELERATION_DELTA, self.rotation)
self.accelerating = True
def can_shoot(self):
if not self.alive():
return False
if self.reload_counter == 0:
self.reload_counter = RELOAD_TIME
return True
else:
self.reload_counter += -1
return False
class Asteroid(Entity):
MAX_SPEED = 4.0
MIN_SIZE = 20
MAX_SIZE = 50
MIN_DISTANCE_PLAYER = 40
SPAWN_CHANCE = DESTROIT_SPAWN_CHANCE
def __init__(self, player_pos=None, pos=None, move_direction=None,
size=None):
if size is None:
self.__size = round(random() * (self.MAX_SIZE - self.MIN_SIZE) + self.MIN_SIZE)
else:
self.__size = round(size)
image = resourceloader.get_image_scaled("destroit", (self.__size, self.__size))
if pos is None:
pos = self.calc_spawn_pos(player_pos)
super().__init__(pos, size=np.ones((2, 1)) * self.__size, image=image)
if move_direction is not None:
self.speed = move_direction
elif player_pos is not None:
self.speed = self.calc_move_direction(player_pos)
else:
Exception("Need to provide either player_pos or move_direction")
@property
def points(self):
return round(self.__size * (self.MAX_SIZE * 2 - DESTROIT_POINT_SCALE_FACTOR))
def calc_spawn_pos(self, player_pos):
distance_player = self.MIN_DISTANCE_PLAYER - 1
while self.MIN_DISTANCE_PLAYER > distance_player:
pos = np.array((random() * GAMESIZE.x, random() * GAMESIZE.y))
distance_player = np.linalg.norm(pos - player_pos)
return pos
def calc_move_direction(self, player_pos):
rel_pos = (player_pos - self.pos)
return rel_pos / np.linalg.norm(rel_pos) * self.MAX_SPEED * random()
def kill(self):
new_size = self.__size / 2
if new_size > self.MIN_SIZE:
a1 = Asteroid(pos=np.copy(self.pos), move_direction=util.rotate(self.speed, -10),
size=new_size)
a2 = Asteroid(pos=np.copy(self.pos), move_direction=util.rotate(self.speed, 10),
size=new_size)
a1.add(self.groups())
a2.add(self.groups())
super(Asteroid, self).kill()
class Bullet(Entity):
MAX_SPEED = 5.0
SIZE = (10, 2)
def __init__(self, pos, direction):
image = resourceloader.get_image("bullet")
super().__init__(pos, size=Bullet.SIZE, image=image)
self.speed = Bullet.MAX_SPEED * np.array(direction)
def check_for_walkout(self):
for axis in (0, 1):
if 0 > self.pos[axis] or self.pos[axis] > GAMESIZE[axis]:
self.kill()
class Animation:
def __init__(self, frame_length, images):
self.images = images
self.index = 0
self.frame_length = frame_length
self.frame_time = 0
def reset(self):
self.index = 0
def update(self):
if self.frame_time == round(self.frame_length / len(self.images)):
self.frame_time = 0
self.index += 1
else:
self.frame_time += 1
if self.index >= len(self.images):
self.index = 0
return self.images[self.index]
def get_image(self, index):
return self.images[index]