Changeset 36:1a0bddee3c54
- Timestamp:
- 12/20/10 18:43:18 (5 years ago)
- Branch:
- new
- Parents:
- 34:b7a85caedc7f (diff), 35:dfdc1def5d24 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
body.py
- Property exe set to *
r34 r36 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 max_velocity = 2 9 max_velocity = 4 10 max_base_angle = 1 11 max_turret_angle = 1 7 12 initial_strength = 1 13 bullet_velocity = 10 14 width = 100 15 height = 100 8 16 9 17 class Body(object): ? ? 11 19 self.position = position 12 20 self.velocity = velocity 13 self.radius = radius14 21 15 22 class Tank(Body): 16 radius = 117 def __init__(self, position, user ): 23 radius = 5 24 def __init__(self, position, user, game): 18 25 Body.__init__(self, position) 26 self.game = game 27 self.turret = vector.i 19 28 self.strength = 0 20 self.turret = vector.i21 29 self.base_orientation = 1 # 1 or -1 22 30 self.user = user 23 user.tank = self # äīįąāė’åņ ńåį’ ā User 31 user.tank = self 24 32 25 def rotate_base(tank, angle): 26 self.velocity.phi += angle 33 34 def rotate_base(tank, angle): 35 if abs(angle) < max_base_angle: 36 self.velocity.phi += angle 37 else: 38 self.velocity.phi += max_base_angle 27 39 28 40 def rotate_turret(self, angle): 29 self.turret.phi += angle 41 if abs(angle) < max_base_angle: 42 self.turret.phi += angle 43 else: 44 self.turret.phi += max_turret_angle 45 30 46 31 47 def accelerate(self, speed_delta): 32 self.velocity.rho += speed_delta * delta_t 33 if self.velocity.rho > max_velocity : 48 self.velocity += self.velocity.normalize() * speed_delta * delta_t 49 print self.velocity.rho 50 if self.velocity.rho > max_velocity: 34 51 self.velocity.rho = max_velocity 35 52 36 53 def fire(self): 37 pass 54 bullet_position = self.position + self.turret * (self.radius + 0.1) 55 bullet_velocity = self.turret.normalize() * self.game.bullet_speed 56 bullet = Bullet(bullet_position, bullet_velocity, self) 57 self.game.bodies.append(bullet) 38 58 39 59 def on_tick(self,other_tanks, bullets): 40 if self.user.base_left == True 41 self.rotate_base( delta_phi)42 if self.user.base_right == True 43 self.rotate_base(-1* delta_phi)44 if self.user.accelerate == True 60 if self.user.base_left == True: 61 self.rotate_base(base_angle) 62 if self.user.base_right == True: 63 self.rotate_base(-1*base_angle) 64 if self.user.accelerate == True: 45 65 self.accelerate(speed_delta) 66 if self.user.decelerate == True: 67 self.accelerate(-1*speed_delta) 68 if self.user.turret_left == True: 69 self.rotate_turret(turret_angle) 70 if self.user.turret_right == True: 71 self.rotate_turret(-1*turret_angle) 72 if self.user.fire == True: 73 self.fire() 46 74 47 75 def on_spawn(self): 48 76 pass 49 77 50 def on_death(self): 78 def on_death(self) : 79 pass 80 81 def on_hit(self, bullet): 51 82 pass 52 83 53 def on_hit(self,bullet): 54 pass 55 56 def on_collision(self): 84 def on_collision(self, other): 57 85 pass 58 86 59 87 def on_wall(self): 60 pass 88 self.next_position = self.position 61 89 62 90 class Bullet(Body): 63 91 radius = 0.1 64 pass 92 def __init__(self, position, velocity, tank): 93 Body.__init__(self, position, velocity = bullet_velocity) 94 self.tank = tank -
game.py
- Property exe set to *
r34 r36 1 import vector 2 import body 3 import time 4 1 5 other_tanks = [] 2 6 bullets = [] ? ? 7 11 self.users = users 8 12 self.width = width 9 self.height = height 13 self.height = height 10 14 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() 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() 18 24 19 def next_positions(game): 25 for i in self.bodies: #test 26 print "end", i, i.position #test 27 # time.sleep(1) #test 28 # self.step() #test 29 30 31 def next_positions(self): 20 32 delta_t = 1 21 for i in game.bodies: 33 for i in self.bodies: 22 34 i.next_position = i.position + i.velocity*(delta_t) 35 23 36 24 def check_collisions(game): 25 pass 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) 26 42 27 43 def collides(self,body1,body2): 28 pass 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 29 54 30 55 def handle_collision(self,body1,body2): 31 pass 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() 32 66 33 def check_walls( game):34 for i in game.bodies :35 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) : 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) : 36 70 i.on_wall() 71 print 'wall' #test 37 72 38 def update_positions( game):39 for i in game.bodies: 73 def update_positions(self): 74 for i in self.bodies: 40 75 i.position = i.next_position 41 76 42 def invoke_ticks(game): 43 for i in game.users : 44 i.tank.on_tick(other_tanks,bullets) 45 46 def respawn(game): 47 for i in game.users : 77 def invoke_ticks(self): 78 for i in self.bodies : 79 if isinstance(i, body.Tank) : 80 i.on_tick([], []) 81 82 83 def respawn(self): 84 for i in self.users : 48 85 if i.tank.strength == 0 : 49 86 i.tank.on_spawn() 50 i.tank.strength = 1 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 92 93 -
user.py
r34 r36 1 1 class User(object): 2 2 3 def __init__(self, base_left = False, base_right = False, 4 turret_left = False, turret_right = False, 5 accelerate = False, decelerate = False, 6 fire = False, tank = 0): 3 def __init__(self, base_left = False , base_right = False, turret_left = False, turret_right = False, accelerate = False, decelerate = False, fire = False, tank = 0): 7 4 self.base_left = base_left 8 5 self.base_right = base_right -
vector.py
- Property exe set to *
r34 r36 1 1 import math 2 2 3 class Vector(object): 3 class Vector(object): 4 4 5 def __init__(self, x=0 , y=0): 5 def __init__(self, x=0 , y=0): 6 6 self.x = x 7 7 self.y = y 8 8 9 9 def __add__(self, other): 10 10 result = Vector(0,0) 11 11 result.x = self.x + other.x 12 12 result.y = self.y + other.y 13 return result 14 15 def __sub__(self, other): 16 result = Vector(0,0) 17 result.x = self.x - other.x 18 result.y = self.y - other.y 13 19 return result 14 20 ? ? 20 26 21 27 def dot_product(self, other): 22 return self.x*other.x + self.y*other.y 28 return self.x*other.x + self.y*other.y 23 29 24 30 def __abs__(self): ? ? 34 40 return 0 35 41 42 def normalize(self): 43 result = Vector() 44 result.x = self.x 45 result.y = self.y 46 result.rho = 1 47 return result 48 36 49 def get_rho(self): 37 50 return abs(self) 38 51 39 52 def set_rho(self, new_rho): 40 if self.is_null() == 1 53 if self.is_null() == 1: 41 54 self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi) 42 55 else : ? ? 49 62 if self.is_null == 1: 50 63 phi = 0 51 return phi 64 return phi 52 65 53 66 def set_phi(self, new_phi): 54 67 rho = abs(self) 55 self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi) 68 self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi) 56 69 57 70 phi = property(get_phi, set_phi) 71 72 i = Vector(1,0) 73 j = Vector(0,1) 74 null = Vector(0,0)
Note: See TracChangeset
for help on using the changeset viewer.