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

# HG changeset patch
# User Olga Zolotareva
# Date 1292831627 -10800
# Node ID 43f5b82f3491387c82fb4a33a32c474ee516c064
# Parent 3634a208da61d0daa8b0f6fccadfa9faa749d8a0
modules changed

diff -r 3634a208da61 -r 43f5b82f3491 body.py
--- a/body.py Sun Dec 19 16:04:29 2010 +0300
+++ b/body.py Mon Dec 20 10:53:47 2010 +0300
@@ -1,66 +1,95 @@
import vector
import math
-delta_phi = math.pi # deltha phi = math.pi
+import random
+
+base_angle = math.pi/32 # deltha phi = math.pi/32
+turret_angle = math.pi/32
speed_delta = 1
delta_t = 1
max_velocity = 2
+max_base_angle = 1
+max_turret_angle = 1
initial_strength = 1
+bullet_velocity = 5
+width = 10
+height = 10

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

class Tank(Body):
- radius = 1
+ radius = 0.5
def __init__(self, position, user):
Body.__init__(self, position)
+ self.turret = vector.i
self.strength = 0
- self.turret = vector.i
self.base_orientation = 1 # 1 or -1
self.user = user
- user.tank = self # добавляет себя в User
+ user.tank = self


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

def rotate_turret(self, angle):
- self.turret.phi += angle
+ if abs(angle) < max_base_angle:
+ self.turret.phi += angle
+ else:
+ self.turret.phi += max_turret_angle
+

def accelerate(self, speed_delta):
- self.velocity.rho += speed_delta * delta_t
+ self.velocity += self.velocity.normalize() * speed_delta * delta_t
+ print self.velocity.rho
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 = Bullet(bullet_position, bullet_velocity, self)
+ bodies.append(bullet)

def on_tick(self,other_tanks, bullets):
if self.user.base_left == True:
- self.rotate_base(delta_phi)
+ self.rotate_base(base_angle)
if self.user.base_right == True:
- self.rotate_base(-1*delta_phi)
+ self.rotate_base(-1*base_angle)
if self.user.accelerate == True:
self.accelerate(speed_delta)
+ if self.user.decelerate == True:
+ self.accelerate(-1*speed_delta)
+ if self.user.turret_left == True:
+ self.rotate_turret(turret_angle)
+ if self.user.turret_right == True:
+ self.rotate_turret(-1*turret_angle)
+ if self.user.fire == True:
+ self.fire()

def on_spawn(self):
pass
+# self.position.x = random.randint(1,width - 10)
+# self.position.y = random.randint(1,height - 10)
+# self.velocity = vector.null

def on_death(self) :
pass

- def on_hit(self,bullet):
+ def on_hit(self, bullet):
pass

- def on_collision(self):
+ def on_collision(self, other):
pass

def on_wall(self):
- pass
+ self.next_position = self.position

class Bullet(Body):
radius = 0.1
- pass
\ No newline at end of file
+ def __init__(self, position, velocity, tank):
+ Body.__init__(self, position, velocity)
+ self.tank = tank
\ No newline at end of file
diff -r 3634a208da61 -r 43f5b82f3491 game.py
--- a/game.py Sun Dec 19 16:04:29 2010 +0300
+++ b/game.py Mon Dec 20 10:53:47 2010 +0300
@@ -1,3 +1,5 @@
+import vector
+
other_tanks = []
bullets = []

@@ -8,45 +10,62 @@
self.width = width
self.height = height

- def step(game):
- game.next_positions()
- game.check_collisions()
- game.check_walls()
- game.update_positions()
- game.invoke_ticks()
- game.respawn()
+ def step(self):
+ self.next_positions()
+ self.check_collisions()
+ self.check_walls()
+ self.update_positions()
+ self.invoke_ticks()
+ self.respawn()
+ for i in self.bodies: #test
+ print i.position #test

- def next_positions(game):
+ def next_positions(self):
delta_t = 1
- for i in game.bodies:
+ for i in self.bodies:
i.next_position = i.position + i.velocity*(delta_t)


- def check_collisions(game):
- pass
+ def check_collisions(self):
+ for i in self.bodies:
+ for j in self.bodies:
+ if self.collides(i,j) == True :
+ self.handle_collision(i,j)

def collides(self,body1,body2):
- pass
+ if (abs(body1.position + body2.position*-1) <= (body1.radius + body2.radius)) and (body1 != body2) :
+ print 'collision'

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

- def check_walls(game):
- for i in game.bodies :
- 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) :
+ def check_walls(self):
+ for i in self.bodies :
+ if ((i.next_position.x - i.radius) <= 0) or ((i.next_position.y - i.radius) <= 0) or ((i.next_position.x + i.radius) >= self.width) or ((i.next_position.y + i.radius) >= self.height) :
i.on_wall()
-
+ print 'wall' #test

- def update_positions(game):
- for i in game.bodies:
+ def update_positions(self):
+ for i in self.bodies:
i.position = i.next_position

- def invoke_ticks(game):
- for i in game.users:
+ def invoke_ticks(self):
+ for i in self.users:
i.tank.on_tick(other_tanks,bullets)
-
- def respawn(game):
- for i in game.users :
+
+ def respawn(self):
+ for i in self.users :
if i.tank.strength == 0 :
i.tank.on_spawn()
i.tank.strength = 1
diff -r 3634a208da61 -r 43f5b82f3491 vector.py
--- a/vector.py Sun Dec 19 16:04:29 2010 +0300
+++ b/vector.py Mon Dec 20 10:53:47 2010 +0300
@@ -33,6 +33,13 @@
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)