tanchiki
diff game.py @ 52:d740eff76e7e
some bugs corrected
author | Olga Zolotareva <olya_zol@inbox.ru> |
---|---|
date | Fri, 24 Dec 2010 22:10:08 +0300 |
parents | 1a0bddee3c54 |
children | 5450c97fbc38 |
line diff
1.1 --- a/game.py Mon Dec 20 21:27:23 2010 +0300 1.2 +++ b/game.py Fri Dec 24 22:10:08 2010 +0300 1.3 @@ -1,9 +1,12 @@ 1.4 import vector 1.5 import body 1.6 import time 1.7 +import random 1.8 1.9 other_tanks = [] 1.10 bullets = [] 1.11 +width = 500 1.12 +height = 500 1.13 1.14 class Game(object): 1.15 def __init__(self, bodies, users, width, height): 1.16 @@ -12,27 +15,38 @@ 1.17 self.width = width 1.18 self.height = height 1.19 1.20 - def step(self): 1.21 - for i in self.bodies: #test 1.22 - print "begin", i, i.position 1.23 + def step(self): 1.24 self.next_positions() 1.25 self.check_collisions() 1.26 self.check_walls() 1.27 self.update_positions() 1.28 self.invoke_ticks() 1.29 self.respawn() 1.30 + self.update_velocities() 1.31 + for i in self.bodies : 1.32 + print i 1.33 + # print 'next position' , i.next_position 1.34 + print 'update_position' , i.position 1.35 + print 'velocity' , i.velocity 1.36 + print 'velocity.rho', i.velocity.rho # test 1.37 + print '\n' 1.38 + if isinstance(i,body.Tank) == True: 1.39 + print 'base_orientation' , i.base_orientation 1.40 1.41 - for i in self.bodies: #test 1.42 - print "end", i, i.position #test 1.43 - # time.sleep(1) #test 1.44 - # self.step() #test 1.45 - 1.46 + def update_velocities(self): 1.47 + for i in self.bodies: 1.48 + if isinstance(i, body.Tank): 1.49 + if abs(i.velocity) > 1e-10 : 1.50 + i.velocity = i.velocity - i.velocity.normalize()*body.acceleration 1.51 + else : 1.52 + i.velocity.rho = 0 1.53 1.54 def next_positions(self): 1.55 delta_t = 1 1.56 for i in self.bodies: 1.57 - i.next_position = i.position + i.velocity*(delta_t) 1.58 - 1.59 + i.next_position = i.position + i.velocity*body.delta_t 1.60 + if isinstance(i, body.Bullet) == True: 1.61 + print 'Bullet', i 1.62 1.63 def check_collisions(self): 1.64 for i in self.bodies: 1.65 @@ -41,16 +55,13 @@ 1.66 self.handle_collision(i,j) 1.67 1.68 def collides(self,body1,body2): 1.69 - print body1, body2 1.70 - if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)): 1.71 - print 'collision' 1.72 - print body1.position , body2.position 1.73 - if (body1 != body2): 1.74 - return True 1.75 - else : 1.76 - return False 1.77 - else : 1.78 - return False 1.79 +# if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)): if (body1 != body2): 1.80 +# print 'collision' 1.81 +# return True 1.82 +# else : 1.83 +# return False 1.84 +# else : 1.85 + return False 1.86 1.87 def handle_collision(self,body1,body2): 1.88 if isinstance(body1, body.Tank) == True : 1.89 @@ -84,10 +95,13 @@ 1.90 for i in self.users : 1.91 if i.tank.strength == 0 : 1.92 i.tank.on_spawn() 1.93 + print 'respawn' 1.94 + print i.tank.strength , i.tank.position 1.95 i.tank.strength = 1 1.96 i.tank.velocity = vector.null 1.97 - i.tank.position.x = random.randint(self.radius , width - self.radius) 1.98 - i.tank.position.y = random.randint(self.radius , height - self.radius) 1.99 + i.tank.position.x = random.randint(i.tank.radius , width - i.tank.radius) 1.100 + i.tank.position.y = random.randint(i.tank.radius , height - i.tank.radius) 1.101 i.tank.velocity = vector.null 1.102 + print i.tank.strength , i.tank.position 1.103 1.104 1.105 \ No newline at end of file