Changeset 28:4ef6336d1707 for tanchiki
- Timestamp:
- 12/20/10 11:32:48 (5 years ago)
- Branch:
- default
- Parents:
- 27:b9856f9de9e2 (diff), 15:43f5b82f3491 (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. - Location:
- tanchiki
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
tanchiki/body.py
r25 r28 ˆà 5 5 ˆà 6 6 class Body(object):ˆà 7 ˆà def __init__(self, position, velocity = vector.Vector.null):ˆà ˆà 7 def __init__(self, game, position, velocity = vector.Vector.null):ˆà ˆà 8 self.game = gameˆà 8 9 self.position = positionˆà 9 10 self.velocity = velocityˆà òÀæ òÀæ ˆà 13 14 model = "tank"ˆà 14 15 ˆà 15 ˆà def __init__(self, position, controller):ˆà16 ˆà Body.__init__(self, position)ˆàˆà 16 def __init__(self, game, position, controller):ˆà ˆà 17 Body.__init__(self, game, position)ˆà 17 18 self.controller = controllerˆà 18 19 self.turret = vector.Vector()ˆà òÀæ òÀæ ˆà 20 21 ˆà 21 22 def rotate_base(tank, angle): ˆà 22 ˆà self.velocity.phi += angleˆà ˆà 23 if abs(angle) < self.game.max_base_delta :ˆà ˆà 24 self.velocity.phi += angleˆà ˆà 25 else :ˆà ˆà 26 self.velocity.phi += self.game.max_base_deltaˆà 23 27 ˆà 24 28 def rotate_turret(self, angle):ˆà 25 ˆà self.turret.phi += angleˆà ˆà 29 if abs(angle) < self.game.max_base_delta :ˆà ˆà 30 self.turret.phi += angleˆà ˆà 31 else :ˆà ˆà 32 self.turret.phi += self.game.max_turret_deltaˆà 26 33 ˆà 27 34 def accelerate(self, speed_delta):ˆà 28 ˆà self.velocity .rho += speed_delta * delta_tˆàˆà 35 self.velocity += self.velocity.normalize() * speed_deltaˆà 29 36 if self.velocity.rho > max_velocity :ˆà 30 37 self.velocity.rho = max_velocityˆà 31 38 ˆà 32 39 def fire(self):ˆà 33 ˆà passˆà ˆà 40 bullet_position = self.position + self.turret * (self.radius + 0.1)ˆà ˆà 41 bullet_velocity = self.turret.normalize() * self.game.bullet_speedˆà ˆà 42 bullet = Bullet(bullet_position, bullet_velocity, self)ˆà ˆà 43 self.game.bodies.append(bullet)ˆà 34 44 ˆà 35 45 class Bullet(Body):ˆà òÀæ òÀæ ˆà 37 47 model = "bullet"ˆà 38 48 ˆà 39 ˆà passˆà ˆà 49 def __init__(self, position, velocity, tank):ˆà ˆà 50 Body.__init__(self, position, velocity)ˆà ˆà 51 self.origin = originˆà -
tanchiki/game.py
r22 r28 ˆà 24 24 ˆà 25 25 def __collides(self, body1, body2):ˆà 26 ˆà passˆà ˆà 26 return (abs(body1.position - body2.position) <= (body1.radius + body2.radius))ˆà ˆà 27 and (body1 != body2)ˆà 27 28 ˆà 28 29 def __handle_collision(self, body1, body2):ˆà 29 ˆà passˆà ˆà 30 if isinstance(body1, body.Tank) == True : ˆà ˆà 31 if isinstance(body2, body.Tank) == True :ˆà ˆà 32 body1.on_collision(body2)ˆà ˆà 33 body2.on_collision(body1)ˆà ˆà 34 else :ˆà ˆà 35 body1.on_hit()ˆà ˆà 36 body1.on_death()ˆà ˆà 37 else :ˆà ˆà 38 if isinstance(body2, body.Tank) == True :ˆà ˆà 39 body2.on_hit()ˆà ˆà 40 body2.on_death()ˆà 30 41 ˆà 31 42 def __check_collisions(game):ˆà 32 ˆà passˆà ˆà 43 for i in range(len(self.bodies)) :ˆà ˆà 44 for j in range(i, len(self.bodies)) :ˆà ˆà 45 a, b = self.bodies[i], self.bodies[j]ˆà ˆà 46 if self.collides(a, b) == True :ˆà ˆà 47 self.handle_collision(a, b)ˆà 33 48 ˆà 34 ˆà def __check_walls( game):ˆà35 ˆà for i in game.bodies :ˆàˆà 49 def __check_walls(self):ˆà ˆà 50 for i in self.bodies :ˆà 36 51 if ((i.next_position.x - i.radius) <= -game.width/2) or \ˆà 37 52 ((i.next_position.y - i.radius) <= -game.width/2) or \ˆà -
tanchiki/vector.py
r23 r28 ˆà 1 1 import mathˆà 2 2 ˆà 3 ˆà class Vector(object):ˆà 4 ˆà def __init__(self, x=0 , y=0):ˆà ˆà 3 class Vector(object): ˆà ˆà 4 ˆà ˆà 5 def __init__(self, x=0 , y=0): ˆà 5 6 self.x = xˆà 6 7 self.y = yˆà 7 ˆà ˆà ˆà 8 ˆà 8 9 def __add__(self, other):ˆà 9 10 result = Vector(0,0)ˆà òÀæ òÀæ ˆà 19 20 ˆà 20 21 def dot_product(self, other):ˆà 21 ˆà return self.x*other.x + self.y*other.yˆàˆà 22 return self.x*other.x + self.y*other.y ˆà 22 23 ˆà 23 24 def __abs__(self):ˆà òÀæ òÀæ ˆà 33 34 return 0ˆà 34 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 ˆà 35 43 def get_rho(self):ˆà 36 44 return abs(self)ˆà 37 45 ˆà 38 46 def set_rho(self, new_rho):ˆà 39 ˆà if self.is_null() == 1 ˆà 47 if self.is_null() == 1:ˆà 40 48 self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi)ˆà 41 49 else :ˆà òÀæ òÀæ ˆà 48 56 if self.is_null == 1:ˆà 49 57 phi = 0ˆà 50 ˆà return phiˆàˆà 58 return phiˆà 51 59 ˆà 52 60 def set_phi(self, new_phi):ˆà 53 61 rho = abs(self)ˆà 54 ˆà self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi)ˆàˆà 62 self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi)ˆà 55 63 ˆà 56 64 phi = property(get_phi, set_phi)ˆà 57 ˆà ˆà58 ˆà Vector.null = Vector(0,0)ˆà
Note: See TracChangeset
for help on using the changeset viewer.