Changeset 15:43f5b82f3491
Legend:
- Unmodified
- Added
- Removed
-
body.py
r13 r15 ˆà 1 1 import vectorˆà 2 2 import mathˆà 3 ˆà delta_phi = math.pi # deltha phi = math.piˆà ˆà 3 import randomˆà ˆà 4 ˆà ˆà 5 base_angle = math.pi/32 # deltha phi = math.pi/32ˆà ˆà 6 turret_angle = math.pi/32ˆà 4 7 speed_delta = 1ˆà 5 8 delta_t = 1ˆà 6 9 max_velocity = 2ˆà ˆà 10 max_base_angle = 1ˆà ˆà 11 max_turret_angle = 1ˆà 7 12 initial_strength = 1ˆà ˆà 13 bullet_velocity = 5ˆà ˆà 14 width = 10ˆà ˆà 15 height = 10ˆà 8 16 ˆà 9 17 class Body(object):ˆà òÀæ òÀæ ˆà 11 19 self.position = positionˆà 12 20 self.velocity = velocityˆà 13 ˆà self.radius = radiusˆà14 ˆà 15 21 ˆà 16 22 class Tank(Body):ˆà 17 ˆà radius = 1ˆàˆà 23 radius = 0.5ˆà 18 24 def __init__(self, position, user):ˆà 19 25 Body.__init__(self, position)ˆà ˆà 26 self.turret = vector.iˆà 20 27 self.strength = 0ˆà 21 ˆà self.turret = vector.iˆà22 28 self.base_orientation = 1 # 1 or -1ˆà 23 29 self.user = userˆà 24 ˆà user.tank = self # †ä†î†á†à†â†ë†ƒ†å†’ †‘†å†á†ƒ †â Userˆàˆà 30 user.tank = self ˆà 25 31 ˆà 26 32 ˆà 27 ˆà def rotate_base(tank, angle): ˆà 28 ˆà self.velocity.phi += angleˆà ˆà 33 def rotate_base(tank, angle):ˆà ˆà 34 if abs(angle) < max_base_angle: ˆà ˆà 35 self.velocity.phi += angleˆà ˆà 36 else:ˆà ˆà 37 self.velocity.phi += max_base_angleˆà 29 38 ˆà 30 39 def rotate_turret(self, angle):ˆà 31 ˆà self.turret.phi += angleˆà ˆà 40 if abs(angle) < max_base_angle: ˆà ˆà 41 self.turret.phi += angleˆà ˆà 42 else:ˆà ˆà 43 self.turret.phi += max_turret_angleˆà ˆà 44 ˆà 32 45 ˆà 33 46 def accelerate(self, speed_delta):ˆà 34 ˆà self.velocity.rho += speed_delta * delta_tˆà ˆà 47 self.velocity += self.velocity.normalize() * speed_delta * delta_tˆà ˆà 48 print self.velocity.rhoˆà 35 49 if self.velocity.rho > max_velocity:ˆà 36 50 self.velocity.rho = max_velocityˆà 37 51 ˆà 38 52 def fire(self):ˆà 39 ˆà passˆà ˆà 53 bullet_position = self.position + self.turret*(self.radius + 0.1) ˆà ˆà 54 bullet = Bullet(bullet_position, bullet_velocity, self)ˆà ˆà 55 bodies.append(bullet)ˆà 40 56 ˆà 41 57 def on_tick(self,other_tanks, bullets):ˆà 42 58 if self.user.base_left == True:ˆà 43 ˆà self.rotate_base( delta_phi)ˆàˆà 59 self.rotate_base(base_angle)ˆà 44 60 if self.user.base_right == True:ˆà 45 ˆà self.rotate_base(-1* delta_phi)ˆàˆà 61 self.rotate_base(-1*base_angle)ˆà 46 62 if self.user.accelerate == True:ˆà 47 63 self.accelerate(speed_delta)ˆà ˆà 64 if self.user.decelerate == True:ˆà ˆà 65 self.accelerate(-1*speed_delta)ˆà ˆà 66 if self.user.turret_left == True:ˆà ˆà 67 self.rotate_turret(turret_angle)ˆà ˆà 68 if self.user.turret_right == True:ˆà ˆà 69 self.rotate_turret(-1*turret_angle)ˆà ˆà 70 if self.user.fire == True:ˆà ˆà 71 self.fire()ˆà 48 72 ˆà 49 73 def on_spawn(self):ˆà 50 74 passˆà ˆà 75 # self.position.x = random.randint(1,width - 10)ˆà ˆà 76 # self.position.y = random.randint(1,height - 10)ˆà ˆà 77 # self.velocity = vector.null ˆà 51 78 ˆà 52 79 def on_death(self) :ˆà 53 80 passˆà 54 81 ˆà 55 ˆà def on_hit(self, bullet):ˆàˆà 82 def on_hit(self, bullet):ˆà 56 83 passˆà 57 84 ˆà 58 ˆà def on_collision(self ):ˆàˆà 85 def on_collision(self, other):ˆà 59 86 passˆà 60 87 ˆà 61 88 def on_wall(self):ˆà 62 ˆà passˆàˆà 89 self.next_position = self.positionˆà 63 90 ˆà 64 91 class Bullet(Body):ˆà 65 92 radius = 0.1ˆà 66 ˆà passˆà ˆà 93 def __init__(self, position, velocity, tank):ˆà ˆà 94 Body.__init__(self, position, velocity)ˆà ˆà 95 self.tank = tankˆà -
game.py
r8 r15 ˆà ˆà 1 import vectorˆà ˆà 2 ˆà 1 3 other_tanks = []ˆà 2 4 bullets = []ˆà òÀæ òÀæ ˆà 9 11 self.height = heightˆà 10 12 ˆà 11 ˆà def step(game):ˆà 12 ˆà game.next_positions()ˆà 13 ˆà game.check_collisions()ˆà 14 ˆà game.check_walls()ˆà 15 ˆà game.update_positions()ˆà 16 ˆà game.invoke_ticks()ˆà 17 ˆà game.respawn()ˆà ˆà 13 def step(self):ˆà ˆà 14 self.next_positions()ˆà ˆà 15 self.check_collisions()ˆà ˆà 16 self.check_walls()ˆà ˆà 17 self.update_positions()ˆà ˆà 18 self.invoke_ticks()ˆà ˆà 19 self.respawn()ˆà ˆà 20 for i in self.bodies: #testˆà ˆà 21 print i.position #testˆà 18 22 ˆà 19 ˆà def next_positions( game):ˆàˆà 23 def next_positions(self):ˆà 20 24 delta_t = 1ˆà 21 ˆà for i in game.bodies:ˆàˆà 25 for i in self.bodies:ˆà 22 26 i.next_position = i.position + i.velocity*(delta_t)ˆà 23 27 ˆà 24 28 ˆà 25 ˆà def check_collisions(game):ˆà 26 ˆà passˆà ˆà 29 def check_collisions(self):ˆà ˆà 30 for i in self.bodies:ˆà ˆà 31 for j in self.bodies:ˆà ˆà 32 if self.collides(i,j) == True :ˆà ˆà 33 self.handle_collision(i,j) ˆà 27 34 ˆà 28 35 def collides(self,body1,body2):ˆà 29 ˆà passˆà ˆà 36 if (abs(body1.position + body2.position*-1) <= (body1.radius + body2.radius)) and (body1 != body2) :ˆà ˆà 37 print 'collision'ˆà 30 38 ˆà 31 39 def handle_collision(self,body1,body2):ˆà 32 ˆà passˆà ˆà 40 if isinstance(body1, body.Tank) == True : ˆà ˆà 41 if isinstance(body2, body.Tank) == True :ˆà ˆà 42 body1.on_collision(body2)ˆà ˆà 43 else :ˆà ˆà 44 body1.on_hit()ˆà ˆà 45 body1.on_death()ˆà ˆà 46 else :ˆà ˆà 47 if isinstance(body2, body.Tank) == True :ˆà ˆà 48 body2.on_hit()ˆà ˆà 49 body2.on_death()ˆà ˆà 50 else :ˆà ˆà 51 pass # bullets disappearˆà 33 52 ˆà 34 ˆà def check_walls( game):ˆà35 ˆà for i in game.bodies :ˆà36 ˆà 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) :ˆàˆà 53 def check_walls(self):ˆà ˆà 54 for i in self.bodies :ˆà ˆà 55 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) :ˆà 37 56 i.on_wall()ˆà 38 ˆà ˆàˆà 57 print 'wall' #testˆà 39 58 ˆà 40 ˆà def update_positions( game):ˆà41 ˆà for i in game.bodies:ˆàˆà 59 def update_positions(self):ˆà ˆà 60 for i in self.bodies:ˆà 42 61 i.position = i.next_positionˆà 43 62 ˆà 44 ˆà def invoke_ticks( game):ˆà45 ˆà for i in game.users:ˆàˆà 63 def invoke_ticks(self):ˆà ˆà 64 for i in self.users:ˆà 46 65 i.tank.on_tick(other_tanks,bullets)ˆà 47 ˆà ˆà 48 ˆà def respawn( game): ˆà49 ˆà for i in game.users :ˆàˆà 66 ˆà ˆà 67 def respawn(self): ˆà ˆà 68 for i in self.users :ˆà 50 69 if i.tank.strength == 0 :ˆà 51 70 i.tank.on_spawn()ˆà -
vector.py
r14 r15 ˆà 34 34 return 0ˆà 35 35 ˆà ˆà 36 def normalize(self):ˆà ˆà 37 result = Vector()ˆà ˆà 38 result.x = self.xˆà ˆà 39 result.y = self.yˆà ˆà 40 result.rho = 1ˆà ˆà 41 return resultˆà ˆà 42 ˆà 36 43 def get_rho(self):ˆà 37 44 return abs(self)ˆà
Note: See TracChangeset
for help on using the changeset viewer.