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@52
|
26 for i in self.bodies : |
olya_zol@52
|
27 print i |
olya_zol@52
|
28 # print 'next position' , i.next_position |
olya_zol@52
|
29 print 'update_position' , i.position |
olya_zol@52
|
30 print 'velocity' , i.velocity |
olya_zol@52
|
31 print 'velocity.rho', i.velocity.rho # test |
olya_zol@52
|
32 if isinstance(i,body.Tank) == True: |
olya_zol@52
|
33 print 'base_orientation' , i.base_orientation |
olya_zol@53
|
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 print 'collision' |
olya_zol@53
|
59 return True |
olya_zol@53
|
60 else : |
olya_zol@53
|
61 return False |
olya_zol@53
|
62 else : |
olya_zol@53
|
63 return False |
olya_zol@35
|
64 |
olya_zol@35
|
65 def handle_collision(self,body1,body2): |
olya_zol@35
|
66 if isinstance(body1, body.Tank) == True : |
olya_zol@35
|
67 if isinstance(body2, body.Tank) == True : |
olya_zol@35
|
68 body1.on_collision(body2) |
olya_zol@35
|
69 else : |
olya_zol@35
|
70 body1.on_hit() |
olya_zol@35
|
71 body1.on_death() |
olya_zol@35
|
72 else : |
olya_zol@35
|
73 if isinstance(body2, body.Tank) == True : |
olya_zol@35
|
74 body2.on_hit() |
olya_zol@35
|
75 body2.on_death() |
olya_zol@35
|
76 |
olya_zol@35
|
77 def check_walls(self): |
olya_zol@35
|
78 for i in self.bodies : |
olya_zol@35
|
79 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
|
80 i.on_wall() |
olya_zol@35
|
81 |
olya_zol@35
|
82 def update_positions(self): |
olya_zol@35
|
83 for i in self.bodies: |
olya_zol@35
|
84 i.position = i.next_position |
olya_zol@35
|
85 |
olya_zol@35
|
86 def invoke_ticks(self): |
olya_zol@35
|
87 for i in self.bodies : |
olya_zol@35
|
88 if isinstance(i, body.Tank) : |
olya_zol@35
|
89 i.on_tick([], []) |
olya_zol@35
|
90 |
olya_zol@35
|
91 |
olya_zol@35
|
92 def respawn(self): |
olya_zol@35
|
93 for i in self.users : |
olya_zol@35
|
94 if i.tank.strength == 0 : |
olya_zol@35
|
95 i.tank.on_spawn() |
olya_zol@52
|
96 print 'respawn' |
olya_zol@52
|
97 print i.tank.strength , i.tank.position |
olya_zol@35
|
98 i.tank.strength = 1 |
olya_zol@35
|
99 i.tank.velocity = vector.null |
olya_zol@52
|
100 i.tank.position.x = random.randint(i.tank.radius , width - i.tank.radius) |
olya_zol@52
|
101 i.tank.position.y = random.randint(i.tank.radius , height - i.tank.radius) |
olya_zol@35
|
102 i.tank.velocity = vector.null |
olya_zol@52
|
103 print i.tank.strength , i.tank.position |
olya_zol@35
|
104 |
olya_zol@35
|
105 |