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

# HG changeset patch
# User Olga Zolotareva
# Date 1293217808 -10800
# Node ID d740eff76e7ecb9a9cdf4b0628de6a475cd7134e
# Parent f6d3c0539e8ccefc40a5a7a1fd76aac06685c70e
some bugs corrected

diff -r f6d3c0539e8c -r d740eff76e7e body.py
--- a/body.py Mon Dec 20 21:27:23 2010 +0300
+++ b/body.py Fri Dec 24 22:10:08 2010 +0300
@@ -4,18 +4,20 @@

base_angle = math.pi/32 # deltha phi = math.pi/32
turret_angle = math.pi/32
-speed_delta = 1
+speed_delta = 2
delta_t = 1
max_velocity = 4
+acceleration = 1
max_base_angle = 1
max_turret_angle = 1
initial_strength = 1
bullet_velocity = 10
-width = 100
-height = 100
+width = 500
+height = 500
+bullet_speed = 10

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

@@ -24,18 +26,23 @@
def __init__(self, position, user, game):
Body.__init__(self, position)
self.game = game
- self.turret = vector.i
+ self.turret = vector.Vector(0,-1)
self.strength = 0
- self.base_orientation = 1 # 1 or -1
+ self.base_orientation = vector.Vector(0,-1) # Vector
self.user = user
user.tank = self


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

def rotate_turret(self, angle):
if abs(angle) < max_base_angle:
@@ -45,14 +52,15 @@


def accelerate(self, speed_delta):
- self.velocity += self.velocity.normalize() * speed_delta * delta_t
- print self.velocity.rho
- if self.velocity.rho > max_velocity:
+ print 'speed_delta', speed_delta #test
+ self.velocity += self.base_orientation*speed_delta*delta_t
+ if abs(self.velocity) > max_velocity:
self.velocity.rho = max_velocity

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

@@ -77,5 +85,9 @@
class Bullet(Body):
radius = 0.1
def __init__(self, position, velocity, tank):
- Body.__init__(self, position, velocity = bullet_velocity)
self.tank = tank
+ Body.__init__(self, position, velocity = self.tank.turret*bullet_velocity)
+
+ def on_wall(self):
+ pass
+
diff -r f6d3c0539e8c -r d740eff76e7e game.py
--- a/game.py Mon Dec 20 21:27:23 2010 +0300
+++ b/game.py Fri Dec 24 22:10:08 2010 +0300
@@ -1,9 +1,12 @@
import vector
import body
import time
+import random

other_tanks = []
bullets = []
+width = 500
+height = 500

class Game(object):
def __init__(self, bodies, users, width, height):
@@ -12,27 +15,38 @@
self.width = width
self.height = height

- def step(self):
- for i in self.bodies: #test
- print "begin", i, i.position
+ def step(self):
self.next_positions()
self.check_collisions()
self.check_walls()
self.update_positions()
self.invoke_ticks()
self.respawn()
+ self.update_velocities()
+ for i in self.bodies :
+ print i
+ # print 'next position' , i.next_position
+ print 'update_position' , i.position
+ print 'velocity' , i.velocity
+ print 'velocity.rho', i.velocity.rho # test
+ print '\n'
+ if isinstance(i,body.Tank) == True:
+ print 'base_orientation' , i.base_orientation

- for i in self.bodies: #test
- print "end", i, i.position #test
- # time.sleep(1) #test
- # self.step() #test
-
+ def update_velocities(self):
+ for i in self.bodies:
+ if isinstance(i, body.Tank):
+ if abs(i.velocity) > 1e-10 :
+ i.velocity = i.velocity - i.velocity.normalize()*body.acceleration
+ else :
+ i.velocity.rho = 0

def next_positions(self):
delta_t = 1
for i in self.bodies:
- i.next_position = i.position + i.velocity*(delta_t)
-
+ i.next_position = i.position + i.velocity*body.delta_t
+ if isinstance(i, body.Bullet) == True:
+ print 'Bullet', i

def check_collisions(self):
for i in self.bodies:
@@ -41,16 +55,13 @@
self.handle_collision(i,j)

def collides(self,body1,body2):
- print body1, body2
- if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)):
- print 'collision'
- print body1.position , body2.position
- if (body1 != body2):
- return True
- else :
- return False
- else :
- return False
+# if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)): if (body1 != body2):
+# print 'collision'
+# return True
+# else :
+# return False
+# else :
+ return False

