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