Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/tanchiki/raw-rev/4ef6336d1707
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 10:38:42 2012
Кодировка:

# HG changeset patch
# User Peter Zotov
# Date 1292833968 -10800
# Node ID 4ef6336d1707056e8c9a118a7a31973ed4e5a2f4
# Parent b9856f9de9e2f856aebfc09b09933ca9ae1eaafd# Parent 43f5b82f3491387c82fb4a33a32c474ee516c064
Merge changes.

diff -r b9856f9de9e2 -r 4ef6336d1707 tanchiki/body.py
--- a/tanchiki/body.py Mon Dec 20 11:03:40 2010 +0300
+++ b/tanchiki/body.py Mon Dec 20 11:32:48 2010 +0300
@@ -4,7 +4,8 @@
import math

class Body(object):
- def __init__(self, position, velocity = vector.Vector.null):
+ def __init__(self, game, position, velocity = vector.Vector.null):
+ self.game = game
self.position = position
self.velocity = velocity

@@ -12,28 +13,39 @@
radius = 1
model = "tank"

- def __init__(self, position, controller):
- Body.__init__(self, position)
+ def __init__(self, game, position, controller):
+ Body.__init__(self, game, position)
self.controller = controller
self.turret = vector.Vector()
self.strength = 0

def rotate_base(tank, angle):
- self.velocity.phi += angle
+ if abs(angle) < self.game.max_base_delta :
+ self.velocity.phi += angle
+ else :
+ self.velocity.phi += self.game.max_base_delta

def rotate_turret(self, angle):
- self.turret.phi += angle
+ if abs(angle) < self.game.max_base_delta :
+ self.turret.phi += angle
+ else :
+ self.turret.phi += self.game.max_turret_delta

def accelerate(self, speed_delta):
- self.velocity.rho += speed_delta * delta_t
+ self.velocity += self.velocity.normalize() * speed_delta
if self.velocity.rho > max_velocity :
self.velocity.rho = max_velocity

def fire(self):
- pass
+ bullet_position = self.position + self.turret * (self.radius + 0.1)
+ bullet_velocity = self.turret.normalize() * self.game.bullet_speed
+ bullet = Bullet(bullet_position, bullet_velocity, self)
+ self.game.bodies.append(bullet)

class Bullet(Body):
radius = 0.1
model = "bullet"

- pass
+ def __init__(self, position, velocity, tank):
+ Body.__init__(self, position, velocity)
+ self.origin = origin
diff -r b9856f9de9e2 -r 4ef6336d1707 tanchiki/game.py
--- a/tanchiki/game.py Mon Dec 20 11:03:40 2010 +0300
+++ b/tanchiki/game.py Mon Dec 20 11:32:48 2010 +0300
@@ -23,16 +23,31 @@
i.position = i.next_position

def __collides(self, body1, body2):
- pass
+ return (abs(body1.position - body2.position) <= (body1.radius + body2.radius))
+ and (body1 != body2)

def __handle_collision(self, body1, body2):
- pass
+ if isinstance(body1, body.Tank) == True :
+ if isinstance(body2, body.Tank) == True :
+ body1.on_collision(body2)
+ body2.on_collision(body1)
+ else :
+ body1.on_hit()
+ body1.on_death()
+ else :
+ if isinstance(body2, body.Tank) == True :
+ body2.on_hit()
+ body2.on_death()

def __check_collisions(game):
- pass
+ for i in range(len(self.bodies)) :
+ for j in range(i, len(self.bodies)) :
+ a, b = self.bodies[i], self.bodies[j]
+ if self.collides(a, b) == True :
+ self.handle_collision(a, b)

- def __check_walls(game):
- for i in game.bodies :
+ def __check_walls(self):
+ for i in self.bodies :
if ((i.next_position.x - i.radius) <= -game.width/2) or \
((i.next_position.y - i.radius) <= -game.width/2) or \
((i.next_position.x + i.radius) >= game.width/2) or \
diff -r b9856f9de9e2 -r 4ef6336d1707 tanchiki/vector.py
--- a/tanchiki/vector.py Mon Dec 20 11:03:40 2010 +0300
+++ b/tanchiki/vector.py Mon Dec 20 11:32:48 2010 +0300
@@ -1,10 +1,11 @@
import math

-class Vector(object):
- def __init__(self, x=0 , y=0):
+class Vector(object):
+
+ def __init__(self, x=0 , y=0):
self.x = x
self.y = y
-
+
def __add__(self, other):
result = Vector(0,0)
result.x = self.x + other.x
@@ -18,7 +19,7 @@
return result

def dot_product(self, other):
- return self.x*other.x + self.y*other.y
+ return self.x*other.x + self.y*other.y

def __abs__(self):
return (self.x**2 + self.y**2)**0.5
@@ -32,11 +33,18 @@
else :
return 0

+ def normalize(self):
+ result = Vector()
+ result.x = self.x
+ result.y = self.y
+ result.rho = 1
+ return result
+
def get_rho(self):
return abs(self)

def set_rho(self, new_rho):
- if self.is_null() == 1 :
+ if self.is_null() == 1:
self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi)
else :
self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self))
@@ -47,12 +55,10 @@
phi = math.pi/2 - math.atan2(self.x, self.y)
if self.is_null == 1:
phi = 0
- return phi
+ return phi

def set_phi(self, new_phi):
rho = abs(self)
- self.x, self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi)
+ self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi)

phi = property(get_phi, set_phi)
-
-Vector.null = Vector(0,0)
\ No newline at end of file