tanchiki
changeset 33:a92686be9c95 new
Merged new with default
author | Daniil Alexeyevsky <me.dendik@gmail.com> |
---|---|
date | Mon, 20 Dec 2010 16:11:34 +0300 |
parents | 803f8353f38f 48f019a6c1c6 |
children | b7a85caedc7f |
files | body.py game.py user.py vector.py |
diffstat | 5 files changed, 1 insertions(+), 186 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Mon Dec 20 16:11:34 2010 +0300 1.3 @@ -0,0 +1,1 @@ 1.4 +.*\.pyc
2.1 --- a/body.py Mon Dec 20 16:05:49 2010 +0300 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,64 +0,0 @@ 2.4 -import vector 2.5 -import math 2.6 -delta_phi = math.pi # deltha phi = math.pi 2.7 -speed_delta = 1 2.8 -delta_t = 1 2.9 -max_velocity = 2 2.10 -initial_strength = 1 2.11 - 2.12 -class Body(object): 2.13 - def __init__(self, position, velocity = vector.null): 2.14 - self.position = position 2.15 - self.velocity = velocity 2.16 - self.radius = radius 2.17 - 2.18 -class Tank(Body): 2.19 - radius = 1 2.20 - def __init__(self, position, user): 2.21 - Body.__init__(self, position) 2.22 - self.strength = 0 2.23 - self.turret = vector.i 2.24 - self.base_orientation = 1 # 1 or -1 2.25 - self.user = user 2.26 - user.tank = self # добавляет себя в User 2.27 - 2.28 - def rotate_base(tank, angle): 2.29 - self.velocity.phi += angle 2.30 - 2.31 - def rotate_turret(self, angle): 2.32 - self.turret.phi += angle 2.33 - 2.34 - def accelerate(self, speed_delta): 2.35 - self.velocity.rho += speed_delta * delta_t 2.36 - if self.velocity.rho > max_velocity : 2.37 - self.velocity.rho = max_velocity 2.38 - 2.39 - def fire(self): 2.40 - pass 2.41 - 2.42 - def on_tick(self,other_tanks, bullets): 2.43 - if self.user.base_left == True : 2.44 - self.rotate_base(delta_phi) 2.45 - if self.user.base_right == True : 2.46 - self.rotate_base(-1*delta_phi) 2.47 - if self.user.accelerate == True : 2.48 - self.accelerate(speed_delta) 2.49 - 2.50 - def on_spawn(self): 2.51 - pass 2.52 - 2.53 - def on_death(self): 2.54 - pass 2.55 - 2.56 - def on_hit(self,bullet): 2.57 - pass 2.58 - 2.59 - def on_collision(self): 2.60 - pass 2.61 - 2.62 - def on_wall(self): 2.63 - pass 2.64 - 2.65 -class Bullet(Body): 2.66 - radius = 0.1 2.67 - pass
3.1 --- a/game.py Mon Dec 20 16:05:49 2010 +0300 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,50 +0,0 @@ 3.4 -other_tanks = [] 3.5 -bullets = [] 3.6 - 3.7 -class Game(object): 3.8 - def __init__(self, bodies, users, width, height): 3.9 - self.bodies = bodies 3.10 - self.users = users 3.11 - self.width = width 3.12 - self.height = height 3.13 - 3.14 - def step(game): 3.15 - game.next_positions() 3.16 - game.check_collisions() 3.17 - game.check_walls() 3.18 - game.update_positions() 3.19 - game.invoke_ticks() 3.20 - game.respawn() 3.21 - 3.22 - def next_positions(game): 3.23 - delta_t = 1 3.24 - for i in game.bodies : 3.25 - i.next_position = i.position + i.velocity*(delta_t) 3.26 - 3.27 - def check_collisions(game): 3.28 - pass 3.29 - 3.30 - def collides(self,body1,body2): 3.31 - pass 3.32 - 3.33 - def handle_collision(self,body1,body2): 3.34 - pass 3.35 - 3.36 - def check_walls(game): 3.37 - for i in game.bodies : 3.38 - 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) : 3.39 - i.on_wall() 3.40 - 3.41 - def update_positions(game): 3.42 - for i in game.bodies : 3.43 - i.position = i.next_position 3.44 - 3.45 - def invoke_ticks(game): 3.46 - for i in game.users : 3.47 - i.tank.on_tick(other_tanks,bullets) 3.48 - 3.49 - def respawn(game): 3.50 - for i in game.users : 3.51 - if i.tank.strength == 0 : 3.52 - i.tank.on_spawn() 3.53 - i.tank.strength = 1
4.1 --- a/user.py Mon Dec 20 16:05:49 2010 +0300 4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 @@ -1,15 +0,0 @@ 4.4 -class User(object): 4.5 - 4.6 - def __init__(self, base_left = False, base_right = False, 4.7 - turret_left = False, turret_right = False, 4.8 - accelerate = False, decelerate = False, 4.9 - fire = False, tank = 0): 4.10 - self.base_left = base_left 4.11 - self.base_right = base_right 4.12 - self.turret_left = turret_left 4.13 - self.turret_right = turret_right 4.14 - self.accelerate = accelerate 4.15 - self.decelerate = decelerate 4.16 - self.fire = fire 4.17 - self.tank = tank 4.18 -
5.1 --- a/vector.py Mon Dec 20 16:05:49 2010 +0300 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,57 +0,0 @@ 5.4 -import math 5.5 - 5.6 -class Vector(object): 5.7 - 5.8 - def __init__(self, x=0 , y=0): 5.9 - self.x = x 5.10 - self.y = y 5.11 - 5.12 - def __add__(self, other): 5.13 - result = Vector(0,0) 5.14 - result.x = self.x + other.x 5.15 - result.y = self.y + other.y 5.16 - return result 5.17 - 5.18 - def __mul__(self, alpha): 5.19 - result = Vector() 5.20 - result.x = self.x * alpha 5.21 - result.y = self.y * alpha 5.22 - return result 5.23 - 5.24 - def dot_product(self, other): 5.25 - return self.x*other.x + self.y*other.y 5.26 - 5.27 - def __abs__(self): 5.28 - return (self.x**2 + self.y**2)**0.5 5.29 - 5.30 - def __str__(self): 5.31 - return "(%s, %s)" % (self.x, self.y) 5.32 - 5.33 - def is_null(self): 5.34 - if abs(self) == 0 : 5.35 - return 1 5.36 - else : 5.37 - return 0 5.38 - 5.39 - def get_rho(self): 5.40 - return abs(self) 5.41 - 5.42 - def set_rho(self, new_rho): 5.43 - if self.is_null() == 1 : 5.44 - self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi) 5.45 - else : 5.46 - self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self)) 5.47 - 5.48 - rho = property(get_rho, set_rho) 5.49 - 5.50 - def get_phi(self): 5.51 - phi = math.pi/2 - math.atan2(self.x, self.y) 5.52 - if self.is_null == 1: 5.53 - phi = 0 5.54 - return phi 5.55 - 5.56 - def set_phi(self, new_phi): 5.57 - rho = abs(self) 5.58 - self.x, self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi) 5.59 - 5.60 - phi = property(get_phi, set_phi)