Guest
Public paste!

phlyingpenguin

By: a guest | Sep 12th, 2008 | Syntax: Python | Size: 6.10 KB | Hits: 364 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. from OpenGL.GL import *
  2. from OpenGL.GLU import *
  3. from OpenGL.GLUT import *
  4. from numpy import *
  5.  
  6. class Turtle:
  7.         def __init__(self,x, y, theta):
  8.                 self.x = x
  9.                 self.y = y
  10.                 self.t = theta
  11.                 self.pen = 0
  12.                 self.trail = []
  13.        
  14.         """ Move forward 'distance' units """
  15.         def forward(self,distance):
  16.                 line = [(self.x,self.y)]
  17.                 self.x += distance * cos(radians(self.t))
  18.                 self.y += distance * sin(radians(self.t))
  19.                 line.append( (self.x, self.y) )
  20.                 if self.pen == 1: self.trail.append(line)
  21.                 glutPostRedisplay()
  22.        
  23.         """ Move right 'angle' degrees. Note that 'angle' is POSITIVE to move right """
  24.         def right(self,angle):
  25.                 self.rotate(-angle)
  26.        
  27.         """ Turn left 'angle' degrees """
  28.         def left(self,angle):
  29.                 self.rotate(angle)
  30.  
  31.         def rotate(self, angle):
  32.                 self.t += angle
  33.                 if self.t > 360: self.t -= 360
  34.                 if self.t < -360: self.t += 360
  35.                 glutPostRedisplay()
  36.        
  37.         """ Set pen on or off (1/0) """
  38.         def pen(self,position):
  39.                 self.pen = position
  40.        
  41.         """ Flip pen bit """
  42.         def flip_pen(self):
  43.                 self.pen = self.pen ^ 1
  44.  
  45.         def get_loc(self):
  46.                 return (self.x, self.y)
  47.  
  48.         def get_dir(self):
  49.                 return self.t
  50.        
  51.         """ Use data to produce the current image """
  52.         def draw(self):
  53.                 """ shape x points """
  54.                 polys = [[(1.0, -0.99999999999999989),
  55.                                   (1.5000000000000002, -2.5),
  56.                                   (2.0, -1.9999999999999998)],
  57.                                  [(-0.99999999999999989, -1.0),
  58.                                   (-1.4999999999999998, -2.5),
  59.                                   (-1.9999999999999998, -2.0)],
  60.                                  [(-1.0, 0.99999999999999989),
  61.                                   (-1.5000000000000002, 2.5),
  62.                                   (-2.0, 1.9999999999999998)],
  63.                                  [(0.99999999999999989, 1.0),
  64.                                   (1.4999999999999998, 2.5),
  65.                                   (1.9999999999999998, 2.0)]]
  66.                 """ circle x ( [points] x radius) """
  67.                 circles = [
  68.                                 [[(0.0, 0.0)], 2],
  69.                                 [[(2.2000000000000002, 1.3471114790620887e-16)], 1]
  70.                                 ]
  71.                 """ trail """
  72.                 glColor3f(0.5, 0.3, 0.1)
  73.                 map(lambda x: self.draw_shape(GL_LINES, x), self.trail)
  74.  
  75.                 glColor3f(0.0, 0.8, 0.0)
  76.  
  77.                 """ Convert points into matricies and multiply """
  78.                 def rotate(x):
  79.                         C = matrix([
  80.                                 [cos(radians(self.t)), -sin(radians(self.t)), 0],
  81.                                 [sin(radians(self.t)), cos(radians(self.t)), 0],
  82.                                 [0,0,1]
  83.                                 ])
  84.                         p = matrix(list(x)+[0])
  85.                         p.shape = (3,1)
  86.                         p = C*p
  87.                         p = p.tolist()
  88.                         return (p[0][0],p[1][0])
  89.  
  90.                 polys = map(lambda x: map(lambda t: rotate(t), x), polys)
  91.                 circles = map(lambda x: [map(lambda y: rotate(y), x[0])] + [x[1]], circles)
  92.  
  93.                 """ Movement: """
  94.                 move = lambda y: map(lambda x:(x[0]+self.x, x[1]+self.y), y)
  95.                 polys = map(lambda t: move(t), polys)
  96.                 circles = map(lambda x: [move(x[0]), x[1]], circles)
  97.  
  98.                 """ Drawing: """
  99.                 map(lambda x: self.draw_shape(GL_POLYGON, x), polys)
  100.                 map(lambda x: self.circle(x[0][0][0], x[0][0][1], x[1]), circles)
  101.        
  102.         """ Abstract shape drawing """
  103.         def draw_shape(self,id,shape):
  104.                 translated = map(lambda x: x, shape)
  105.                 glBegin(id)
  106.                 map(lambda x: glVertex2f(x[0], x[1]), translated)
  107.                 glEnd()
  108.        
  109.         """ Draws a circle at x,y, radius in size. """
  110.         def circle(self, x, y, radius):
  111.                 self.draw_circle(x,y,radius,0.5)
  112.        
  113.         """
  114.         Draws a circle at x,y, radius in size.
  115.  
  116.         Taken from: http://www.allegro.cc/forums/thread/588625
  117.         """
  118.         def draw_circle(self,x,y,radius,accuracy):
  119.                 glDisable(GL_TEXTURE_2D)
  120.                 da = min( (2.0 * math.asin(1.0/radius)/accuracy), 0.5)
  121.                 glBegin(GL_TRIANGLE_FAN)
  122.                 glVertex2d(x,y)
  123.                 t = arange(0,2*pi,da)
  124.                 map(lambda a: glVertex2d(x + cos(a) * radius, y + sin(a) * radius), t)
  125.                 glVertex2d(x + radius, y)
  126.                 glEnd()
  127.  
  128. sys.setrecursionlimit(100000000)
  129.  
  130. class TurtleClient:
  131.         framerate = 33
  132.  
  133.         def __init__(self):
  134.                 self.main()
  135.        
  136.         def display(self):
  137.                 glClear (GL_COLOR_BUFFER_BIT)
  138.                 glPushMatrix()
  139.  
  140.                 self.turt.draw()
  141.  
  142.                 glRectf(29, 29, 26, 26)
  143.                 self.turt2.draw()
  144.  
  145.                 self.turt3.draw()
  146.  
  147.                 glPopMatrix()
  148.                 glutSwapBuffers()
  149.  
  150.         def turt2_timer(self, e):
  151.                 if self.turt2.x >= 32 and self.turt2.y >= 32 or self.turt2.x >= 32 and self.turt2.y <= 23 or self.turt2.x <= 23 and self.turt2.y <= 23 or self.turt2.x <= 23 and self.turt2.y >= 32:
  152.                         self.turt2.right(90)
  153.                 self.turt2.forward(1)
  154.                 glutTimerFunc(self.framerate, self.turt2_timer, 0)
  155.  
  156.         def turt3_timer(self, e):
  157.                 p0 = self.t3_p
  158.                 count = self.t3_c
  159.                 v1 = self.v[random.randint(0, 3)]
  160.                 if count==0:
  161.                         self.turt3_poop_dot_at(v1[0] + 5, v1[1]+5)
  162.                         return
  163.                 p1 = ( (p0[0] + v1[0])/2, (p0[1] + v1[1])/2 )
  164.                 self.turt3_poop_dot_at(p1[0], p1[1])
  165.                 self.t3_c = count-1
  166.                 glutTimerFunc(self.framerate, self.turt3_timer, 0)
  167.        
  168.         def turt3_poop_dot_at(self, x, y):
  169.                 loc = self.turt3.get_loc()
  170.                 direction = self.turt3.get_dir()
  171.  
  172.                 x0, y0 = x-loc[0], y-loc[1]
  173.                 d = sqrt(x0**2 + y0**2)
  174.                 if x0 == 0: angle = 0
  175.                 else: angle = degrees(arctan(y0 / x0))
  176.                 if x0 < 0 and y0 > 0: angle += 180
  177.                 if x0 < 0 and y0 < 0: angle += 180
  178.                 if x0 > 0 and y0 < 0: angle += 360
  179.                 t = angle-direction
  180.  
  181.                 self.turt3.left(t)
  182.                 self.turt3.forward(d)
  183.                 self.turt3.flip_pen()
  184.                 self.turt3.forward(0.4)
  185.                 self.turt3.flip_pen()
  186.                 self.t3_p = self.turt3.get_loc()
  187.  
  188.         def reshape(self, w, h):
  189.                 glViewport (0, 0, w, h)
  190.                 glMatrixMode(GL_PROJECTION)
  191.                 glLoadIdentity()
  192.                 glOrtho(-40.0, 40.0, -40.0, 40.0, -1.0, 1.0)
  193.                 glMatrixMode(GL_MODELVIEW)
  194.                 glLoadIdentity()
  195.  
  196.         def keyboard(self, key, x, y):
  197.                 if (key == 'q' or key == 'Q'): exit(0)
  198.        
  199.         def special_key(self, key, x, y):
  200.                 if key == GLUT_KEY_LEFT: self.turt.left(10)
  201.                 if key == GLUT_KEY_RIGHT: self.turt.right(10)
  202.                 if key == GLUT_KEY_UP: self.turt.forward(2)
  203.                 if key == GLUT_KEY_DOWN: self.turt.flip_pen()
  204.  
  205.         def main(self):
  206.                 self.turt = Turtle(-16, 16, 0)
  207.                 self.turt2 = Turtle(32, 32, 0)
  208.                 self.turt3 = Turtle(-30, -30, 0)
  209.                 self.t3_p = (-30, -30)
  210.                 self.t3_c = 600
  211.                 self.v = [(-30, -30), (-15,0), (0,-30)]
  212.                 glutInit()
  213.                 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB)
  214.                 glutInitWindowSize (500, 500)
  215.                 glutInitWindowPosition (100, 100)
  216.                 glutCreateWindow("o hai!")
  217.                 glClearColor(0.0, 0.0, 0.0, 0.0)
  218.                 glShadeModel(GL_FLAT)
  219.                 glutDisplayFunc(self.display)
  220.                 glutReshapeFunc(self.reshape)
  221.                 glutKeyboardFunc(self.keyboard)
  222.                 glutSpecialFunc(self.special_key)
  223.                 glutTimerFunc(self.framerate, self.turt2_timer, 0)
  224.                 glutTimerFunc(self.framerate, self.turt3_timer, 0)
  225.                 glutMainLoop()
  226.  
  227. tc = TurtleClient()