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

# HG changeset patch
# User Daniil Alexeyevsky
# Date 1292850775 -10800
# Node ID b7a85caedc7f13c63a83881fb25b2c4e371a63e7
# Parent a92686be9c95c31b91d91aa1be6952eb5cfb02bb
Re-imported files after merge

diff -r a92686be9c95 -r b7a85caedc7f body.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/body.py Mon Dec 20 16:12:55 2010 +0300
@@ -0,0 +1,64 @@
+import vector
+import math
+delta_phi = math.pi # deltha phi = math.pi
+speed_delta = 1
+delta_t = 1
+max_velocity = 2
+initial_strength = 1
+
+class Body(object):
+ def __init__(self, position, velocity = vector.null):
+ self.position = position
+ self.velocity = velocity
+ self.radius = radius
+
+class Tank(Body):
+ radius = 1
+ def __init__(self, position, user):
+ Body.__init__(self, position)
+ self.strength = 0
+ self.turret = vector.i
+ self.base_orientation = 1 # 1 or -1
+ self.user = user
+ user.tank = self # добавляет себя в User
+
+ def rotate_base(tank, angle):
+ self.velocity.phi += angle
+
+ def rotate_turret(self, angle):
+ self.turret.phi += angle
+
+ def accelerate(self, speed_delta):
+ self.velocity.rho += speed_delta * delta_t
+ if self.velocity.rho > max_velocity :
+ self.velocity.rho = max_velocity
+
+ def fire(self):
+ pass
+
+ def on_tick(self,other_tanks, bullets):
+ if self.user.base_left == True :
+ self.rotate_base(delta_phi)
+ if self.user.base_right == True :
+ self.rotate_base(-1*delta_phi)
+ if self.user.accelerate == True :
+ self.accelerate(speed_delta)
+
+ def on_spawn(self):
+ pass
+
+ def on_death(self):
+ pass
+
+ def on_hit(self,bullet):
+ pass
+
+ def on_collision(self):
+ pass
+
+ def on_wall(self):
+ pass
+
+class Bullet(Body):
+ radius = 0.1
+ pass
diff -r a92686be9c95 -r b7a85caedc7f game.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/game.py Mon Dec 20 16:12:55 2010 +0300
@@ -0,0 +1,50 @@
+other_tanks = []
+bullets = []
+
+class Game(object):
+ def __init__(self, bodies, users, width, height):
+ self.bodies = bodies
+ self.users = users
+ 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 next_positions(game):
+ delta_t = 1
+ for i in game.bodies :
+ i.next_position = i.position + i.velocity*(delta_t)
+
+ def check_collisions(game):
+ pass
+
+ def collides(self,body1,body2):
+ pass
+
+ def handle_collision(self,body1,body2):
+ pass
+
+ 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) :
+ i.on_wall()
+
+ def update_positions(game):
+ for i in game.bodies :
+ i.position = i.next_position
+
+ def invoke_ticks(game):
+ for i in game.users :
+ i.tank.on_tick(other_tanks,bullets)
+
+ def respawn(game):
+ for i in game.users :
+ if i.tank.strength == 0 :
+ i.tank.on_spawn()
+ i.tank.strength = 1
diff -r a92686be9c95 -r b7a85caedc7f tk_ui.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tk_ui.py Mon Dec 20 16:12:55 2010 +0300
@@ -0,0 +1,50 @@
+import math
+import Tkinter as tk
+from game import Game
+from body import Tank, Bullet
+from vector import Vector
+from user_controller import UserController
+
+class UI(object):
+ def __init__(self):
+ self.init_game()
+ self.init_ui()
+ self.step()
+
+ def init_ui(self):
+ root = self.root = tk.Tk()
+ canvas = self.canvas = tk.Canvas(root, background="black")
+ canvas.pack(fill="both", expand="yes")
+ canvas.bind("", self.on_key)
+
+ def on_key(self, ev):
+ self.controller
+
+ def init_game(self):
+ bodies = []
+ self.game = Game(100, 100, bodies)
+ tank = Tank(self.game, Vector.null, None)
+ controller = UserController('a', 'd', 'q', 'e', 'w', 's', 'x', tank)
+ tank.controller = controller
+ bodies.append(tank)
+
+ def step(self):
+ self.root.after(100, self.step)
+ self.game.step(1)
+ self.redraw()
+
+ 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
+ if isinstance(body, Tank):
+ t = body.turret * body.radius + p
+ self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="green")
+ self.canvas.create_line(p.x, p.y, t.x, t.y, fill="yellow", width=2)
+ elif isinstance(body, Bullet):
+ self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="red")
+
+if __name__ == "__main__":
+ UI().root.mainloop()
diff -r a92686be9c95 -r b7a85caedc7f user.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/user.py Mon Dec 20 16:12:55 2010 +0300
@@ -0,0 +1,15 @@
+class User(object):
+
+ def __init__(self, base_left = False, base_right = False,
+ turret_left = False, turret_right = False,
+ accelerate = False, decelerate = False,
+ fire = False, tank = 0):
+ self.base_left = base_left
+ self.base_right = base_right
+ self.turret_left = turret_left
+ self.turret_right = turret_right
+ self.accelerate = accelerate
+ self.decelerate = decelerate
+ self.fire = fire
+ self.tank = tank
+
diff -r a92686be9c95 -r b7a85caedc7f vector.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vector.py Mon Dec 20 16:12:55 2010 +0300
@@ -0,0 +1,57 @@
+import math
+
+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
+ result.y = self.y + other.y
+ return result
+
+ def __mul__(self, alpha):
+ result = Vector()
+ result.x = self.x * alpha
+ result.y = self.y * alpha
+ return result
+
+ def dot_product(self, other):
+ return self.x*other.x + self.y*other.y
+
+ def __abs__(self):
+ return (self.x**2 + self.y**2)**0.5
+
+ def __str__(self):
+ return "(%s, %s)" % (self.x, self.y)
+
+ def is_null(self):
+ if abs(self) == 0 :
+ return 1
+ else :
+ return 0
+
+ def get_rho(self):
+ return abs(self)
+
+ def set_rho(self, new_rho):
+ 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))
+
+ rho = property(get_rho, set_rho)
+
+ def get_phi(self):
+ phi = math.pi/2 - math.atan2(self.x, self.y)
+ if self.is_null == 1:
+ phi = 0
+ 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)
+
+ phi = property(get_phi, set_phi)