Changeset 3:af02cd410e37
- Timestamp:
- 12/18/10 15:20:51 (5 years ago)
- Branch:
- default
- Children:
- 4:7313e5a09453, 6:b95121219b90
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
body.py
- Property exe set to *
r2 r3 ˆà 1 1 import vectorˆà ˆà 2 import mathˆà ˆà 3 delta_phi = math.pi # deltha phi = math.piˆà ˆà 4 speed_delta = 1ˆà ˆà 5 delta_t = 1ˆà ˆà 6 max_velocity = 2ˆà 2 7 ˆà 3 8 class Body(object):ˆà 4 ˆà def __init__(self, position, velocity, next_position, radius =1):ˆàˆà 9 def __init__(self, position, velocity, next_position, radius):ˆà 5 10 self.position = positionˆà 6 11 self.velocity = velocityˆà òÀæ òÀæ ˆà 13 18 self.strength = strengthˆà 14 19 self.turret = turretˆà 15 ˆà self.base_orientation = base_orientation ˆàˆà 20 self.base_orientation = base_orientation # 1 or -1ˆà 16 21 self.game = gameˆà 17 22 self.user = userˆà 18 ˆà def rotate_base(tank, angle):ˆà 19 ˆà passˆà ˆà 23 ˆà ˆà 24 def rotate_base(tank, angle): ˆà ˆà 25 tank.base_velocity.phi += angleˆà ˆà 26 tank.velocity.phi *= tank.base_orientationˆà 20 27 ˆà 21 28 def rotate_turret(tank, angle):ˆà òÀæ òÀæ ˆà 23 30 ˆà 24 31 def accelerate(tank, speed_delta):ˆà 25 ˆà passˆà ˆà 32 tank.velocity.rho += speed_delta * delta_tˆà ˆà 33 if tank.velocity.rho > max_velocity:ˆà ˆà 34 tank.velocity.rho = max_velocityˆà 26 35 ˆà 27 36 def fire(tank):ˆà òÀæ òÀæ ˆà 29 38 ˆà 30 39 def on_tick(tank,other_tanks, bullets):ˆà 31 ˆà passˆà ˆà 40 if tank.user.base_left == True:ˆà ˆà 41 tank.rotate_base(delta_phi)ˆà ˆà 42 if tank.user.base_right == True:ˆà ˆà 43 tank.rotate_base(-1*delta_phi)ˆà ˆà 44 if tank.user.accelerate == True:ˆà ˆà 45 tank.accelerate(speed_delta)ˆà 32 46 ˆà 33 47 def on_spawn(tank):ˆà -
game.py
- Property exe set to *
r2 r3 ˆà ˆà 1 other_tanks = []ˆà ˆà 2 bullets = []ˆà ˆà 3 ˆà 1 4 class Game(object):ˆà 2 5 def __init__(self, bodies, users, width, height):ˆà òÀæ òÀæ ˆà 17 20 delta_t = 1ˆà 18 21 for i in game.bodies:ˆà 19 ˆà i.next_position = i.position + i.velocity .mul_v(delta_t)ˆàˆà 22 i.next_position = i.position + i.velocity*(delta_t)ˆà 20 23 ˆà 21 24 ˆà òÀæ òÀæ ˆà 31 34 def check_walls(game):ˆà 32 35 for i in game.bodies :ˆà 33 ˆà if ( i.next_position.x <= 0) or (i.next_position.y <= 0) or (i.next_position.x <= width) or (i.next_position.y>= game.height) :ˆàˆà 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) :ˆà 34 37 i.on_wall()ˆà 35 38 ˆà òÀæ òÀæ ˆà 40 43 ˆà 41 44 def invoke_ticks(game):ˆà 42 ˆà passˆà ˆà 45 for i in game.users:ˆà ˆà 46 i.tank.on_tick(other_tanks,bullets)ˆà 43 47 ˆà 44 48 def respawn(game): ˆà 45 49 for i in game.users :ˆà 46 50 if i.tank.strength == 0 :ˆà 47 ˆà i.tank.respawn()ˆà 48 ˆà ˆà 49 ˆà ˆà 50 ˆà ˆà ˆà 51 i.tank.on_spawn()ˆà ˆà 52 i.tank.strength = 1 ˆà -
user.py
- Property exe set to *
-
vector.py
- Property exe set to *
r0 r3 ˆà 13 13 return resultˆà 14 14 ˆà 15 ˆà def mul_v(self, alpha):ˆàˆà 15 def __mul__(self, alpha):ˆà 16 16 result = Vector()ˆà 17 17 result.x = self.x * alphaˆà òÀæ òÀæ ˆà 22 22 return self.x*other.x + self.y*other.y ˆà 23 23 ˆà 24 ˆà def len_v(self):ˆàˆà 24 def __abs__(self):ˆà 25 25 return (self.x**2 + self.y**2)**0.5ˆà 26 26 ˆà òÀæ òÀæ ˆà 29 29 ˆà 30 30 def is_null(self):ˆà 31 ˆà if self.len_v() == 0 :ˆàˆà 31 if abs(self) == 0 :ˆà 32 32 return 1ˆà 33 33 else :ˆà òÀæ òÀæ ˆà 36 36 ˆà 37 37 def get_rho(self):ˆà 38 ˆà return self.len_v()ˆàˆà 38 return abs(self)ˆà 39 39 ˆà 40 40 def set_rho(self, new_rho):ˆà 41 ˆà self.x , self.y = self.x*(new_rho/ self.len_v()) , self.y*(new_rho/self.len_v())ˆàˆà 41 self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self))ˆà 42 42 ˆà 43 43 rho = property(get_rho, set_rho) ˆà 44 44 ˆà 45 45 def get_phi(self):ˆà 46 ˆà cos = self.dot_product(Vector(1,0))/( self.len_v()*1)ˆàˆà 46 cos = self.dot_product(Vector(1,0))/(abs(self)*1)ˆà 47 47 if self.y < 0 : ˆà 48 48 phi = -acos(cos)ˆà òÀæ òÀæ ˆà 52 52 ˆà 53 53 def set_phi(self, new_phi):ˆà 54 ˆà self.x , self.y = self.len_v()*cos(new_phi) , self.len_v()*sin(new_phi)ˆàˆà 54 self.x , self.y = abs(self)*cos(new_phi) , abs(self)*sin(new_phi)ˆà 55 55 ˆà 56 56 phi = property(get_phi, set_phi)ˆà
Note: See TracChangeset
for help on using the changeset viewer.