tanchiki
diff game.py @ 35:dfdc1def5d24
modules changed
author | Olga Zolotareva <olya_zol@inbox.ru> |
---|---|
date | Mon, 20 Dec 2010 18:42:45 +0300 |
parents | c71c27b09bc7 |
children | 1a0bddee3c54 |
line diff
1.1 --- a/game.py Mon Dec 20 16:05:49 2010 +0300 1.2 +++ b/game.py Mon Dec 20 18:42:45 2010 +0300 1.3 @@ -1,50 +1,93 @@ 1.4 -other_tanks = [] 1.5 -bullets = [] 1.6 - 1.7 -class Game(object): 1.8 - def __init__(self, bodies, users, width, height): 1.9 - self.bodies = bodies 1.10 - self.users = users 1.11 - self.width = width 1.12 - self.height = height 1.13 - 1.14 - def step(game): 1.15 - game.next_positions() 1.16 - game.check_collisions() 1.17 - game.check_walls() 1.18 - game.update_positions() 1.19 - game.invoke_ticks() 1.20 - game.respawn() 1.21 - 1.22 - def next_positions(game): 1.23 - delta_t = 1 1.24 - for i in game.bodies : 1.25 - i.next_position = i.position + i.velocity*(delta_t) 1.26 - 1.27 - def check_collisions(game): 1.28 - pass 1.29 - 1.30 - def collides(self,body1,body2): 1.31 - pass 1.32 - 1.33 - def handle_collision(self,body1,body2): 1.34 - pass 1.35 - 1.36 - def check_walls(game): 1.37 - for i in game.bodies : 1.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) : 1.39 - i.on_wall() 1.40 - 1.41 - def update_positions(game): 1.42 - for i in game.bodies : 1.43 - i.position = i.next_position 1.44 - 1.45 - def invoke_ticks(game): 1.46 - for i in game.users : 1.47 - i.tank.on_tick(other_tanks,bullets) 1.48 - 1.49 - def respawn(game): 1.50 - for i in game.users : 1.51 - if i.tank.strength == 0 : 1.52 - i.tank.on_spawn() 1.53 - i.tank.strength = 1 1.54 +import vector 1.55 +import body 1.56 +import time 1.57 + 1.58 +other_tanks = [] 1.59 +bullets = [] 1.60 + 1.61 +class Game(object): 1.62 + def __init__(self, bodies, users, width, height): 1.63 + self.bodies = bodies 1.64 + self.users = users 1.65 + self.width = width 1.66 + self.height = height 1.67 + 1.68 + def step(self): 1.69 + for i in self.bodies: #test 1.70 + print "begin", i, i.position 1.71 + self.next_positions() 1.72 + self.check_collisions() 1.73 + self.check_walls() 1.74 + self.update_positions() 1.75 + self.invoke_ticks() 1.76 + self.respawn() 1.77 + 1.78 + for i in self.bodies: #test 1.79 + print "end", i, i.position #test 1.80 + # time.sleep(1) #test 1.81 + # self.step() #test 1.82 + 1.83 + 1.84 + def next_positions(self): 1.85 + delta_t = 1 1.86 + for i in self.bodies: 1.87 + i.next_position = i.position + i.velocity*(delta_t) 1.88 + 1.89 + 1.90 + def check_collisions(self): 1.91 + for i in self.bodies: 1.92 + for j in self.bodies: 1.93 + if self.collides(i,j) == True : 1.94 + self.handle_collision(i,j) 1.95 + 1.96 + def collides(self,body1,body2): 1.97 + print body1, body2 1.98 + if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)): 1.99 + print 'collision' 1.100 + print body1.position , body2.position 1.101 + if (body1 != body2): 1.102 + return True 1.103 + else : 1.104 + return False 1.105 + else : 1.106 + return False 1.107 + 1.108 + def handle_collision(self,body1,body2): 1.109 + if isinstance(body1, body.Tank) == True : 1.110 + if isinstance(body2, body.Tank) == True : 1.111 + body1.on_collision(body2) 1.112 + else : 1.113 + body1.on_hit() 1.114 + body1.on_death() 1.115 + else : 1.116 + if isinstance(body2, body.Tank) == True : 1.117 + body2.on_hit() 1.118 + body2.on_death() 1.119 + 1.120 + def check_walls(self): 1.121 + for i in self.bodies : 1.122 + 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) : 1.123 + i.on_wall() 1.124 + print 'wall' #test 1.125 + 1.126 + def update_positions(self): 1.127 + for i in self.bodies: 1.128 + i.position = i.next_position 1.129 + 1.130 + def invoke_ticks(self): 1.131 + for i in self.bodies : 1.132 + if isinstance(i, body.Tank) : 1.133 + i.on_tick([], []) 1.134 + 1.135 + 1.136 + def respawn(self): 1.137 + for i in self.users : 1.138 + if i.tank.strength == 0 : 1.139 + i.tank.on_spawn() 1.140 + i.tank.strength = 1 1.141 + i.tank.velocity = vector.null 1.142 + i.tank.position.x = random.randint(self.radius , width - self.radius) 1.143 + i.tank.position.y = random.randint(self.radius , height - self.radius) 1.144 + i.tank.velocity = vector.null 1.145 + 1.146 + 1.147 \ No newline at end of file