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

tanchiki

changeset 52:d740eff76e7e new

some bugs corrected
author Olga Zolotareva <olya_zol@inbox.ru>
date Fri, 24 Dec 2010 22:10:08 +0300
parents f6d3c0539e8c
children 5450c97fbc38
files body.py game.py tk_ui.py user.py vector.py
diffstat 5 files changed, 96 insertions(+), 65 deletions(-) [+]
line diff
     1.1 --- a/body.py	Mon Dec 20 21:27:23 2010 +0300
     1.2 +++ b/body.py	Fri Dec 24 22:10:08 2010 +0300
     1.3 @@ -4,18 +4,20 @@
     1.4  
     1.5  base_angle = math.pi/32   # deltha phi = math.pi/32
     1.6  turret_angle = math.pi/32
     1.7 -speed_delta = 1
     1.8 +speed_delta = 2
     1.9  delta_t = 1
    1.10  max_velocity = 4
    1.11 +acceleration = 1
    1.12  max_base_angle = 1
    1.13  max_turret_angle = 1
    1.14  initial_strength = 1
    1.15  bullet_velocity = 10
    1.16 -width = 100
    1.17 -height = 100
    1.18 +width = 500
    1.19 +height = 500
    1.20 +bullet_speed = 10
    1.21  
    1.22  class Body(object):
    1.23 -	def __init__(self, position, velocity = vector.null):
    1.24 +	def __init__(self, position = vector.null, velocity = vector.null):
    1.25  		self.position = position
    1.26  		self.velocity = velocity
    1.27  
    1.28 @@ -24,18 +26,23 @@
    1.29  	def __init__(self, position, user, game):
    1.30  		Body.__init__(self, position)
    1.31  		self.game = game
    1.32 -		self.turret = vector.i
    1.33 +		self.turret = vector.Vector(0,-1)
    1.34  		self.strength = 0
    1.35 -		self.base_orientation = 1	# 1 or -1
    1.36 +		self.base_orientation = vector.Vector(0,-1)	# Vector
    1.37  		self.user = user
    1.38  		user.tank = self 
    1.39  
    1.40  
    1.41 -	def rotate_base(tank, angle):
    1.42 +	def rotate_base(self, angle):
    1.43  		if abs(angle) < max_base_angle:  
    1.44  			self.velocity.phi += angle
    1.45 +			self.base_orientation.phi +=angle
    1.46 +			self.turret.phi += angle
    1.47 +
    1.48  		else:
    1.49  			self.velocity.phi += max_base_angle
    1.50 +			self.base_orientation.phi += max_base_angle
    1.51 +			self.turret.phi += max_turret_angle
    1.52  
    1.53  	def rotate_turret(self, angle):
    1.54  		if abs(angle) < max_base_angle:  
    1.55 @@ -45,14 +52,15 @@
    1.56  
    1.57  
    1.58  	def accelerate(self, speed_delta):
    1.59 -		self.velocity += self.velocity.normalize() * speed_delta * delta_t
    1.60 -		print self.velocity.rho
    1.61 -		if self.velocity.rho > max_velocity:
    1.62 +		print 'speed_delta', speed_delta	#test
    1.63 +		self.velocity += self.base_orientation*speed_delta*delta_t
    1.64 +		if abs(self.velocity) > max_velocity:
    1.65  			self.velocity.rho = max_velocity
    1.66  
    1.67  	def fire(self):
    1.68 +		print 'fire!'
    1.69                  bullet_position = self.position + self.turret * (self.radius + 0.1)
    1.70 -		bullet_velocity = self.turret.normalize() * self.game.bullet_speed
    1.71 +		bullet_velocity = self.turret.normalize() * bullet_speed
    1.72  		bullet = Bullet(bullet_position, bullet_velocity, self)
    1.73  		self.game.bodies.append(bullet)
    1.74  
    1.75 @@ -77,5 +85,9 @@
    1.76  class Bullet(Body):
    1.77  	radius = 0.1
    1.78  	def __init__(self, position, velocity, tank):
    1.79 -		Body.__init__(self, position, velocity = bullet_velocity)
    1.80  		self.tank = tank
    1.81 +		Body.__init__(self, position, velocity = self.tank.turret*bullet_velocity)
    1.82 +
    1.83 +	def on_wall(self):
    1.84 +		pass
    1.85 +
     2.1 --- a/game.py	Mon Dec 20 21:27:23 2010 +0300
     2.2 +++ b/game.py	Fri Dec 24 22:10:08 2010 +0300
     2.3 @@ -1,9 +1,12 @@
     2.4  import vector
     2.5  import body
     2.6  import time
     2.7 +import random
     2.8  
     2.9  other_tanks = []
    2.10  bullets = []
    2.11 +width = 500
    2.12 +height = 500
    2.13  
    2.14  class Game(object):
    2.15  	def __init__(self, bodies, users, width, height):
    2.16 @@ -12,27 +15,38 @@
    2.17  		self.width = width
    2.18  		self.height = height	 
    2.19  
    2.20 -	def step(self):
    2.21 -		for i in self.bodies:		#test
    2.22 -			print "begin", i, i.position
    2.23 +	def step(self):                                  
    2.24  		self.next_positions()
    2.25  		self.check_collisions()
    2.26  		self.check_walls()
    2.27  		self.update_positions()
    2.28  		self.invoke_ticks()
    2.29  		self.respawn()
    2.30 +		self.update_velocities()
    2.31 +		for i in self.bodies :
    2.32 +			print i
    2.33 +		#	print 'next position' , i.next_position 
    2.34 +			print 'update_position' ,  i.position
    2.35 +			print 'velocity'  , i.velocity
    2.36 +			print 'velocity.rho', i.velocity.rho 	# test
    2.37 +			print '\n'
    2.38 +			if isinstance(i,body.Tank) == True:
    2.39 +				print 'base_orientation' , i.base_orientation
    2.40  
    2.41 -		for i in self.bodies:		#test
    2.42 -			print "end", i, i.position	#test
    2.43 -	#	time.sleep(1)                   #test
    2.44 -	#	self.step()                     #test
    2.45 -		
    2.46 +	def update_velocities(self):
    2.47 +		for i in self.bodies:
    2.48 +			if isinstance(i, body.Tank):
    2.49 +				if abs(i.velocity) > 1e-10 : 
    2.50 +					i.velocity = i.velocity - i.velocity.normalize()*body.acceleration
    2.51 +				else :
    2.52 +					i.velocity.rho = 0
    2.53  
    2.54  	def next_positions(self):
    2.55  		delta_t = 1
    2.56  		for i in self.bodies:
    2.57 -			i.next_position = i.position + i.velocity*(delta_t)
    2.58 -	
    2.59 +				i.next_position = i.position + i.velocity*body.delta_t
    2.60 +				if isinstance(i, body.Bullet) == True:
    2.61 +					print 'Bullet', i
    2.62  
    2.63  	def check_collisions(self):
    2.64  		for i in self.bodies:
    2.65 @@ -41,16 +55,13 @@
    2.66  					self.handle_collision(i,j)			
    2.67  
    2.68  	def collides(self,body1,body2):
    2.69 -		print body1, body2
    2.70 -		if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)):
    2.71 -			print 'collision'
    2.72 -			print body1.position , body2.position 
    2.73 -			if (body1 != body2):
    2.74 -				return True
    2.75 -			else :
    2.76 -				return False
    2.77 -		else :
    2.78 -			return False
    2.79 +#		if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)):			if (body1 != body2):
    2.80 +#				print 'collision' 
    2.81 +#				return True
    2.82 +#			else :
    2.83 +#				return False
    2.84 +#		else :
    2.85 +		return False
    2.86  
    2.87  	def handle_collision(self,body1,body2):
    2.88  		if isinstance(body1, body.Tank) == True : 
    2.89 @@ -84,10 +95,13 @@
    2.90  		for i in self.users :
    2.91  			if i.tank.strength == 0 :
    2.92  				i.tank.on_spawn()
    2.93 +				print 'respawn'
    2.94 +				print i.tank.strength , i.tank.position
    2.95  				i.tank.strength = 1
    2.96  				i.tank.velocity = vector.null
    2.97 -				i.tank.position.x = random.randint(self.radius , width - self.radius)
    2.98 -				i.tank.position.y = random.randint(self.radius , height - self.radius)
    2.99 +				i.tank.position.x = random.randint(i.tank.radius , width - i.tank.radius)
   2.100 +				i.tank.position.y = random.randint(i.tank.radius , height - i.tank.radius)
   2.101  				i.tank.velocity = vector.null 
   2.102 +                                print i.tank.strength , i.tank.position
   2.103  
   2.104   
   2.105 \ No newline at end of file
     3.1 --- a/tk_ui.py	Mon Dec 20 21:27:23 2010 +0300
     3.2 +++ b/tk_ui.py	Fri Dec 24 22:10:08 2010 +0300
     3.3 @@ -9,13 +9,13 @@
     3.4  
     3.5  game_size = 500, 500
     3.6  keys = {
     3.7 -	'a': 'base_left',
     3.8 -	'd': 'base_right',
     3.9 -	'q': 'turret_left',
    3.10 -	'e': 'turret_right',
    3.11 -	'w': 'accelerate',
    3.12 -	's': 'decelerate',
    3.13 -	'x': 'fire',
    3.14 +	'ocircumflex': 'base_left', 
    3.15 +	'acircumflex': 'base_right',
    3.16 +	'eacute': 'turret_left',
    3.17 +	'oacute': 'turret_right',
    3.18 +	'odiaeresis': 'accelerate',
    3.19 +	'ucircumflex': 'decelerate',
    3.20 +	'division': 'fire',
    3.21  }
    3.22  
    3.23  welcome = """Press F5 to start
    3.24 @@ -30,17 +30,17 @@
    3.25  
    3.26  	def on_tick(self, tanks, bullets):
    3.27  		if self.user.base_left:
    3.28 -			self.rotate_base(-body.base_angle)
    3.29 +			self.rotate_base(-1 * body.base_angle)
    3.30  		if self.user.base_right:
    3.31  			self.rotate_base(body.base_angle)
    3.32  		if self.user.turret_left:
    3.33 -			self.rotate_turret(-body.turret_angle)
    3.34 +			self.rotate_turret(-1 * body.turret_angle)
    3.35  		if self.user.turret_right:
    3.36  			self.rotate_turret(body.turret_angle)
    3.37  		if self.user.accelerate:
    3.38  			self.accelerate(body.speed_delta)
    3.39  		if self.user.decelerate:
    3.40 -			self.accelerate(-body.speed_delta)
    3.41 +			self.accelerate(-1 * body.speed_delta)
    3.42  		if self.user.fire:
    3.43  			self.fire()
    3.44  
    3.45 @@ -53,7 +53,7 @@
    3.46  
    3.47  	def init_game(self):
    3.48  		self.user = User(keys)
    3.49 -		w, h = game_size
    3.50 +		w, h = game_size 
    3.51  		game = self.game = Game([], [self.user], w, h)
    3.52  		tank = Tank(Vector(*game_size) * 0.5, self.user, self.game)
    3.53  		game.bodies.append(tank)
    3.54 @@ -90,25 +90,28 @@
    3.55  
    3.56  	def redraw(self):
    3.57  		self.canvas.delete("all")
    3.58 -		for body in self.game.bodies:
    3.59 -			p = body.position
    3.60 -			lt = p + Vector(-1,-1) * body.radius
    3.61 -			rb = p + Vector(1,1) * body.radius
    3.62 +		for i in self.game.bodies:
    3.63 +			p = i.position
    3.64 +			lt = p + Vector(-1,-1) * i.radius
    3.65 +			rb = p + Vector(1,1) * i.radius
    3.66  
    3.67 -			v1 = Vector(body.radius, 0)
    3.68 -			v1.phi = body.velocity.phi
    3.69 +			v1 = Vector(i.radius, 0)
    3.70 +			if isinstance(i, body.Tank):
    3.71 +				v1.phi = i.base_orientation.phi
    3.72 +			else :
    3.73 +				v1.phi = i.velocity.phi
    3.74  			vb = p - v1
    3.75  			ve = p + v1 * 2
    3.76 -			self.canvas.create_line(vb.x, vb.y, ve.x, ve.y, fill="darkgray", width=body.radius*2)
    3.77 +			self.canvas.create_line(vb.x, vb.y, ve.x, ve.y, fill="darkgray", width=i.radius*2)
    3.78  
    3.79 -			if isinstance(body, BaseTank):
    3.80 -				t = body.turret * body.radius * 1.5 + p
    3.81 +			if isinstance(i, BaseTank):
    3.82 +				t = i.turret * i.radius * 1.5 + p
    3.83  				self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="darkgreen")
    3.84  				self.canvas.create_line(p.x, p.y, t.x, t.y, fill="orange", width=2)
    3.85 -			elif isinstance(body, Bullet):
    3.86 +			elif isinstance(i, Bullet):
    3.87  				self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, outline="red")
    3.88  			else:
    3.89 -				raise AssertionError("Unknown object type: %s" % body.__class__.__name__)
    3.90 +				raise AssertionError("Unknown object type: %s" % i.__class__.__name__)
    3.91  
    3.92  if __name__ == "__main__":
    3.93  	UI().root.mainloop()
     4.1 --- a/user.py	Mon Dec 20 21:27:23 2010 +0300
     4.2 +++ b/user.py	Fri Dec 24 22:10:08 2010 +0300
     4.3 @@ -11,11 +11,13 @@
     4.4  		self.accelerate = False
     4.5  		self.decelerate = False
     4.6  		self.fire = False
     4.7 +		
     4.8 +	def on_key(self, key, value):		
     4.9 +		if key not in self.keyset:     
    4.10 +			return
    4.11 +		else:
    4.12 +			action = self.keyset[key]
    4.13 +			print 'action=', action
    4.14 +			if hasattr(self, action) and action not in ['tank', 'keyset']:
    4.15 +				setattr(self, action, value)
    4.16  
    4.17 -	def on_key(self, key, value):
    4.18 -		if key not in self.keyset:
    4.19 -			return
    4.20 -		action = self.keyset[key]
    4.21 -		if hasattr(self, action) and action not in ['tank', 'keyset']:
    4.22 -			setattr(self, action, value)
    4.23 -
     5.1 --- a/vector.py	Mon Dec 20 21:27:23 2010 +0300
     5.2 +++ b/vector.py	Fri Dec 24 22:10:08 2010 +0300
     5.3 @@ -7,13 +7,13 @@
     5.4  		self.y = y
     5.5  	
     5.6  	def __add__(self, other):
     5.7 -		result = Vector(0,0)
     5.8 +		result = Vector()
     5.9  		result.x = self.x + other.x
    5.10  		result.y = self.y + other.y
    5.11  		return result
    5.12  
    5.13  	def __sub__(self, other):
    5.14 -		result = Vector(0,0)
    5.15 +		result = Vector()
    5.16  		result.x = self.x - other.x
    5.17  		result.y = self.y - other.y
    5.18  		return result