Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/tanchiki/raw-rev/48f019a6c1c6
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 10:38:38 2012
Кодировка:

# HG changeset patch
# User Peter Zotov
# Date 1292837348 -10800
# Node ID 48f019a6c1c6bab499cff0478e4cd3a52cd979fa
# Parent 1b117f1451bdc99979441262e4aacc18e018cafc
Made a working sandbox.

diff -r 1b117f1451bd -r 48f019a6c1c6 tanchiki/body.py
--- a/tanchiki/body.py Mon Dec 20 11:37:03 2010 +0300
+++ b/tanchiki/body.py Mon Dec 20 12:29:08 2010 +0300
@@ -10,16 +10,16 @@
self.velocity = velocity

class Tank(Body):
- radius = 1
+ radius = 5
model = "tank"

def __init__(self, game, position, controller):
Body.__init__(self, game, position)
self.controller = controller
- self.turret = vector.Vector()
+ self.turret = vector.Vector(0, 1)
self.strength = 0

- def rotate_base(tank, angle):
+ def rotate_base(self, angle):
if abs(angle) < self.game.max_base_delta :
self.velocity.phi += angle
else :
@@ -33,19 +33,19 @@

def accelerate(self, speed_delta):
self.velocity += self.velocity.normalize() * speed_delta
- if self.velocity.rho > max_velocity :
- self.velocity.rho = max_velocity
+ if self.velocity.rho > self.game.max_velocity :
+ self.velocity.rho = self.game.max_velocity

def fire(self):
bullet_position = self.position + self.turret * (self.radius + 0.1)
bullet_velocity = self.turret.normalize() * self.game.bullet_speed
- bullet = Bullet(bullet_position, bullet_velocity, self)
+ bullet = Bullet(self.game, bullet_position, bullet_velocity, self)
self.game.bodies.append(bullet)

class Bullet(Body):
- radius = 0.1
+ radius = 1
model = "bullet"

- def __init__(self, position, velocity, tank):
- Body.__init__(self, position, velocity)
- self.origin = origin
+ def __init__(self, game, position, velocity, tank):
+ Body.__init__(self, game, position, velocity)
+ self.origin = tank
diff -r 1b117f1451bd -r 48f019a6c1c6 tanchiki/controller.py
--- a/tanchiki/controller.py Mon Dec 20 11:37:03 2010 +0300
+++ b/tanchiki/controller.py Mon Dec 20 12:29:08 2010 +0300
@@ -1,13 +1,8 @@
import body

-class Controller(object)
- def on_tick(self,other_tanks, bullets):
- if self.user.base_left == True :
- self.rotate_base(delta_phi)
- if self.user.base_right == True :
- self.rotate_base(-1*delta_phi)
- if self.user.accelerate == True :
- self.accelerate(speed_delta)
+class Controller(object):
+ def on_tick(self, other_tanks, bullets):
+ pass

def on_spawn(self):
pass
@@ -15,7 +10,7 @@
def on_death(self):
pass

- def on_hit(self,bullet):
+ def on_hit(self, bullet):
pass

def on_collision(self):
diff -r 1b117f1451bd -r 48f019a6c1c6 tanchiki/game.py
--- a/tanchiki/game.py Mon Dec 20 11:37:03 2010 +0300
+++ b/tanchiki/game.py Mon Dec 20 12:29:08 2010 +0300
@@ -1,7 +1,11 @@
-other_tanks = []
-bullets = []
+from body import *

class Game(object):
+ max_base_delta = 1
+ max_turret_delta = 1
+ max_velocity = 15
+ bullet_speed = 3
+
def __init__(self, width, height, bodies):
self.bodies = bodies
self.width = width
@@ -23,38 +27,41 @@
i.position = i.next_position

def __collides(self, body1, body2):
- return (abs(body1.position - body2.position) <= (body1.radius + body2.radius)) \
+ return (abs(body1.position + body2.position*-1) <= (body1.radius + body2.radius)) \
and (body1 != body2)

def __handle_collision(self, body1, body2):
- if isinstance(body1, body.Tank) == True :
- if isinstance(body2, body.Tank) == True :
- body1.on_collision(body2)
- body2.on_collision(body1)
+ if isinstance(body1, Tank) == True :
+ if isinstance(body2, Tank) == True :
+ body1.controller.on_collision(body2)
+ body2.controller.on_collision(body1)
else :
- body1.on_hit()
- body1.on_death()
+ body1.controller.on_hit(body2)
+ body1.controller.on_death()
else :
- if isinstance(body2, body.Tank) == True :
- body2.on_hit()
- body2.on_death()
+ if isinstance(body2, Tank) == True :
+ body2.controller.on_hit(body1)
+ body2.controller.on_death()

- def __check_collisions(game):
+ def __check_collisions(self):
for i in range(len(self.bodies)) :
for j in range(i, len(self.bodies)) :
a, b = self.bodies[i], self.bodies[j]
- if self.collides(a, b) == True :
- self.handle_collision(a, b)
+ if self.__collides(a, b) == True :
+ self.__handle_collision(a, b)

