rev |
line source |
olya_zol@35
|
1 import vector |
olya_zol@35
|
2 import body |
olya_zol@35
|
3 import time |
olya_zol@52
|
4 import random |
olya_zol@35
|
5 |
olya_zol@35
|
6 other_tanks = [] |
olya_zol@35
|
7 bullets = [] |
olya_zol@52
|
8 width = 500 |
olya_zol@52
|
9 height = 500 |
olya_zol@35
|
10 |
olya_zol@35
|
11 class Game(object): |
olya_zol@35
|
12 def __init__(self, bodies, users, width, height): |
olya_zol@35
|
13 self.bodies = bodies |
olya_zol@35
|
14 self.users = users |
olya_zol@35
|
15 self.width = width |
olya_zol@35
|
16 self.height = height |
olya_zol@35
|
17 |
olya_zol@52
|
18 def step(self): |
olya_zol@35
|
19 self.next_positions() |
olya_zol@35
|
20 self.check_collisions() |
olya_zol@35
|
21 self.check_walls() |
olya_zol@35
|
22 self.update_positions() |
olya_zol@35
|
23 self.invoke_ticks() |
olya_zol@35
|
24 self.respawn() |
olya_zol@52
|
25 self.update_velocities() |
olya_zol@54
|
26 # for i in self.bodies : |
olya_zol@54
|
27 # print i # test |
olya_zol@54
|
28 # print 'next position' , i.next_position # test |
olya_zol@54
|
29 # print 'update_position' , i.position # test |
olya_zol@54
|
30 # print 'velocity' , i.velocity # test |
olya_zol@54
|
31 # print 'velocity.rho', i.velocity.rho # test |
olya_zol@54
|
32 # if isinstance(i,body.Tank) == True: # test |
olya_zol@54
|
33 # print 'base_orientation' , i.base_orientation # test |
olya_zol@54
|
34 # print '\n' |
olya_zol@35
|
35 |
olya_zol@52
|
36 def update_velocities(self): |
olya_zol@52
|
37 for i in self.bodies: |
olya_zol@52
|
38 if isinstance(i, body.Tank): |
olya_zol@52
|
39 if abs(i.velocity) > 1e-10 : |
olya_zol@52
|
40 i.velocity = i.velocity - i.velocity.normalize()*body.acceleration |
olya_zol@52
|
41 else : |
olya_zol@52
|
42 i.velocity.rho = 0 |
olya_zol@35
|
43 |
olya_zol@35
|
44 def next_positions(self): |
olya_zol@35
|
45 delta_t = 1 |
olya_zol@35
|
46 for i in self.bodies: |
olya_zol@52
|
47 i.next_position = i.position + i.velocity*body.delta_t |
olya_zol@35
|
48 |
olya_zol@35
|
49 def check_collisions(self): |
olya_zol@35
|
50 for i in self.bodies: |
olya_zol@35
|
51 for j in self.bodies: |
olya_zol@35
|
52 if self.collides(i,j) == True : |
olya_zol@35
|
53 self.handle_collision(i,j) |
olya_zol@35
|
54 |
olya_zol@35
|
55 def collides(self,body1,body2): |
olya_zol@53
|
56 if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)): |
olya_zol@53
|
57 if (body1 != body2): |
olya_zol@53
|
58 return True |
olya_zol@53
|
59 else : |
olya_zol@53
|
60 return False |
olya_zol@53
|
61 else : |
olya_zol@53
|
62 return False |
olya_zol@35
|
63 |
olya_zol@35
|
64 def handle_collision(self,body1,body2): |
olya_zol@35
|
65 if isinstance(body1, body.Tank) == True : |
olya_zol@35
|
66 if isinstance(body2, body.Tank) == True : |
olya_zol@35
|
67 body1.on_collision(body2) |
olya_zol@35
|
68 else : |
olya_zol@54
|
69 body1.on_hit(body2) |
olya_zol@35
|
70 body1.on_death() |
olya_zol@54
|
71 self.bodies.remove(body2) |
olya_zol@35
|
72 else : |
olya_zol@35
|
73 if isinstance(body2, body.Tank) == True : |
olya_zol@54
|
74 body2.on_hit(body2) |
olya_zol@35
|
75 body2.on_death() |
olya_zol@54
|
76 self.bodies.remove(body1) |
olya_zol@35
|
77 |
olya_zol@35
|
78 def check_walls(self): |
olya_zol@35
|
79 for i in self.bodies : |
olya_zol@35
|
80 if ((i.next_position.x - i.radius) <= 0) or ((i.next_position.y - i.radius) <= 0) or ((i.next_position.x + i.radius) >= self.width) or ((i.next_position.y + i.radius) >= self.height) : |
olya_zol@35
|
81 i.on_wall() |
olya_zol@35
|
82 |
olya_zol@35
|
83 def update_positions(self): |
olya_zol@35
|
84 for i in self.bodies: |
olya_zol@35
|
85 i.position = i.next_position |
olya_zol@35
|
86 |
olya_zol@35
|
87 def invoke_ticks(self): |
olya_zol@35
|
88 for i in self.bodies : |
olya_zol@35
|
89 if isinstance(i, body.Tank) : |
olya_zol@35
|
90 i.on_tick([], []) |
olya_zol@35
|
91 |
olya_zol@35
|
92 |
olya_zol@35
|
93 def respawn(self): |
olya_zol@35
|
94 for i in self.users : |
olya_zol@35
|
95 if i.tank.strength == 0 : |
olya_zol@35
|
96 i.tank.on_spawn() |
olya_zol@35
|
97 i.tank.strength = 1 |
olya_zol@35
|
98 i.tank.velocity = vector.null |
olya_zol@52
|
99 i.tank.position.x = random.randint(i.tank.radius , width - i.tank.radius) |
olya_zol@52
|
100 i.tank.position.y = random.randint(i.tank.radius , height - i.tank.radius) |
olya_zol@35
|
101 i.tank.velocity = vector.null |
olya_zol@35
|
102 |
olya_zol@35
|
103 |