def handle_collision(self,body1,body2):
if isinstance(body1, body.Tank) == True :
@@ -84,10 +95,13 @@
for i in self.users :
if i.tank.strength == 0 :
i.tank.on_spawn()
+ print 'respawn'
+ print i.tank.strength , i.tank.position
i.tank.strength = 1
i.tank.velocity = vector.null
- i.tank.position.x = random.randint(self.radius , width - self.radius)
- i.tank.position.y = random.randint(self.radius , height - self.radius)
+ i.tank.position.x = random.randint(i.tank.radius , width - i.tank.radius)
+ i.tank.position.y = random.randint(i.tank.radius , height - i.tank.radius)
i.tank.velocity = vector.null
+ print i.tank.strength , i.tank.position


\ No newline at end of file
diff -r f6d3c0539e8c -r d740eff76e7e tk_ui.py
--- a/tk_ui.py Mon Dec 20 21:27:23 2010 +0300
+++ b/tk_ui.py Fri Dec 24 22:10:08 2010 +0300
@@ -9,13 +9,13 @@

game_size = 500, 500
keys = {
- 'a': 'base_left',
- 'd': 'base_right',
- 'q': 'turret_left',
- 'e': 'turret_right',
- 'w': 'accelerate',
- 's': 'decelerate',
- 'x': 'fire',
+ 'ocircumflex': 'base_left',
+ 'acircumflex': 'base_right',
+ 'eacute': 'turret_left',
+ 'oacute': 'turret_right',
+ 'odiaeresis': 'accelerate',
+ 'ucircumflex': 'decelerate',
+ 'division': 'fire',
}

welcome = """Press F5 to start
@@ -30,17 +30,17 @@

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

@@ -53,7 +53,7 @@

def init_game(self):
self.user = User(keys)
- w, h = game_size
+ w, h = game_size
game = self.game = Game([], [self.user], w, h)
tank = Tank(Vector(*game_size) * 0.5, self.user, self.game)
game.bodies.append(tank)
@@ -90,25 +90,28 @@

def redraw(self):
self.canvas.delete("all")
- for body in self.game.bodies:
- p = body.position
- lt = p + Vector(-1,-1) * body.radius
- rb = p + Vector(1,1) * body.radius
+ for i in self.game.bodies:
+ p = i.position
+ lt = p + Vector(-1,-1) * i.radius
+ rb = p + Vector(1,1) * i.radius

- v1 = Vector(body.radius, 0)
- v1.phi = body.velocity.phi
+ v1 = Vector(i.radius, 0)
+ if isinstance(i, body.Tank):
+ v1.phi = i.base_orientation.phi
+ else :
+ v1.phi = i.velocity.phi
vb = p - v1
ve = p + v1 * 2
- self.canvas.create_line(vb.x, vb.y, ve.x, ve.y, fill="darkgray", width=body.radius*2)
+ self.canvas.create_line(vb.x, vb.y, ve.x, ve.y, fill="darkgray", width=i.radius*2)

- if isinstance(body, BaseTank):
- t = body.turret * body.radius * 1.5 + p
+ if isinstance(i, BaseTank):
+ t = i.turret * i.radius * 1.5 + p
self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="darkgreen")
self.canvas.create_line(p.x, p.y, t.x, t.y, fill="orange", width=2)
- elif isinstance(body, Bullet):
+ elif isinstance(i, Bullet):
self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, outline="red")
else:
- raise AssertionError("Unknown object type: %s" % body.__class__.__name__)
+ raise AssertionError("Unknown object type: %s" % i.__class__.__name__)

if __name__ == "__main__":
UI().root.mainloop()
diff -r f6d3c0539e8c -r d740eff76e7e user.py
--- a/user.py Mon Dec 20 21:27:23 2010 +0300
+++ b/user.py Fri Dec 24 22:10:08 2010 +0300
@@ -11,11 +11,13 @@
self.accelerate = False
self.decelerate = False
self.fire = False
+
+ def on_key(self, key, value):
+ if key not in self.keyset:
+ return
+ else:
+ action = self.keyset[key]
+ print 'action=', action
+ if hasattr(self, action) and action not in ['tank', 'keyset']:
+ setattr(self, action, value)

- def on_key(self, key, value):
- if key not in self.keyset:
- return
- action = self.keyset[key]
- if hasattr(self, action) and action not in ['tank', 'keyset']:
- setattr(self, action, value)
-
diff -r f6d3c0539e8c -r d740eff76e7e vector.py
--- a/vector.py Mon Dec 20 21:27:23 2010 +0300
+++ b/vector.py Fri Dec 24 22:10:08 2010 +0300
@@ -7,13 +7,13 @@
self.y = y

def __add__(self, other):
- result = Vector(0,0)
+ result = Vector()
result.x = self.x + other.x
result.y = self.y + other.y
return result

def __sub__(self, other):
- result = Vector(0,0)
+ result = Vector()
result.x = self.x - other.x
result.y = self.y - other.y
return result