def __check_walls(self):
for i in self.bodies :
- if ((i.next_position.x - i.radius) <= -game.width/2) or \
- ((i.next_position.y - i.radius) <= -game.width/2) or \
- ((i.next_position.x + i.radius) >= game.width/2) or \
- ((i.next_position.y + i.radius) >= game.height/2) :
- i.on_wall()
+ if ((i.next_position.x - i.radius) <= -self.width/2) or \
+ ((i.next_position.y - i.radius) <= -self.width/2) or \
+ ((i.next_position.x + i.radius) >= self.width/2) or \
+ ((i.next_position.y + i.radius) >= self.height/2) :
+ if isinstance(i, Tank) :
+ i.controller.on_wall()
+ else :
+ self.bodies.remove(i)

def __invoke_ticks(self):
for i in self.bodies :
- if i.controller :
- i.controller.on_tick(other_tanks, bullets)
+ if isinstance(i, Tank) :
+ i.controller.on_tick([], [])
diff -r 1b117f1451bd -r 48f019a6c1c6 tanchiki/ui.py
--- a/tanchiki/ui.py Mon Dec 20 11:37:03 2010 +0300
+++ b/tanchiki/ui.py Mon Dec 20 12:29:08 2010 +0300
@@ -7,13 +7,14 @@
from body import *
from vector import *
from game import *
+from user_controller import *
import game as game_module

glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)

class GLWindow(object):
- def __init__(self, game, tank):
+ def __init__(self, game, tank, timer=None, ival=None):
self.game, self.tank = game, tank

window = glutCreateWindow('hello')
@@ -23,7 +24,7 @@
glPolygonMode(GL_FRONT, GL_LINE)
glBegin(GL_POLYGON)
for i in range(100) :
- glVertex2f(math.cos(i*2*math.pi/100.0), math.sin(i*2*math.pi/100.0))
+ glVertex2f(math.cos(i*2*math.pi/100.0)/2, math.sin(i*2*math.pi/100.0)/2)
glEnd()
glEndList(circle)

@@ -32,32 +33,59 @@
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glLoadIdentity()
- glScalef(1/game.width, 1/game.height, 0)
+ glScalef(2.0/game.width, 2.0/game.height, 0)

for body in game.bodies :
glPushMatrix()
- #glTranslatef(body.position.x, body.position.y, 0)
+ glTranslatef(body.position.x, body.position.y, 0)

- #glScalef(body.radius, body.radius, 0)
+ glScalef(body.radius, body.radius, 0)
if body is tank :
glColor3f(255, 255, 0)
- elif body.__class__ is Tank :
+ elif isinstance(body, Tank) :
glColor3f(0, 255, 0)
- elif body.__class__ is Tank :
+ elif isinstance(body, Bullet) :
glColor3f(255, 0, 0)

glCallList(circle)
+
+ glBegin(GL_LINES)
+ glVertex2f(0, 0)
+ glVertex2f(body.velocity.x, body.velocity.y)
+ glEnd()
+
glPopMatrix()

glutSwapBuffers()

+ def keyboard(key, a, b):
+ tank.controller.handle_keypress(key)
+ glutPostRedisplay()
+
+ def timer_func(val):
+ timer.__call__()
+ glutPostRedisplay()
+ glutTimerFunc(ival, timer_func, 0)
+
glutDisplayFunc(display)
+ glutKeyboardFunc(keyboard)
+
+ if timer :
+ glutTimerFunc(ival, timer_func, 0)

class Ui(object):
def __init__(self):
- bodies = [ Tank(Vector.null, None) ]
+ bodies = []
self.game = Game(100, 100, bodies)
- self.window = GLWindow(self.game, bodies[0])
+ tank = Tank(self.game, Vector.null, None)
+ controller = UserController('a', 'd', 'q', 'e', 'w', 's', 'x', tank)
+ tank.controller = controller
+ bodies.append(tank)
+
+ def idle() :
+ self.game.step(1)
+
+ self.window = GLWindow(self.game, bodies[0], idle, 100)

def loop(self):
glutMainLoop()
diff -r 1b117f1451bd -r 48f019a6c1c6 tanchiki/user_controller.py
--- a/tanchiki/user_controller.py Mon Dec 20 11:37:03 2010 +0300
+++ b/tanchiki/user_controller.py Mon Dec 20 12:29:08 2010 +0300
@@ -1,6 +1,11 @@
-import controller
+from controller import *
+import math

class UserController(Controller):
+ base_angle_delta = math.pi/32
+ turret_angle_delta = math.pi/32
+ speed_delta = 1
+
def __init__(self, base_left = None, base_right = None,
turret_left = None, turret_right = None,
accelerate = None, decelerate = None,
@@ -14,6 +19,18 @@
self.fire = fire
self.tank = tank

- def handle_keypress():
- pass
-
+ def handle_keypress(self, key):
+ if self.base_left == key:
+ self.tank.rotate_base(self.base_angle_delta)
+ if self.base_right == key:
+ self.tank.rotate_base(-self.base_angle_delta)
+ if self.accelerate == key:
+ self.tank.accelerate(self.speed_delta)
+ if self.decelerate == key:
+ self.tank.accelerate(-self.speed_delta)
+ if self.turret_left == key:
+ self.tank.rotate_turret(self.turret_angle_delta)
+ if self.turret_right == key:
+ self.tank.rotate_turret(-self.turret_angle_delta)
+ if self.fire == key:
+ self.tank.fire()