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

tanchiki

changeset 34:b7a85caedc7f new

Re-imported files after merge
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 16:12:55 +0300
parents a92686be9c95
children 1a0bddee3c54
files body.py game.py tk_ui.py user.py vector.py
diffstat 5 files changed, 236 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/body.py	Mon Dec 20 16:12:55 2010 +0300
     1.3 @@ -0,0 +1,64 @@
     1.4 +import vector
     1.5 +import math
     1.6 +delta_phi = math.pi   # deltha phi = math.pi
     1.7 +speed_delta = 1
     1.8 +delta_t = 1
     1.9 +max_velocity = 2
    1.10 +initial_strength = 1
    1.11 +
    1.12 +class Body(object):
    1.13 +	def __init__(self, position, velocity = vector.null):
    1.14 +		self.position = position
    1.15 +		self.velocity = velocity
    1.16 +		self.radius = radius
    1.17 +
    1.18 +class Tank(Body):
    1.19 +	radius = 1
    1.20 +	def __init__(self, position, user):
    1.21 +		Body.__init__(self, position)
    1.22 +		self.strength = 0
    1.23 +		self.turret = vector.i
    1.24 +		self.base_orientation = 1	# 1 or -1
    1.25 +		self.user = user
    1.26 +		user.tank = self # добавляет себя в User
    1.27 +
    1.28 +	def rotate_base(tank, angle): 
    1.29 +		self.velocity.phi += angle
    1.30 +
    1.31 +	def rotate_turret(self, angle):
    1.32 +		self.turret.phi += angle
    1.33 +
    1.34 +	def accelerate(self, speed_delta):
    1.35 +		self.velocity.rho += speed_delta * delta_t
    1.36 +		if self.velocity.rho > max_velocity :
    1.37 +			self.velocity.rho = max_velocity
    1.38 +
    1.39 +	def fire(self):
    1.40 +		pass
    1.41 +
    1.42 +	def on_tick(self,other_tanks, bullets):
    1.43 +		if self.user.base_left == True :
    1.44 +			self.rotate_base(delta_phi)
    1.45 +		if self.user.base_right == True :
    1.46 +			self.rotate_base(-1*delta_phi)
    1.47 +		if self.user.accelerate == True :
    1.48 +			self.accelerate(speed_delta)
    1.49 +
    1.50 +	def on_spawn(self):
    1.51 +		pass
    1.52 +
    1.53 +	def on_death(self):
    1.54 +		pass
    1.55 +
    1.56 +	def on_hit(self,bullet):
    1.57 +		pass
    1.58 +
    1.59 +	def on_collision(self):
    1.60 +		pass
    1.61 +
    1.62 +	def on_wall(self):
    1.63 +		pass
    1.64 +
    1.65 +class Bullet(Body):
    1.66 +	radius = 0.1
    1.67 +	pass
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/game.py	Mon Dec 20 16:12:55 2010 +0300
     2.3 @@ -0,0 +1,50 @@
     2.4 +other_tanks = []
     2.5 +bullets = []
     2.6 +
     2.7 +class Game(object):
     2.8 +	def __init__(self, bodies, users, width, height):
     2.9 +		self.bodies = bodies
    2.10 +		self.users = users
    2.11 +		self.width = width
    2.12 +		self.height = height
    2.13 +
    2.14 +	def step(game):
    2.15 +		game.next_positions()
    2.16 +		game.check_collisions()
    2.17 +		game.check_walls()
    2.18 +		game.update_positions()
    2.19 +		game.invoke_ticks()
    2.20 +		game.respawn()
    2.21 +
    2.22 +	def next_positions(game):
    2.23 +		delta_t = 1
    2.24 +		for i in game.bodies :
    2.25 +			i.next_position = i.position + i.velocity*(delta_t)
    2.26 +
    2.27 +	def check_collisions(game):
    2.28 +		pass
    2.29 +
    2.30 +	def collides(self,body1,body2):
    2.31 +		pass
    2.32 +
    2.33 +	def handle_collision(self,body1,body2):
    2.34 +		pass
    2.35 +
    2.36 +	def check_walls(game):
    2.37 +		for i in game.bodies :
    2.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) :
    2.39 +				i.on_wall()
    2.40 +
    2.41 +	def update_positions(game):
    2.42 +		for i in game.bodies :
    2.43 +			i.position = i.next_position
    2.44 +
    2.45 +	def invoke_ticks(game):
    2.46 +		for i in game.users :
    2.47 +			i.tank.on_tick(other_tanks,bullets)
    2.48 +	
    2.49 +	def respawn(game):
    2.50 +		for i in game.users :
    2.51 +			if i.tank.strength == 0 :
    2.52 +				i.tank.on_spawn()
    2.53 +				i.tank.strength = 1 
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tk_ui.py	Mon Dec 20 16:12:55 2010 +0300
     3.3 @@ -0,0 +1,50 @@
     3.4 +import math
     3.5 +import Tkinter as tk
     3.6 +from game import Game
     3.7 +from body import Tank, Bullet
     3.8 +from vector import Vector
     3.9 +from user_controller import UserController
    3.10 +
    3.11 +class UI(object):
    3.12 +	def __init__(self):
    3.13 +		self.init_game()
    3.14 +		self.init_ui()
    3.15 +		self.step()
    3.16 +
    3.17 +	def init_ui(self):
    3.18 +		root = self.root = tk.Tk()
    3.19 +		canvas = self.canvas = tk.Canvas(root, background="black")
    3.20 +		canvas.pack(fill="both", expand="yes")
    3.21 +		canvas.bind("<Keypress>", self.on_key)
    3.22 +
    3.23 +	def on_key(self, ev):
    3.24 +		self.controller
    3.25 +
    3.26 +	def init_game(self):
    3.27 +		bodies = []
    3.28 +		self.game = Game(100, 100, bodies)
    3.29 +		tank = Tank(self.game, Vector.null, None)
    3.30 +		controller = UserController('a', 'd', 'q', 'e', 'w', 's', 'x', tank)
    3.31 +		tank.controller = controller
    3.32 +		bodies.append(tank)
    3.33 +
    3.34 +	def step(self):
    3.35 +		self.root.after(100, self.step)
    3.36 +		self.game.step(1)
    3.37 +		self.redraw()
    3.38 +
    3.39 +	def redraw(self):
    3.40 +		self.canvas.delete("all")
    3.41 +		for body in self.game.bodies:
    3.42 +			p = body.position
    3.43 +			lt = p + Vector(-1,-1) * body.radius
    3.44 +			rb = p + Vector(1,1) * body.radius
    3.45 +			if isinstance(body, Tank):
    3.46 +				t = body.turret * body.radius + p
    3.47 +				self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="green")
    3.48 +				self.canvas.create_line(p.x, p.y, t.x, t.y, fill="yellow", width=2)
    3.49 +			elif isinstance(body, Bullet):
    3.50 +				self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="red")
    3.51 +
    3.52 +if __name__ == "__main__":
    3.53 +	UI().root.mainloop()
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/user.py	Mon Dec 20 16:12:55 2010 +0300
     4.3 @@ -0,0 +1,15 @@
     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 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/vector.py	Mon Dec 20 16:12:55 2010 +0300
     5.3 @@ -0,0 +1,57 @@
     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)