Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://kodomo.cmm.msu.su/trac/tanchiki/changeset/15%3A43f5b82f3491
Äàòà èçìåíåíèÿ: Unknown
Äàòà èíäåêñèðîâàíèÿ: Sun Apr 10 05:03:43 2016
Êîäèðîâêà: IBM-866
Changeset 15:43f5b82f3491 òÀÓ Tanchiki

Changeset 15:43f5b82f3491


Ignore:
Timestamp:
12/20/10 10:53:47 (5 years ago)
Author:
Olga Zolotareva <olya_zol@òÀæ>
Branch:
default
Message:

modules changed

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • body.py

    r13 r15 ˆà
    11import vectorˆà
    22import mathˆà
    3ˆàdelta_phi = math.pi   # deltha phi = math.piˆà
    ˆà3import randomˆà
    ˆà4ˆà
    ˆà5base_angle = math.pi/32   # deltha phi = math.pi/32ˆà
    ˆà6turret_angle = math.pi/32ˆà
    47speed_delta = 1ˆà
    58delta_t = 1ˆà
    69max_velocity = 2ˆà
    ˆà10max_base_angle = 1ˆà
    ˆà11max_turret_angle = 1ˆà
    712initial_strength = 1ˆà
    ˆà13bullet_velocity = 5ˆà
    ˆà14width = 10ˆà
    ˆà15height = 10ˆà
    816ˆà
    917class Body(object):ˆà
    òÀæ òÀæ ˆà
    1119                self.position = positionˆà
    1220                self.velocity = velocityˆà
    13ˆà                self.radius = radiusˆà
    14ˆà        ˆà
    1521ˆà
    1622class Tank(Body):ˆà
    17ˆà        radius = 1ˆà
    ˆà23        radius = 0.5ˆà
    1824        def __init__(self, position, user):ˆà
    1925                Body.__init__(self, position)ˆà
    ˆà26                self.turret = vector.iˆà
    2027                self.strength = 0ˆà
    21ˆà                self.turret = vector.iˆà
    2228                self.base_orientation = 1       # 1 or -1ˆà
    2329                self.user = userˆà
    24ˆà                user.tank = self # †ä†î†á†à†â†ë†ƒ†å†’ †‘†å†á†ƒ †â Userˆà
    ˆà30                user.tank = self ˆà
    2531ˆà
    2632ˆà
    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ˆà
    2938ˆà
    3039        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ˆà
    3245ˆà
    3346        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ˆà
    3549                if self.velocity.rho > max_velocity:ˆà
    3650                        self.velocity.rho = max_velocityˆà
    3751ˆà
    3852        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)ˆà
    4056ˆà
    4157        def on_tick(self,other_tanks, bullets):ˆà
    4258                if self.user.base_left == True:ˆà
    43ˆà                        self.rotate_base(delta_phi)ˆà
    ˆà59                        self.rotate_base(base_angle)ˆà
    4460                if self.user.base_right == True:ˆà
    45ˆà                        self.rotate_base(-1*delta_phi)ˆà
    ˆà61                        self.rotate_base(-1*base_angle)ˆà
    4662                if self.user.accelerate == True:ˆà
    4763                        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()ˆà
    4872ˆà
    4973        def on_spawn(self):ˆà
    5074                passˆà
    ˆà75#               self.position.x = random.randint(1,width - 10)ˆà
    ˆà76#               self.position.y = random.randint(1,height - 10)ˆà
    ˆà77#               self.velocity = vector.null ˆà
    5178ˆà
    5279        def on_death(self) :ˆà
    5380                passˆà
    5481        ˆà
    55ˆà        def on_hit(self,bullet):ˆà
    ˆà82        def on_hit(self, bullet):ˆà
    5683                passˆà
    5784ˆà
    58ˆà        def on_collision(self):ˆà
    ˆà85        def on_collision(self, other):ˆà
    5986                passˆà
    6087ˆà
    6188        def on_wall(self):ˆà
    62ˆà                passˆà
    ˆà89                self.next_position = self.positionˆà
    6390ˆà
    6491class Bullet(Body):ˆà
    6592        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 ˆà
    ˆà1import vectorˆà
    ˆà2ˆà
    13other_tanks = []ˆà
    24bullets = []ˆà
    òÀæ òÀæ ˆà
    911                self.height = heightˆà
    1012ˆà
    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ˆà
    1822ˆà
    19ˆà        def next_positions(game):ˆà
    ˆà23        def next_positions(self):ˆà
    2024                delta_t = 1ˆà
    21ˆà                for i in game.bodies:ˆà
    ˆà25                for i in self.bodies:ˆà
    2226                        i.next_position = i.position + i.velocity*(delta_t)ˆà
    2327        ˆà
    2428ˆà
    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)                      ˆà
    2734ˆà
    2835        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'ˆà
    3038ˆà
    3139        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ˆà
    3352ˆà
    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) :ˆà
    3756                                i.on_wall()ˆà
    38ˆà                ˆà
    ˆà57                                print 'wall'      #testˆà
    3958ˆà
    40ˆà        def update_positions(game):ˆà
    41ˆà                for i in game.bodies:ˆà
    ˆà59        def update_positions(self):ˆà
    ˆà60                for i in self.bodies:ˆà
    4261                        i.position = i.next_positionˆà
    4362ˆà
    44ˆà        def invoke_ticks(game):ˆà
    45ˆà                for i in game.users:ˆà
    ˆà63        def invoke_ticks(self):ˆà
    ˆà64                for i in self.users:ˆà
    4665                        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 :ˆà
    5069                        if i.tank.strength == 0 :ˆà
    5170                                i.tank.on_spawn()ˆà
  • vector.py

    r14 r15 ˆà
    3434                        return 0ˆà
    3535ˆà
    ˆà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ˆà
    3643        def get_rho(self):ˆà
    3744                return abs(self)ˆà
Note: See TracChangeset for help on using the changeset viewer.