1 | importˆàvector |
---|
2 | importˆàmath |
---|
3 | importˆàrandom |
---|
4 | |
---|
5 | base_angle =ˆàmath.pi/32ˆà ˆà# deltha phi = math.pi/32 |
---|
6 | turret_angle =ˆàmath.pi/32 |
---|
7 | speed_delta =ˆà2 |
---|
8 | delta_t =ˆà1 |
---|
9 | max_velocity =ˆà4 |
---|
10 | acceleration =ˆà1 |
---|
11 | max_base_angle =ˆà1 |
---|
12 | max_turret_angle =ˆà1 |
---|
13 | initial_strength =ˆà1 |
---|
14 | bullet_velocity =ˆà10 |
---|
15 | width =ˆà500 |
---|
16 | height =ˆà500 |
---|
17 | bullet_speed =ˆà10 |
---|
18 | |
---|
19 | classˆàBody(object): |
---|
20 | ˆà ˆà ˆà ˆà defˆà__init__(self,ˆàposition =ˆàvector.null,ˆàvelocity =ˆàvector.null): |
---|
21 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.position =ˆàposition |
---|
22 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.velocity =ˆàvelocity |
---|
23 | |
---|
24 | classˆàTank(Body): |
---|
25 | ˆà ˆà ˆà ˆà radius =ˆà5 |
---|
26 | ˆà ˆà ˆà ˆà defˆà__init__(self,ˆàposition,ˆàuser,ˆàgame): |
---|
27 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà Body.__init__(self,ˆàposition) |
---|
28 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.game =ˆàgame |
---|
29 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.turret =ˆàvector.Vector(0,-1) |
---|
30 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.strength =ˆà0 |
---|
31 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.base_orientation =ˆàvector.Vector(0,-1)ˆà ˆà ˆà# Vector |
---|
32 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.user =ˆàuser |
---|
33 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà user.tank =ˆàselfˆà |
---|
34 | |
---|
35 | |
---|
36 | ˆà ˆà ˆà ˆà defˆàrotate_base(self,ˆàangle): |
---|
37 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàabs(angle)ˆà<ˆàmax_base_angle:ˆà |
---|
38 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.velocity.phi +=ˆàangle |
---|
39 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.base_orientation.phi +=angle |
---|
40 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.turret.phi +=ˆàangle |
---|
41 | |
---|
42 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà else: |
---|
43 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.velocity.phi +=ˆàmax_base_angle |
---|
44 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.base_orientation.phi +=ˆàmax_base_angle |
---|
45 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.turret.phi +=ˆàmax_turret_angle |
---|
46 | |
---|
47 | ˆà ˆà ˆà ˆà defˆàrotate_turret(self,ˆàangle): |
---|
48 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàabs(angle)ˆà<ˆàmax_base_angle:ˆà |
---|
49 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.turret.phi +=ˆàangle |
---|
50 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà else: |
---|
51 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.turret.phi +=ˆàmax_turret_angle |
---|
52 | |
---|
53 | |
---|
54 | ˆà ˆà ˆà ˆà defˆàaccelerate(self,ˆàspeed_delta): |
---|
55 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.velocity +=ˆàself.base_orientation*speed_delta*delta_t |
---|
56 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàabs(self.velocity)ˆà>ˆàmax_velocity: |
---|
57 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.velocity.rho =ˆàmax_velocity |
---|
58 | |
---|
59 | ˆà ˆà ˆà ˆà defˆàfire(self): |
---|
60 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà bullet_position =ˆàself.position +ˆàself.turret *ˆà(self.radius +ˆà0.1) |
---|
61 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà bullet_velocity =ˆàself.turret.normalize()ˆà*ˆàbullet_speed |
---|
62 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà bullet =ˆàBullet(bullet_position,ˆàbullet_velocity,ˆàself) |
---|
63 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.game.bodies.append(bullet) |
---|
64 | |
---|
65 | ˆà ˆà ˆà ˆà defˆàon_tick(self,other_tanks,ˆàbullets): |
---|
66 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pass |
---|
67 | |
---|
68 | ˆà ˆà ˆà ˆà defˆàon_spawn(self): |
---|
69 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pass |
---|
70 | |
---|
71 | ˆà ˆà ˆà ˆà defˆàon_death(self)ˆà: |
---|
72 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pass |
---|
73 | ˆà ˆà ˆà ˆà |
---|
74 | ˆà ˆà ˆà ˆà defˆàon_hit(self,ˆàbullet): |
---|
75 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.strength -=ˆà1 |
---|
76 | |
---|
77 | ˆà ˆà ˆà ˆà defˆàon_collision(self,ˆàother): |
---|
78 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pass |
---|
79 | ˆà ˆà ˆà ˆà #ˆà ˆà ˆà ˆàself.velocity , other.velocity =ˆà self.velocity + other.velocity , other.velocity + self.velocity |
---|
80 | |
---|
81 | ˆà ˆà ˆà ˆà defˆàon_wall(self): |
---|
82 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.velocity =ˆàvector.Vector(0,0) |
---|
83 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.next_position =ˆàself.position |
---|
84 | |
---|
85 | classˆàBullet(Body): |
---|
86 | ˆà ˆà ˆà ˆà radius =ˆà0.1 |
---|
87 | ˆà ˆà ˆà ˆà defˆà__init__(self,ˆàposition,ˆàvelocity,ˆàtank): |
---|
88 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.tank =ˆàtank |
---|
89 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà Body.__init__(self,ˆàposition,ˆàvelocity =ˆàself.tank.turret*bullet_velocity) |
---|
90 | |
---|
91 | ˆà ˆà ˆà ˆà defˆàon_wall(self): |
---|
92 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.tank.game.bodies.remove(self) |
---|