This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframework.py
377 lines (340 loc) · 12.8 KB
/
framework.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from functions import *
import time
import math
import threading
import random
class Game:
def __init__(self, radius=2000):
self.entities = set()
self.running_time = 0.01
self.radius = radius
self.last_time = time.time()
self.mapped_entities = dict()
def update(self):
self.running_time = round((time.time()-self.last_time)*1000)
self.last_time = time.time()
for entity in list(self.entities):
try:
entity.act()
except Exception as e:
print("Error occured while executing act function of entity "+entity.it)
print(type(e), e)
thr = threading.Thread(target=self.update)
thr.start()
def add_entity(self, entity):
self.entities.add(entity)
if entity.it in self.mapped_entities:
self.mapped_entities[entity.it].append(entity)
else:
self.mapped_entities[entity.it] = [entity]
def remove_entity(self, entity):
if entity in self.entities:
self.entities.remove(entity)
class Entity:
def __init__(self, game, x, y):
self.game = game
self.x = x
self.y = y
self.vel_x = 0
self.vel_y = 0
self.radius = 0
self.it = "entity"
self.color = "#ffffff"
self.game.add_entity(self)
self.last_updated = time.time()
self.outline = "#ffffff"
def delete(self):
self.game.remove_entity(self)
del self
def distance_to(self, entity):
return distance(self.x, self.y, entity.x, entity.y)
def act(self):
now = time.time()
self.x += self.vel_x * (now - self.last_updated) * 25
self.y += self.vel_y * (now - self.last_updated) * 25
self.last_updated = time.time()
def colliding(self):
for entity in self.game.entities:
delta_x = entity.x - self.x
delta_y = entity.y - self.y
if math.hypot(abs(delta_x), abs(delta_y)) < self.radius+entity.radius and entity != self:
return entity
return False
def colliding_with(self, entity):
delta_x = entity.x - self.x
delta_y = entity.y - self.y
if math.hypot(abs(delta_x), abs(delta_y)) < self.radius+entity.radius and entity != self:
return True
return False
def jsonify(self):
return {
"x": self.x,
"y": self.y,
"radius": self.radius,
"it": self.it,
"color": self.color,
"vel_x": self.vel_x,
"vel_y": self.vel_y,
"last_updated": self.last_updated,
"outline": self.outline
}
class Coin(Entity):
def __init__(self, game, x, y, value):
Entity.__init__(self, game, x, y)
self.value = value
self.radius = 10
self.it = "coin"
self.color = "#fdd700"
class Wall(Entity):
def __init__(self, game, x, y):
Entity.__init__(self, game, x, y)
self.radius = 50
self.it = "wall"
self.color = "#ffffff"
self.outline = "#aaaaaa"
class Tree(Entity):
def __init__(self, game, x, y):
Entity.__init__(self, game, x, y)
self.radius = 25
self.it = "tree"
self.color = "#533118"
self.outline = "#975445"
class Bullet(Entity):
def __init__(self, game, x, y, sender, vel_x, vel_y):
Entity.__init__(self, game, x, y)
self.radius = 5
self.it = "bullet"
self.color = "#000000"
self.outline = "#aaaaaa"
self.sender = sender
self.vel_x = vel_x
self.vel_y = vel_y
self.birth_date = time.time()
def act(self):
Entity.act(self)
entities = list(self.game.entities)
if time.time() - self.birth_date > 5:
self.delete()
for entity in entities:
if self.colliding_with(entity):
if type(entity) == Wall or type(entity) == Tree:
self.delete()
class Enemy(Entity):
def __init__(self, game, x, y, name):
Entity.__init__(self, game, x, y)
self.name = name
self.radius = 20
self.hp = 100
self.last_fired = time.time()
self.it = "enemy"
self.color = "#eeeeee"
self.direction = 0
self.score = 0
self.dead = False
self.outline = "#777777"
self.target = None # The player the Enemy is currently attacking
self.last_fired = time.time()
self.last_hitter = None
self.last_targeted = time.time()
def find_nearest_player(self): # Find the nearest player
nearest_player = None
for entity in self.game.entities:
if entity.it == "player" or entity.it == "enemy":
if nearest_player:
if self.distance_to(entity) > self.distance_to(nearest_player):
nearest_player = entity
else:
nearest_player = entity
return nearest_player
def fire_bullet(self, direction):
if self.dead:
return
vx = math.cos(math.radians(direction))
vy = math.sin(math.radians(direction))
if (time.time() - self.last_fired > 0.4):
Bullet(self.game, self.x, self.y, self, vx*25, vy*25)
self.last_fired = time.time()
def act(self):
entities = list(self.game.entities)
Entity.act(self)
if time.time() - self.last_targeted > 2:
self.target = self.find_nearest_player()
if self.hp <= 0:
Enemy(self.game, random.randint(-self.game.radius, self.game.radius), random.randint(-self.game.radius, self.game.radius), self.name)
if self.last_hitter:
self.last_hitter.hp = 100
self.last_hitter.score += 1
self.delete()
if self.target:
if self.target not in self.game.entities:
self.target = self.find_nearest_player()
else:
if time.time() - self.last_fired > 0.4:
self.fire_bullet(self.direction)
delta_x = self.target.x - self.x
delta_y = self.target.y - self.y
direction = math.atan2(delta_y, delta_x)
self.direction = math.degrees(direction)
self.vel_x = math.cos(direction)*3
self.vel_y = math.sin(direction)*3
else:
self.target = self.find_nearest_player()
for entity in entities:
if self.colliding_with(entity):
if type(entity) == Bullet:
if entity.sender != self:
self.hp -= 10
if entity.sender and entity.sender in self.game.entities:
self.last_hitter = entity.sender
entity.delete()
if type(entity) == Wall or type(entity) == Tree:
if entity.y > self.y and self.vel_y > 0:
self.y -= self.vel_y
self.vel_y = -self.vel_y
if entity.y < self.y and self.vel_y < 0:
self.y -= self.vel_y
self.vel_y = -self.vel_y# - self.vel_y
if entity.x > self.x and self.vel_x > 0:
self.x -= self.vel_x
self.vel_x = -self.vel_x# - self.vel_x
if entity.x < self.x and self.vel_x < 0:
self.x -= self.vel_x
self.vel_x = -self.vel_x
def jsonify(self):
return {
"x": self.x,
"y": self.y,
"radius": self.radius,
"it": self.it,
"color": self.color,
"vel_x": self.vel_x,
"vel_y": self.vel_y,
"last_updated": self.last_updated,
"outline": self.outline,
"hp": self.hp,
"direction": self.direction,
"dead": self.dead,
"name": self.name,
"score": self.score
}
class Player(Entity):
def __init__(self, game, x, y, name):
Entity.__init__(self, game, x, y)
self.name = name
self.radius = 20
self.score = 0
self.view = list()
self.it = "player"
self.color = "#eeeeee"
self.outline = "#777777"
self.ping = time.time()
self.hp = 100
self.velocity_queue = [0, 0]
self.last_fired = time.time()
self.dead = False
self.dying_timer = time.time()
self.direction = 0
self.mapped_view = dict()
self.last_hitter = None
def set_velocity(self, vel_x, vel_y):
if self.dead:
return
self.velocity_queue[0] = vel_x
self.velocity_queue[1] = vel_y
if self.x > self.game.radius and self.vel_x > 0:
self.velocity_queue[0] = -self.velocity_queue[0]
if self.y > self.game.radius and self.vel_y > 0:
self.velocity_queue[1] = -self.velocity_queue[1]
if self.x < -self.game.radius and self.vel_x < 0:
self.velocity_queue[0] = -self.velocity_queue[0]
if self.y < -self.game.radius and self.vel_y < 0:
self.velocity_queue[1] = -self.velocity_queue[1]
def fire_bullet(self, direction):
if self.dead:
return
vx = math.cos(math.radians(direction))
vy = math.sin(math.radians(direction))
if (time.time() - self.last_fired > 0.2):
Bullet(self.game, self.x, self.y, self, vx*25, vy*25)
self.last_fired = time.time()
def handle_collisions(self):
self.new_view = list()
new_mapped_view = {
"wall": list(),
"player": list(),
"entity": list(),
"bullet": list(),
"tree": list(),
"coin": list(),
"enemy": list()
}
velocity_queue = self.velocity_queue
for entity in list(self.game.entities):
if abs(self.x-entity.x) < 300+entity.radius and abs(self.y-entity.y) < 300+entity.radius:
self.new_view.append(entity.jsonify())
new_mapped_view[entity.it].append(entity.jsonify())
elif type(entity) == Tree and abs(self.x-entity.x) < 300+entity.radius*5 and abs(self.y-entity.y) < 300+entity.radius*5:
self.new_view.append(entity.jsonify())
new_mapped_view[entity.it].append(entity.jsonify())
if self.colliding_with(entity):
if type(entity) == Coin:
self.score += entity.value
entity.delete()
if type(entity) == Wall or type(entity) == Tree:
if entity.y > self.y and velocity_queue[1] > 0:
velocity_queue[1] = -velocity_queue[1]
if entity.y < self.y and velocity_queue[1] < 0:
velocity_queue[1] = -velocity_queue[1]# - self.vel_y
if entity.x > self.x and velocity_queue[0] > 0:
velocity_queue[0] = -velocity_queue[0]# - self.vel_x
if entity.x < self.x and velocity_queue[0] < 0:
velocity_queue[0] = -velocity_queue[0]# - self.vel_x
if type(entity) == Bullet:
if entity.sender != self:
self.hp -= 10;
entity.delete();
self.view = self.new_view
self.mapped_view = new_mapped_view
if not self.dead:
self.vel_x = velocity_queue[0]
self.vel_y = velocity_queue[1]
def act(self):
## Hardcoding game wall mechanic
if self.x > self.game.radius:
self.x = self.game.radius
if self.y > self.game.radius:
self.y = self.game.radius
if self.x < -self.game.radius:
self.x = -self.game.radius
if self.y < -self.game.radius:
self.y = -self.game.radius
if time.time() - self.ping >= 3:
print("Eteria: Game: Player "+self.name+" has disconnected")
self.delete()
if self.hp <= 0:
self.dead = True
self.dying_timer = time.time()
if self.last_hitter:
self.last_hitter.hp = 100
self.last_hitter.score += self.score + 1
self.vel_x = 0
self.vel_y = 0
Entity.act(self)
self.handle_collisions()
def jsonify(self):
return {
"x": self.x,
"y": self.y,
"radius": self.radius,
"it": self.it,
"name": self.name,
"color": self.color,
"score": self.score,
"hp": self.hp,
"vel_x": self.vel_x,
"vel_y": self.vel_y,
"dead": self.dead,
"direction": self.direction,
"last_updated": self.last_updated,
"outline": self.outline
}