tanchiki
changeset 28:4ef6336d1707
Merge changes.
author | Peter Zotov <whitequark@whitequark.org> |
---|---|
date | Mon, 20 Dec 2010 11:32:48 +0300 |
parents | b9856f9de9e2 43f5b82f3491 |
children | 779e091fb740 |
files | tanchiki/body.py tanchiki/game.py tanchiki/vector.py |
diffstat | 3 files changed, 55 insertions(+), 22 deletions(-) [+] |
line diff
1.1 --- a/tanchiki/body.py Mon Dec 20 11:03:40 2010 +0300 1.2 +++ b/tanchiki/body.py Mon Dec 20 11:32:48 2010 +0300 1.3 @@ -4,7 +4,8 @@ 1.4 import math 1.5 1.6 class Body(object): 1.7 - def __init__(self, position, velocity = vector.Vector.null): 1.8 + def __init__(self, game, position, velocity = vector.Vector.null): 1.9 + self.game = game 1.10 self.position = position 1.11 self.velocity = velocity 1.12 1.13 @@ -12,28 +13,39 @@ 1.14 radius = 1 1.15 model = "tank" 1.16 1.17 - def __init__(self, position, controller): 1.18 - Body.__init__(self, position) 1.19 + def __init__(self, game, position, controller): 1.20 + Body.__init__(self, game, position) 1.21 self.controller = controller 1.22 self.turret = vector.Vector() 1.23 self.strength = 0 1.24 1.25 def rotate_base(tank, angle): 1.26 - self.velocity.phi += angle 1.27 + if abs(angle) < self.game.max_base_delta : 1.28 + self.velocity.phi += angle 1.29 + else : 1.30 + self.velocity.phi += self.game.max_base_delta 1.31 1.32 def rotate_turret(self, angle): 1.33 - self.turret.phi += angle 1.34 + if abs(angle) < self.game.max_base_delta : 1.35 + self.turret.phi += angle 1.36 + else : 1.37 + self.turret.phi += self.game.max_turret_delta 1.38 1.39 def accelerate(self, speed_delta): 1.40 - self.velocity.rho += speed_delta * delta_t 1.41 + self.velocity += self.velocity.normalize() * speed_delta 1.42 if self.velocity.rho > max_velocity : 1.43 self.velocity.rho = max_velocity 1.44 1.45 def fire(self): 1.46 - pass 1.47 + bullet_position = self.position + self.turret * (self.radius + 0.1) 1.48 + bullet_velocity = self.turret.normalize() * self.game.bullet_speed 1.49 + bullet = Bullet(bullet_position, bullet_velocity, self) 1.50 + self.game.bodies.append(bullet) 1.51 1.52 class Bullet(Body): 1.53 radius = 0.1 1.54 model = "bullet" 1.55 1.56 - pass 1.57 + def __init__(self, position, velocity, tank): 1.58 + Body.__init__(self, position, velocity) 1.59 + self.origin = origin
2.1 --- a/tanchiki/game.py Mon Dec 20 11:03:40 2010 +0300 2.2 +++ b/tanchiki/game.py Mon Dec 20 11:32:48 2010 +0300 2.3 @@ -23,16 +23,31 @@ 2.4 i.position = i.next_position 2.5 2.6 def __collides(self, body1, body2): 2.7 - pass 2.8 + return (abs(body1.position - body2.position) <= (body1.radius + body2.radius)) 2.9 + and (body1 != body2) 2.10 2.11 def __handle_collision(self, body1, body2): 2.12 - pass 2.13 + if isinstance(body1, body.Tank) == True : 2.14 + if isinstance(body2, body.Tank) == True : 2.15 + body1.on_collision(body2) 2.16 + body2.on_collision(body1) 2.17 + else : 2.18 + body1.on_hit() 2.19 + body1.on_death() 2.20 + else : 2.21 + if isinstance(body2, body.Tank) == True : 2.22 + body2.on_hit() 2.23 + body2.on_death() 2.24 2.25 def __check_collisions(game): 2.26 - pass 2.27 + for i in range(len(self.bodies)) : 2.28 + for j in range(i, len(self.bodies)) : 2.29 + a, b = self.bodies[i], self.bodies[j] 2.30 + if self.collides(a, b) == True : 2.31 + self.handle_collision(a, b) 2.32 2.33 - def __check_walls(game): 2.34 - for i in game.bodies : 2.35 + def __check_walls(self): 2.36 + for i in self.bodies : 2.37 if ((i.next_position.x - i.radius) <= -game.width/2) or \ 2.38 ((i.next_position.y - i.radius) <= -game.width/2) or \ 2.39 ((i.next_position.x + i.radius) >= game.width/2) or \
3.1 --- a/tanchiki/vector.py Mon Dec 20 11:03:40 2010 +0300 3.2 +++ b/tanchiki/vector.py Mon Dec 20 11:32:48 2010 +0300 3.3 @@ -1,10 +1,11 @@ 3.4 import math 3.5 3.6 -class Vector(object): 3.7 - def __init__(self, x=0 , y=0): 3.8 +class Vector(object): 3.9 + 3.10 + def __init__(self, x=0 , y=0): 3.11 self.x = x 3.12 self.y = y 3.13 - 3.14 + 3.15 def __add__(self, other): 3.16 result = Vector(0,0) 3.17 result.x = self.x + other.x 3.18 @@ -18,7 +19,7 @@ 3.19 return result 3.20 3.21 def dot_product(self, other): 3.22 - return self.x*other.x + self.y*other.y 3.23 + return self.x*other.x + self.y*other.y 3.24 3.25 def __abs__(self): 3.26 return (self.x**2 + self.y**2)**0.5 3.27 @@ -32,11 +33,18 @@ 3.28 else : 3.29 return 0 3.30 3.31 + def normalize(self): 3.32 + result = Vector() 3.33 + result.x = self.x 3.34 + result.y = self.y 3.35 + result.rho = 1 3.36 + return result 3.37 + 3.38 def get_rho(self): 3.39 return abs(self) 3.40 3.41 def set_rho(self, new_rho): 3.42 - if self.is_null() == 1 : 3.43 + if self.is_null() == 1: 3.44 self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi) 3.45 else : 3.46 self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self)) 3.47 @@ -47,12 +55,10 @@ 3.48 phi = math.pi/2 - math.atan2(self.x, self.y) 3.49 if self.is_null == 1: 3.50 phi = 0 3.51 - return phi 3.52 + return phi 3.53 3.54 def set_phi(self, new_phi): 3.55 rho = abs(self) 3.56 - self.x, self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi) 3.57 + self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi) 3.58 3.59 phi = property(get_phi, set_phi) 3.60 - 3.61 -Vector.null = Vector(0,0) 3.62 \ No newline at end of file