tanchiki
changeset 54:5cb3fef573f6 new tip
UI for two users
author | Olga Zolotareva <olya_zol@inbox.ru> |
---|---|
date | Sat, 25 Dec 2010 01:51:31 +0300 |
parents | 5450c97fbc38 |
children | |
files | body.py game.py tk_ui.py user.py |
diffstat | 4 files changed, 50 insertions(+), 26 deletions(-) [+] |
line diff
1.1 --- a/body.py Fri Dec 24 23:28:21 2010 +0300 1.2 +++ b/body.py Sat Dec 25 01:51:31 2010 +0300 1.3 @@ -52,7 +52,6 @@ 1.4 1.5 1.6 def accelerate(self, speed_delta): 1.7 - print 'speed_delta', speed_delta #test 1.8 self.velocity += self.base_orientation*speed_delta*delta_t 1.9 if abs(self.velocity) > max_velocity: 1.10 self.velocity.rho = max_velocity 1.11 @@ -77,6 +76,7 @@ 1.12 1.13 def on_collision(self, other): 1.14 pass 1.15 + # self.velocity , other.velocity = self.velocity + other.velocity , other.velocity + self.velocity 1.16 1.17 def on_wall(self): 1.18 self.velocity = vector.Vector(0,0)
2.1 --- a/game.py Fri Dec 24 23:28:21 2010 +0300 2.2 +++ b/game.py Sat Dec 25 01:51:31 2010 +0300 2.3 @@ -23,15 +23,15 @@ 2.4 self.invoke_ticks() 2.5 self.respawn() 2.6 self.update_velocities() 2.7 - for i in self.bodies : 2.8 - print i 2.9 - # print 'next position' , i.next_position 2.10 - print 'update_position' , i.position 2.11 - print 'velocity' , i.velocity 2.12 - print 'velocity.rho', i.velocity.rho # test 2.13 - if isinstance(i,body.Tank) == True: 2.14 - print 'base_orientation' , i.base_orientation 2.15 - print '\n' 2.16 + # for i in self.bodies : 2.17 + # print i # test 2.18 + # print 'next position' , i.next_position # test 2.19 + # print 'update_position' , i.position # test 2.20 + # print 'velocity' , i.velocity # test 2.21 + # print 'velocity.rho', i.velocity.rho # test 2.22 + # if isinstance(i,body.Tank) == True: # test 2.23 + # print 'base_orientation' , i.base_orientation # test 2.24 + # print '\n' 2.25 2.26 def update_velocities(self): 2.27 for i in self.bodies: 2.28 @@ -55,7 +55,6 @@ 2.29 def collides(self,body1,body2): 2.30 if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)): 2.31 if (body1 != body2): 2.32 - print 'collision' 2.33 return True 2.34 else : 2.35 return False 2.36 @@ -67,12 +66,14 @@ 2.37 if isinstance(body2, body.Tank) == True : 2.38 body1.on_collision(body2) 2.39 else : 2.40 - body1.on_hit() 2.41 + body1.on_hit(body2) 2.42 body1.on_death() 2.43 + self.bodies.remove(body2) 2.44 else : 2.45 if isinstance(body2, body.Tank) == True : 2.46 - body2.on_hit() 2.47 + body2.on_hit(body2) 2.48 body2.on_death() 2.49 + self.bodies.remove(body1) 2.50 2.51 def check_walls(self): 2.52 for i in self.bodies : 2.53 @@ -93,13 +94,10 @@ 2.54 for i in self.users : 2.55 if i.tank.strength == 0 : 2.56 i.tank.on_spawn() 2.57 - print 'respawn' 2.58 - print i.tank.strength , i.tank.position 2.59 i.tank.strength = 1 2.60 i.tank.velocity = vector.null 2.61 i.tank.position.x = random.randint(i.tank.radius , width - i.tank.radius) 2.62 i.tank.position.y = random.randint(i.tank.radius , height - i.tank.radius) 2.63 i.tank.velocity = vector.null 2.64 - print i.tank.strength , i.tank.position 2.65 2.66 2.67 \ No newline at end of file
3.1 --- a/tk_ui.py Fri Dec 24 23:28:21 2010 +0300 3.2 +++ b/tk_ui.py Sat Dec 25 01:51:31 2010 +0300 3.3 @@ -8,7 +8,7 @@ 3.4 from vector import Vector 3.5 3.6 game_size = 500, 500 3.7 -keys = { 3.8 +keys1 = { 3.9 'ocircumflex': 'base_left', 3.10 'acircumflex': 'base_right', 3.11 'eacute': 'turret_left', 3.12 @@ -17,14 +17,36 @@ 3.13 'ucircumflex': 'decelerate', 3.14 'division': 'fire', 3.15 } 3.16 + 3.17 +keys2 = { 3.18 + 'icircumflex': 'base_left', 3.19 + 'adiaeresis': 'base_right', 3.20 + 'atilde': 'turret_left', 3.21 + 'ugrave': 'turret_right', 3.22 + 'oslash': 'accelerate', 3.23 + 'ediaeresis': 'decelerate', 3.24 + 'udiaeresis': 'fire', 3.25 +} 3.26 3.27 welcome = """Press F5 to start 3.28 3.29 Keys are: 3.30 + 3.31 + 3.32 +User 1 : 3.33 + 3.34 a, d -- turn tank 3.35 q, e -- turn muzzle 3.36 w, s -- change speed 3.37 -x -- fire""" 3.38 +x -- fire 3.39 + 3.40 +User 2 : 3.41 + 3.42 +j, l -- turn tank 3.43 +u, o -- turn muzzle 3.44 +i, k -- change speed 3.45 +m -- fire 3.46 +""" 3.47 3.48 class Tank(BaseTank): 3.49 3.50 @@ -52,11 +74,14 @@ 3.51 self.display_welcome() 3.52 3.53 def init_game(self): 3.54 - self.user = User(keys) 3.55 + self.user1 = User(keys1) 3.56 + self.user2 = User(keys2) 3.57 w, h = game_size 3.58 - game = self.game = Game([], [self.user], w, h) 3.59 - tank = Tank(Vector(*game_size) * 0.5, self.user, self.game) 3.60 - game.bodies.append(tank) 3.61 + game = self.game = Game([], [self.user1, self.user2], w, h) 3.62 + tank1 = Tank(Vector(*game_size) * 0.5, self.user1, self.game) 3.63 + tank2 = Tank(Vector(*game_size) * 0.5, self.user2, self.game) 3.64 + game.bodies.append(tank1) 3.65 + game.bodies.append(tank2) 3.66 3.67 def init_ui(self): 3.68 root = self.root = tk.Tk() 3.69 @@ -78,10 +103,12 @@ 3.70 def on_keypress(self, ev): 3.71 if ev.keysym == "F5": 3.72 self.step() 3.73 - self.user.on_key(ev.keysym, True) 3.74 + self.user1.on_key(ev.keysym, True) 3.75 + self.user2.on_key(ev.keysym, True) 3.76 3.77 def on_keyrelease(self, ev): 3.78 - self.user.on_key(ev.keysym, False) 3.79 + self.user1.on_key(ev.keysym, False) 3.80 + self.user2.on_key(ev.keysym, False) 3.81 3.82 def step(self): 3.83 self.root.after(100, self.step) 3.84 @@ -114,4 +141,4 @@ 3.85 raise AssertionError("Unknown object type: %s" % i.__class__.__name__) 3.86 3.87 if __name__ == "__main__": 3.88 - UI().root.mainloop() 3.89 + UI().root.mainloop() 3.90 \ No newline at end of file
4.1 --- a/user.py Fri Dec 24 23:28:21 2010 +0300 4.2 +++ b/user.py Sat Dec 25 01:51:31 2010 +0300 4.3 @@ -17,7 +17,6 @@ 4.4 return 4.5 else: 4.6 action = self.keyset[key] 4.7 - print 'action=', action 4.8 if hasattr(self, action) and action not in ['tank', 'keyset']: 4.9 setattr(self, action, value) 4.10