Changes in [20:ee776b889df7:30:1b117f1451bd]
- Location:
- tanchiki
- Files:
-
- 2 added
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
tanchiki/body.py
r19 r28 1 #coding:utf-8 2 1 3 import vector 2 4 import math 3 delta_phi = math.pi # deltha phi = math.pi4 speed_delta = 15 delta_t = 16 max_velocity = 27 initial_strength = 18 5 9 6 class Body(object): 10 def __init__(self, position, velocity = vector.null): 7 def __init__(self, game, position, velocity = vector.Vector.null): 8 self.game = game 11 9 self.position = position 12 10 self.velocity = velocity 13 self.radius = radius14 11 15 12 class Tank(Body): 16 13 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() 19 20 self.strength = 0 20 self.turret = vector.i21 self.base_orientation = 1 # 1 or -122 self.user = user23 user.tank = self # äīįąāė’åņ ńåį’ ā User24 21 25 22 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 27 27 28 28 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 30 33 31 34 def accelerate(self, speed_delta): 32 self.velocity .rho += speed_delta * delta_t 35 self.velocity += self.velocity.normalize() * speed_delta 33 36 if self.velocity.rho > max_velocity : 34 37 self.velocity.rho = max_velocity 35 38 36 39 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) 61 44 62 45 class Bullet(Body): 63 46 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 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 :36 if ((i.next_position.x - i.radius) <= 0) or37 ((i.next_position.y - i.radius) <= 0) or38 ((i.next_position.x + i.radius) >= game.width ) or39 ((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) : 40 55 i.on_wall() 41 42 def __update_positions(self):43 for i in self.bodies :44 i.position = i.next_position45 56 46 57 def __invoke_ticks(self): 47 58 for i in self.bodies : 48 if i.controller 59 if i.controller : 49 60 i.controller.on_tick(other_tanks, bullets) -
tanchiki/ui.py
r19 r26 1 import game 1 import math 2 3 from OpenGL.GLUT import * 4 from OpenGL.GLU import * 5 from OpenGL.GL import * 6 7 from body import * 8 from vector import * 9 from game import * 10 import game as game_module 11 12 glutInit() 13 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) 14 15 class 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) 2 55 3 56 class Ui(object): 4 57 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 65 ui = Ui() 66 ui.loop() -
tanchiki/vector.py
r19 r29 2 2 3 3 class Vector(object): 4 5 4 def __init__(self, x=0 , y=0): 6 5 self.x = x 7 6 self.y = y 8 7 9 8 def __add__(self, other): 10 result = Vector(0, 0) 9 result = Vector(0, 0) 11 10 result.x = self.x + other.x 12 11 result.y = self.y + other.y ? ? 20 19 21 20 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 23 22 24 23 def __abs__(self): ? ? 34 33 return 0 35 34 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 36 42 def get_rho(self): 37 43 return abs(self) 38 44 39 45 def set_rho(self, new_rho): 40 if self.is_null() == 1 46 if self.is_null() == 1: 41 47 self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi) 42 48 else : 43 49 self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self)) 44 50 45 rho = property(get_rho, set_rho) 51 rho = property(get_rho, set_rho) 46 52 47 53 def get_phi(self): ? ? 49 55 if self.is_null == 1: 50 56 phi = 0 51 return phi 57 return phi 52 58 53 59 def set_phi(self, new_phi): 54 60 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) 56 62 57 63 phi = property(get_phi, set_phi) 64 65 Vector.null = Vector(0, 0)
Note: See TracChangeset
for help on using the changeset viewer.