Äīźóģåķņ āē’ņ čē źżųą ļīčńźīāīé ģąųčķū. Ąäšåń īščćčķąėüķīćī äīźóģåķņą : http://kodomo.cmm.msu.ru/trac/tanchiki/changeset/1b117f1451bdc99979441262e4aacc18e018cafc/?old=ee776b889df7b0c44a0e79372dc94f1e1a8f177d&old_path=%2F
Äąņą čēģåķåķč’: Unknown
Äąņą čķäåźńčšīāąķč’: Sun Mar 2 00:04:53 2014
Źīäčšīāźą: UTF-8
Diff [ee776b889df7b0c44a0e79372dc94f1e1a8f177d:1b117f1451bdc99979441262e4aacc18e018cafc] for / ? Tanchiki

Ignore:
Location:
tanchiki
Files:
2 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • tanchiki/body.py

    r19 r28 š
    š1#coding:utf-8š
    š2š
    13import vectorš
    24import mathš
    3šdelta_phi = math.pi   # deltha phi = math.piš
    4šspeed_delta = 1š
    5šdelta_t = 1š
    6šmax_velocity = 2š
    7šinitial_strength = 1š
    85š
    96class Body(object):š
    10š        def __init__(self, position, velocity = vector.null):š
    š7        def __init__(self, game, position, velocity = vector.Vector.null):š
    š8                self.game = gameš
    119                self.position = positionš
    1210                self.velocity = velocityš
    13š                self.radius = radiusš
    1411š
    1512class Tank(Body):š
    1613        radius = 1š
    17š        def __init__(self, position, user):š
    18š                Body.__init__(self, position)š
    š14        model = "tank"š
    š15š
    š16        def __init__(self, game, position, controller):š
    š17                Body.__init__(self, game, position)š
    š18                self.controller = controllerš
    š19                self.turret = vector.Vector()š
    1920                self.strength = 0š
    20š                self.turret = vector.iš
    21š                self.base_orientation = 1       # 1 or -1š
    22š                self.user = userš
    23š                user.tank = self # äīįąāė’åņ ńåį’ ā Userš
    2421š
    2522        def rotate_base(tank, angle): š
    26š                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š
    2727š
    2828        def rotate_turret(self, angle):š
    29š                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š
    3033š
    3134        def accelerate(self, speed_delta):š
    32š                self.velocity.rho += speed_delta * delta_tš
    š35                self.velocity += self.velocity.normalize() * speed_deltaš
    3336                if self.velocity.rho > max_velocity :š
    3437                        self.velocity.rho = max_velocityš
    3538š
    3639        def fire(self):š
    37š                passš
    38šš
    39š        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 :š
    45š                        self.accelerate(speed_delta)š
    46šš
    47š        def on_spawn(self):š
    48š                passš
    49šš
    50š        def on_death(self):š
    51š                passš
    52šš
    53š        def on_hit(self,bullet):š
    54š                passš
    55šš
    56š        def on_collision(self):š
    57š                passš
    58šš
    59š        def on_wall(self):š
    60š                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)š
    6144š
    6245class Bullet(Body):š
    6346        radius = 0.1š
    64š        passš
    š47        model = "bullet"š
    š48š
    š49        def __init__(self, position, velocity, tank):š
    š50                Body.__init__(self, position, velocity)š
    š51                self.origin = originš
  • tanchiki/game.py

    r20 r30 š
    2424š
    2525        def __collides(self, body1, body2):š
    26š                passš
    š26                return (abs(body1.position - body2.position) <= (body1.radius + body2.radius)) \š
    š27                       and (body1 != body2)š
    2728š
    2829        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()š
    3041š
    3142        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)š
    3348š
    34š        def __check_walls(game):š
    35š                for i in game.bodies :š
    36š                        if ((i.next_position.x - i.radius) <= 0) orš
    37š                           ((i.next_position.y - i.radius) <= 0) orš
    38š                           ((i.next_position.x + i.radius) >= game.width) orš
    39š                           ((i.next_position.y + i.radius) >= game.height) :š
    š49        def __check_walls(self):š
    š50                for i in self.bodies :š
    š51                        if ((i.next_position.x - i.radius) <= -game.width/2) or \š
    š52                           ((i.next_position.y - i.radius) <= -game.width/2) or \š
    š53                           ((i.next_position.x + i.radius) >= game.width/2) or \š
    š54                           ((i.next_position.y + i.radius) >= game.height/2) :š
    4055                                i.on_wall()š
    41šš
    42š        def __update_positions(self):š
    43š                for i in self.bodies :š
    44š                        i.position = i.next_positionš
    4556š
    4657        def __invoke_ticks(self):š
    4758                for i in self.bodies :š
    48š                        if i.controllerš
    š59                        if i.controller :š
    4960                                i.controller.on_tick(other_tanks, bullets)š
  • tanchiki/ui.py

    r19 r26 š
    1šimport gameš
    š1import mathš
    š2š
    š3from OpenGL.GLUT import *š
    š4from OpenGL.GLU import *š
    š5from OpenGL.GL import *š
    š6š
    š7from body import *š
    š8from vector import *š
    š9from game import *š
    š10import game as game_moduleš
    š11š
    š12glutInit()š
    š13glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)š
    š14š
    š15class GLWindow(object):š
    š16        def __init__(self, game, tank):š
    š17                self.game, self.tank = game, tankš
    š18š
    š19                window = glutCreateWindow('hello')š
    š20š
    š21                circle = glGenLists(1)š
    š22                glNewList(circle, GL_COMPILE)š
    š23                glPolygonMode(GL_FRONT, GL_LINE)š
    š24                glBegin(GL_POLYGON)š
    š25                for i in range(100) :š
    š26                        glVertex2f(math.cos(i*2*math.pi/100.0), math.sin(i*2*math.pi/100.0))š
    š27                glEnd()š
    š28                glEndList(circle)š
    š29š
    š30                def display():š
    š31                        glutSetWindow(window)š
    š32                        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)š
    š33š
    š34                        glLoadIdentity()š
    š35                        glScalef(1/game.width, 1/game.height, 0)š
    š36š
    š37                        for body in game.bodies :š
    š38                                glPushMatrix()š
    š39                                #glTranslatef(body.position.x, body.position.y, 0)š
    š40š
    š41                                #glScalef(body.radius, body.radius, 0)š
    š42                                if body is tank :š
    š43                                        glColor3f(255, 255, 0)š
    š44                                elif body.__class__ is Tank :š
    š45                                        glColor3f(0, 255, 0)š
    š46                                elif body.__class__ is Tank :š
    š47                                        glColor3f(255, 0, 0)š
    š48š
    š49                                glCallList(circle)š
    š50                                glPopMatrix()š
    š51š
    š52                        glutSwapBuffers()š
    š53š
    š54                glutDisplayFunc(display)š
    255š
    356class Ui(object):š
    457        def __init__(self):š
    5š                passš
    š58                bodies = [ Tank(Vector.null, None) ]š
    š59                self.game = Game(100, 100, bodies)š
    š60                self.window = GLWindow(self.game, bodies[0])š
    š61š
    š62        def loop(self):š
    š63                glutMainLoop()š
    š64š
    š65ui = Ui()š
    š66ui.loop()š
  • tanchiki/vector.py

    r19 r29 š
    22š
    33class Vector(object):š
    4šš
    54        def __init__(self, x=0 , y=0):š
    65                self.x = xš
    76                self.y = yš
    8šš
    š7        š
    98        def __add__(self, other):š
    10š                result = Vector(0,0)š
    š9                result = Vector(0, 0)š
    1110                result.x = self.x + other.xš
    1211                result.y = self.y + other.yš
    ? ? š
    2019š
    2120        def dot_product(self, other):š
    22š                return self.x*other.x + self.y*other.yš
    š21                return  self.x*other.x + self.y*other.yš
    2322š
    2423        def __abs__(self):š
    ? ? š
    3433                        return 0š
    3534š
    š35        def normalize(self):š
    š36                result = Vector()š
    š37                result.x = self.xš
    š38                result.y = self.yš
    š39                result.rho = 1š
    š40                return resultš
    š41š
    3642        def get_rho(self):š
    3743                return abs(self)š
    3844š
    3945        def set_rho(self, new_rho):š
    40š                if self.is_null() == 1 :š
    š46                if self.is_null() == 1:š
    4147                        self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi)š
    4248                else :š
    4349                        self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self))š
    4450š
    45š        rho = property(get_rho, set_rho) š
    š51        rho = property(get_rho, set_rho)š
    4652š
    4753        def get_phi(self):š
    ? ? š
    4955                if self.is_null == 1:š
    5056                        phi = 0š
    51š                return phiš
    š57                return  phiš
    5258š
    5359        def set_phi(self, new_phi):š
    5460                rho = abs(self)š
    55š                self.x, self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi)š
    š61                self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi)š
    5662š
    5763        phi = property(get_phi, set_phi)š
    š64š
    š65Vector.null = Vector(0, 0)š
Note: See TracChangeset for help on using the changeset viewer.