1 | other_tanks =ˆà[] |
---|
2 | bullets =ˆà[] |
---|
3 | |
---|
4 | classˆàGame(object): |
---|
5 | ˆà ˆà ˆà ˆà defˆà__init__(self,ˆàwidth,ˆàheight,ˆàbodies): |
---|
6 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.bodies =ˆàbodies |
---|
7 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.width =ˆàwidth |
---|
8 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.height =ˆàheight |
---|
9 | |
---|
10 | ˆà ˆà ˆà ˆà defˆàstep(self,ˆàdelta_t): |
---|
11 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.__calculate_positions(delta_t) |
---|
12 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.__check_walls() |
---|
13 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.__check_collisions() |
---|
14 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.__update_positions() |
---|
15 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.__invoke_ticks() |
---|
16 | |
---|
17 | ˆà ˆà ˆà ˆà defˆà__calculate_positions(game,ˆàdelta_t): |
---|
18 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà forˆài inˆàgame.bodies : |
---|
19 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà i.next_position =ˆài.position +ˆài.velocity*(delta_t) |
---|
20 | |
---|
21 | ˆà ˆà ˆà ˆà defˆà__update_positions(self): |
---|
22 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà forˆài inˆàself.bodies : |
---|
23 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà i.position =ˆài.next_position |
---|
24 | |
---|
25 | ˆà ˆà ˆà ˆà defˆà__collides(self,ˆàbody1,ˆàbody2): |
---|
26 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà returnˆà(abs(body1.position -ˆàbody2.position)ˆà<=ˆà(body1.radius +ˆàbody2.radius)) |
---|
27 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆàandˆà(body1 !=ˆàbody2) |
---|
28 | |
---|
29 | ˆà ˆà ˆà ˆà defˆà__handle_collision(self,ˆàbody1,ˆàbody2): |
---|
30 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàisinstance(body1,ˆàbody.Tank)ˆà==ˆàTrueˆà:ˆà |
---|
31 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàisinstance(body2,ˆàbody.Tank)ˆà==ˆàTrueˆà: |
---|
32 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà body1.on_collision(body2) |
---|
33 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà body2.on_collision(body1) |
---|
34 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà elseˆà: |
---|
35 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà body1.on_hit() |
---|
36 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà body1.on_death() |
---|
37 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà elseˆà: |
---|
38 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàisinstance(body2,ˆàbody.Tank)ˆà==ˆàTrueˆà: |
---|
39 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà body2.on_hit() |
---|
40 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà body2.on_death() |
---|
41 | |
---|
42 | ˆà ˆà ˆà ˆà defˆà__check_collisions(game): |
---|
43 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà forˆài inˆàrange(len(self.bodies))ˆà: |
---|
44 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà forˆàj inˆàrange(i,ˆàlen(self.bodies))ˆà: |
---|
45 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà a,ˆàb =ˆàself.bodies[i],ˆàself.bodies[j] |
---|
46 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàself.collides(a,ˆàb)ˆà==ˆàTrueˆà: |
---|
47 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.handle_collision(a,ˆàb) |
---|
48 | |
---|
49 | ˆà ˆà ˆà ˆà defˆà__check_walls(self): |
---|
50 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà forˆài inˆàself.bodies : |
---|
51 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆà((i.next_position.x -ˆài.radius)ˆà<=ˆà-game.width/2)ˆàorˆà\ |
---|
52 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà((i.next_position.y -ˆài.radius)ˆà<=ˆà-game.width/2)ˆàorˆà\ |
---|
53 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà((i.next_position.x +ˆài.radius)ˆà>=ˆàgame.width/2)ˆàorˆà\ |
---|
54 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà((i.next_position.y +ˆài.radius)ˆà>=ˆàgame.height/2)ˆà: |
---|
55 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà i.on_wall() |
---|
56 | |
---|
57 | ˆà ˆà ˆà ˆà defˆà__invoke_ticks(self): |
---|
58 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà forˆài inˆàself.bodies : |
---|
59 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆài.controller : |
---|
60 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà i.controller.on_tick(other_tanks,ˆàbullets) |
---|