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