tanchiki
changeset 3:af02cd410e37
modules changed
author | Olga Zolotareva <olya_zol@inbox.ru> |
---|---|
date | Sat, 18 Dec 2010 15:20:51 +0300 |
parents | e6e7b30ecde0 |
children | 7313e5a09453 b95121219b90 |
files | body.py game.py user.py vector.py |
diffstat | 3 files changed, 36 insertions(+), 20 deletions(-) [+] |
line diff
1.1 --- a/body.py Sat Dec 18 13:23:48 2010 +0300 1.2 +++ b/body.py Sat Dec 18 15:20:51 2010 +0300 1.3 @@ -1,7 +1,12 @@ 1.4 import vector 1.5 +import math 1.6 +delta_phi = math.pi # deltha phi = math.pi 1.7 +speed_delta = 1 1.8 +delta_t = 1 1.9 +max_velocity = 2 1.10 1.11 class Body(object): 1.12 - def __init__(self, position, velocity, next_position, radius=1): 1.13 + def __init__(self, position, velocity, next_position, radius): 1.14 self.position = position 1.15 self.velocity = velocity 1.16 self.next_position = next_position 1.17 @@ -12,23 +17,32 @@ 1.18 def __init__(self, strength, turret, base_orientation, game, user): 1.19 self.strength = strength 1.20 self.turret = turret 1.21 - self.base_orientation = base_orientation 1.22 + self.base_orientation = base_orientation # 1 or -1 1.23 self.game = game 1.24 self.user = user 1.25 - def rotate_base(tank, angle): 1.26 - pass 1.27 + 1.28 + def rotate_base(tank, angle): 1.29 + tank.base_velocity.phi += angle 1.30 + tank.velocity.phi *= tank.base_orientation 1.31 1.32 def rotate_turret(tank, angle): 1.33 pass 1.34 1.35 def accelerate(tank, speed_delta): 1.36 - pass 1.37 + tank.velocity.rho += speed_delta * delta_t 1.38 + if tank.velocity.rho > max_velocity: 1.39 + tank.velocity.rho = max_velocity 1.40 1.41 def fire(tank): 1.42 pass 1.43 1.44 def on_tick(tank,other_tanks, bullets): 1.45 - pass 1.46 + if tank.user.base_left == True: 1.47 + tank.rotate_base(delta_phi) 1.48 + if tank.user.base_right == True: 1.49 + tank.rotate_base(-1*delta_phi) 1.50 + if tank.user.accelerate == True: 1.51 + tank.accelerate(speed_delta) 1.52 1.53 def on_spawn(tank): 1.54 pass
2.1 --- a/game.py Sat Dec 18 13:23:48 2010 +0300 2.2 +++ b/game.py Sat Dec 18 15:20:51 2010 +0300 2.3 @@ -1,3 +1,6 @@ 2.4 +other_tanks = [] 2.5 +bullets = [] 2.6 + 2.7 class Game(object): 2.8 def __init__(self, bodies, users, width, height): 2.9 self.bodies = bodies 2.10 @@ -16,7 +19,7 @@ 2.11 def next_positions(game): 2.12 delta_t = 1 2.13 for i in game.bodies: 2.14 - i.next_position = i.position + i.velocity.mul_v(delta_t) 2.15 + i.next_position = i.position + i.velocity*(delta_t) 2.16 2.17 2.18 def check_collisions(game): 2.19 @@ -30,7 +33,7 @@ 2.20 2.21 def check_walls(game): 2.22 for i in game.bodies : 2.23 - if (i.next_position.x <= 0) or (i.next_position.y <= 0) or (i.next_position.x <= width) or (i.next_position.y >= game.height) : 2.24 + if ((i.next_position.x - i.radius) <= 0) or ((i.next_position.y - i.radius) <= 0) or ((i.next_position.x + i.radius) >= game.width) or ((i.next_position.y + i.radius) >= game.height) : 2.25 i.on_wall() 2.26 2.27 2.28 @@ -39,12 +42,11 @@ 2.29 i.position = i.next_position 2.30 2.31 def invoke_ticks(game): 2.32 - pass 2.33 + for i in game.users: 2.34 + i.tank.on_tick(other_tanks,bullets) 2.35 2.36 def respawn(game): 2.37 for i in game.users : 2.38 if i.tank.strength == 0 : 2.39 - i.tank.respawn() 2.40 - 2.41 - 2.42 - 2.43 \ No newline at end of file 2.44 + i.tank.on_spawn() 2.45 + i.tank.strength = 1 2.46 \ No newline at end of file
3.1 --- a/vector.py Sat Dec 18 13:23:48 2010 +0300 3.2 +++ b/vector.py Sat Dec 18 15:20:51 2010 +0300 3.3 @@ -12,7 +12,7 @@ 3.4 result.y = self.y + other.y 3.5 return result 3.6 3.7 - def mul_v(self, alpha): 3.8 + def __mul__(self, alpha): 3.9 result = Vector() 3.10 result.x = self.x * alpha 3.11 result.y = self.y * alpha 3.12 @@ -21,29 +21,29 @@ 3.13 def dot_product(self, other): 3.14 return self.x*other.x + self.y*other.y 3.15 3.16 - def len_v(self): 3.17 + def __abs__(self): 3.18 return (self.x**2 + self.y**2)**0.5 3.19 3.20 def __str__(self): 3.21 return "(%s, %s)" % (self.x, self.y) 3.22 3.23 def is_null(self): 3.24 - if self.len_v() == 0 : 3.25 + if abs(self) == 0 : 3.26 return 1 3.27 else : 3.28 return 0 3.29 3.30 3.31 def get_rho(self): 3.32 - return self.len_v() 3.33 + return abs(self) 3.34 3.35 def set_rho(self, new_rho): 3.36 - self.x , self.y = self.x*(new_rho/self.len_v()) , self.y*(new_rho/self.len_v()) 3.37 + self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self)) 3.38 3.39 rho = property(get_rho, set_rho) 3.40 3.41 def get_phi(self): 3.42 - cos = self.dot_product(Vector(1,0))/(self.len_v()*1) 3.43 + cos = self.dot_product(Vector(1,0))/(abs(self)*1) 3.44 if self.y < 0 : 3.45 phi = -acos(cos) 3.46 else : 3.47 @@ -51,7 +51,7 @@ 3.48 return phi 3.49 3.50 def set_phi(self, new_phi): 3.51 - self.x , self.y = self.len_v()*cos(new_phi) , self.len_v()*sin(new_phi) 3.52 + self.x , self.y = abs(self)*cos(new_phi) , abs(self)*sin(new_phi) 3.53 3.54 phi = property(get_phi, set_phi) 3.55