Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/tanchiki/file/dba440ba9a00/game.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 10:40:05 2013
Кодировка:
tanchiki: dba440ba9a00 game.py

tanchiki

view game.py @ 46:dba440ba9a00

Bugfixed: constant names for tank movement; added the tank to the game
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 20:37:54 +0300
parents b7a85caedc7f dfdc1def5d24
children d740eff76e7e
line source
1 import vector
2 import body
3 import time
5 other_tanks = []
6 bullets = []
8 class Game(object):
9 def __init__(self, bodies, users, width, height):
10 self.bodies = bodies
11 self.users = users
12 self.width = width
13 self.height = height
15 def step(self):
16 for i in self.bodies: #test
17 print "begin", i, i.position
18 self.next_positions()
19 self.check_collisions()
20 self.check_walls()
21 self.update_positions()
22 self.invoke_ticks()
23 self.respawn()
25 for i in self.bodies: #test
26 print "end", i, i.position #test
27 # time.sleep(1) #test
28 # self.step() #test
31 def next_positions(self):
32 delta_t = 1
33 for i in self.bodies:
34 i.next_position = i.position + i.velocity*(delta_t)
37 def check_collisions(self):
38 for i in self.bodies:
39 for j in self.bodies:
40 if self.collides(i,j) == True :
41 self.handle_collision(i,j)
43 def collides(self,body1,body2):
44 print body1, body2
45 if (abs(body1.next_position - body2.next_position) <= (body1.radius + body2.radius)):
46 print 'collision'
47 print body1.position , body2.position
48 if (body1 != body2):
49 return True
50 else :
51 return False
52 else :
53 return False
55 def handle_collision(self,body1,body2):
56 if isinstance(body1, body.Tank) == True :
57 if isinstance(body2, body.Tank) == True :
58 body1.on_collision(body2)
59 else :
60 body1.on_hit()
61 body1.on_death()
62 else :
63 if isinstance(body2, body.Tank) == True :
64 body2.on_hit()
65 body2.on_death()
67 def check_walls(self):
68 for i in self.bodies :
69 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) :
70 i.on_wall()
71 print 'wall' #test
73 def update_positions(self):
74 for i in self.bodies:
75 i.position = i.next_position
77 def invoke_ticks(self):
78 for i in self.bodies :
79 if isinstance(i, body.Tank) :
80 i.on_tick([], [])
83 def respawn(self):
84 for i in self.users :
85 if i.tank.strength == 0 :
86 i.tank.on_spawn()
87 i.tank.strength = 1
88 i.tank.velocity = vector.null
89 i.tank.position.x = random.randint(self.radius , width - self.radius)
90 i.tank.position.y = random.randint(self.radius , height - self.radius)
91 i.tank.velocity = vector.null