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

tanchiki

changeset 15:43f5b82f3491

modules changed
author Olga Zolotareva <olya_zol@inbox.ru>
date Mon, 20 Dec 2010 10:53:47 +0300
parents 3634a208da61
children 4ef6336d1707
files body.py game.py vector.py
diffstat 3 files changed, 96 insertions(+), 41 deletions(-) [+]
line diff
     1.1 --- a/body.py	Sun Dec 19 16:04:29 2010 +0300
     1.2 +++ b/body.py	Mon Dec 20 10:53:47 2010 +0300
     1.3 @@ -1,66 +1,95 @@
     1.4  import vector
     1.5  import math
     1.6 -delta_phi = math.pi   # deltha phi = math.pi
     1.7 +import random
     1.8 +
     1.9 +base_angle = math.pi/32   # deltha phi = math.pi/32
    1.10 +turret_angle = math.pi/32
    1.11  speed_delta = 1
    1.12  delta_t = 1
    1.13  max_velocity = 2
    1.14 +max_base_angle = 1
    1.15 +max_turret_angle = 1
    1.16  initial_strength = 1
    1.17 +bullet_velocity = 5
    1.18 +width = 10
    1.19 +height = 10
    1.20  
    1.21  class Body(object):
    1.22  	def __init__(self, position, velocity = vector.null):
    1.23  		self.position = position
    1.24  		self.velocity = velocity
    1.25 -		self.radius = radius
    1.26 -	
    1.27  
    1.28  class Tank(Body):
    1.29 -	radius = 1
    1.30 +	radius = 0.5
    1.31  	def __init__(self, position, user):
    1.32  		Body.__init__(self, position)
    1.33 +		self.turret = vector.i
    1.34  		self.strength = 0
    1.35 -		self.turret = vector.i
    1.36  		self.base_orientation = 1	# 1 or -1
    1.37  		self.user = user
    1.38 -		user.tank = self # добавляет себя в User
    1.39 +		user.tank = self 
    1.40  
    1.41  
    1.42 -	def rotate_base(tank, angle): 
    1.43 -		self.velocity.phi += angle
    1.44 +	def rotate_base(tank, angle):
    1.45 +		if abs(angle) < max_base_angle:  
    1.46 +			self.velocity.phi += angle
    1.47 +		else:
    1.48 +			self.velocity.phi += max_base_angle
    1.49  
    1.50  	def rotate_turret(self, angle):
    1.51 -		self.turret.phi += angle
    1.52 +		if abs(angle) < max_base_angle:  
    1.53 +			self.turret.phi += angle
    1.54 +		else:
    1.55 +			self.turret.phi += max_turret_angle
    1.56 +
    1.57  
    1.58  	def accelerate(self, speed_delta):
    1.59 -		self.velocity.rho += speed_delta * delta_t
    1.60 +		self.velocity += self.velocity.normalize() * speed_delta * delta_t
    1.61 +		print self.velocity.rho
    1.62  		if self.velocity.rho > max_velocity:
    1.63  			self.velocity.rho = max_velocity
    1.64  
    1.65  	def fire(self):
    1.66 -		pass
    1.67 +		bullet_position = self.position + self.turret*(self.radius + 0.1) 
    1.68 +		bullet = Bullet(bullet_position, bullet_velocity, self)
    1.69 +		bodies.append(bullet)
    1.70  
    1.71  	def on_tick(self,other_tanks, bullets):
    1.72  		if self.user.base_left == True:
    1.73 -			self.rotate_base(delta_phi)
    1.74 +			self.rotate_base(base_angle)
    1.75  		if self.user.base_right == True:
    1.76 -			self.rotate_base(-1*delta_phi)
    1.77 +			self.rotate_base(-1*base_angle)
    1.78  		if self.user.accelerate == True:
    1.79  			self.accelerate(speed_delta)
    1.80 +		if self.user.decelerate == True:
    1.81 +			self.accelerate(-1*speed_delta)
    1.82 +		if self.user.turret_left == True:
    1.83 +			self.rotate_turret(turret_angle)
    1.84 +		if self.user.turret_right == True:
    1.85 +			self.rotate_turret(-1*turret_angle)
    1.86 +		if self.user.fire == True:
    1.87 +			self.fire()
    1.88  
    1.89  	def on_spawn(self):
    1.90  		pass
    1.91 +#		self.position.x = random.randint(1,width - 10)
    1.92 +#		self.position.y = random.randint(1,height - 10)
    1.93 +#		self.velocity = vector.null 
    1.94  
    1.95  	def on_death(self) :
    1.96  		pass
    1.97  	
    1.98 -	def on_hit(self,bullet):
    1.99 +	def on_hit(self, bullet):
   1.100  		pass
   1.101  
   1.102 -	def on_collision(self):
   1.103 +	def on_collision(self, other):
   1.104  		pass
   1.105  
   1.106  	def on_wall(self):
   1.107 -		pass
   1.108 +		self.next_position = self.position
   1.109  
   1.110  class Bullet(Body):
   1.111  	radius = 0.1
   1.112 -	pass
   1.113 \ No newline at end of file
   1.114 +	def __init__(self, position, velocity, tank):
   1.115 +		Body.__init__(self, position, velocity)
   1.116 +		self.tank = tank
   1.117 \ No newline at end of file
     2.1 --- a/game.py	Sun Dec 19 16:04:29 2010 +0300
     2.2 +++ b/game.py	Mon Dec 20 10:53:47 2010 +0300
     2.3 @@ -1,3 +1,5 @@
     2.4 +import vector
     2.5 +
     2.6  other_tanks = []
     2.7  bullets = []
     2.8  
     2.9 @@ -8,45 +10,62 @@
    2.10  		self.width = width
    2.11  		self.height = height
    2.12  
    2.13 -	def step(game):
    2.14 -		game.next_positions()
    2.15 -		game.check_collisions()
    2.16 -		game.check_walls()
    2.17 -		game.update_positions()
    2.18 -		game.invoke_ticks()
    2.19 -		game.respawn()
    2.20 +	def step(self):
    2.21 +		self.next_positions()
    2.22 +		self.check_collisions()
    2.23 +		self.check_walls()
    2.24 +		self.update_positions()
    2.25 +		self.invoke_ticks()
    2.26 +		self.respawn()
    2.27 +		for i in self.bodies:		#test
    2.28 +			print i.position	#test
    2.29  
    2.30 -	def next_positions(game):
    2.31 +	def next_positions(self):
    2.32  		delta_t = 1
    2.33 -		for i in game.bodies:
    2.34 +		for i in self.bodies:
    2.35  			i.next_position = i.position + i.velocity*(delta_t)
    2.36  	
    2.37  
    2.38 -	def check_collisions(game):
    2.39 -		pass
    2.40 +	def check_collisions(self):
    2.41 +		for i in self.bodies:
    2.42 +			for j in self.bodies:
    2.43 +				if self.collides(i,j) == True :
    2.44 +					self.handle_collision(i,j)			
    2.45  
    2.46  	def collides(self,body1,body2):
    2.47 -		pass
    2.48 +		if (abs(body1.position + body2.position*-1) <= (body1.radius + body2.radius)) and (body1 != body2) :
    2.49 +			print 'collision'
    2.50  
    2.51  	def handle_collision(self,body1,body2):
    2.52 -		pass
    2.53 +		if isinstance(body1, body.Tank) == True : 
    2.54 +			if isinstance(body2, body.Tank) == True :
    2.55 +				body1.on_collision(body2)
    2.56 +			else :
    2.57 +				body1.on_hit()
    2.58 +				body1.on_death()
    2.59 +		else :
    2.60 +			if isinstance(body2, body.Tank) == True :
    2.61 +				body2.on_hit()
    2.62 +				body2.on_death()
    2.63 +			else :
    2.64 +				pass # bullets disappear
    2.65  
    2.66 -	def check_walls(game):
    2.67 -		for i in game.bodies :
    2.68 -			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.69 +	def check_walls(self):
    2.70 +		for i in self.bodies :
    2.71 +			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) :
    2.72  				i.on_wall()
    2.73 -		
    2.74 +				print 'wall'      #test
    2.75  
    2.76 -	def update_positions(game):
    2.77 -		for i in game.bodies:
    2.78 +	def update_positions(self):
    2.79 +		for i in self.bodies:
    2.80  			i.position = i.next_position
    2.81  
    2.82 -	def invoke_ticks(game):
    2.83 -		for i in game.users:
    2.84 +	def invoke_ticks(self):
    2.85 +		for i in self.users:
    2.86  			i.tank.on_tick(other_tanks,bullets)
    2.87 -	
    2.88 -	def respawn(game):                     
    2.89 -		for i in game.users :
    2.90 +
    2.91 +	def respawn(self):                     
    2.92 +		for i in self.users :
    2.93  			if i.tank.strength == 0 :
    2.94  				i.tank.on_spawn()
    2.95  				i.tank.strength = 1 
     3.1 --- a/vector.py	Sun Dec 19 16:04:29 2010 +0300
     3.2 +++ b/vector.py	Mon Dec 20 10:53:47 2010 +0300
     3.3 @@ -33,6 +33,13 @@
     3.4  		else :
     3.5  			return 0
     3.6  
     3.7 +	def normalize(self):
     3.8 +		result = Vector()
     3.9 +		result.x = self.x
    3.10 +		result.y = self.y
    3.11 +		result.rho = 1
    3.12 +		return result
    3.13 +
    3.14  	def get_rho(self):
    3.15  		return abs(self)
    3.16