Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. import pyglet
  2. import numpy as np
  3.  
  4. game_window = pyglet.window.Window(800, 600)
  5.  
  6. def Rotate(x,y,theta):
  7.     return x*np.cos(theta) - y*np.sin(theta), x*np.sin(theta) + y*np.cos(theta)
  8.  
  9. class Robot:
  10.     LENGTH = 10
  11.     WIDTH = 8
  12.     SENSOR_RADIUS = 50
  13.     VELOCITY = 2
  14.     ROTATION_VELOCITY = 0.1
  15.     def __init__(self):
  16.         self.x = 400
  17.         self.y = 300
  18.         self.theta = 0
  19.     def GetTriangle(self):
  20.         x0,y0 = Rotate(3*Robot.LENGTH/4, 0, self.theta)
  21.         x1,y1 = Rotate(-Robot.LENGTH/4, Robot.WIDTH/2, self.theta)
  22.         x2,y2 = Rotate(-Robot.LENGTH/4, -Robot.WIDTH/2, self.theta)
  23.         x0 += self.x
  24.         x1 += self.x
  25.         x2 += self.x
  26.         y0 += self.y
  27.         y1 += self.y
  28.         y2 += self.y
  29.         return [x0,y0,x1,y1,x2,y2]
  30.     def GetCircle(self, iterations = 200):
  31.         points = []
  32.         x = Robot.SENSOR_RADIUS
  33.         y = 0
  34.         step = 2*np.pi/ iterations
  35.         theta = 0
  36.         for _ in range(iterations+1):
  37.             x,y = Rotate(x,y,theta)
  38.             points.append(self.x + x)
  39.             points.append(self.y + y)
  40.             theta += step
  41.         return points
  42.     def draw(self):
  43.         pyglet.gl.glColor3f(0.75,1.0,0.75)
  44.         pyglet.graphics.draw(3, pyglet.gl.GL_POLYGON, ('v2f',self.GetTriangle()))
  45.         pyglet.gl.glColor3f(0.0,1.0,0.0)
  46.         circle = self.GetCircle()
  47.         pyglet.graphics.draw(len(circle)/2, pyglet.gl.GL_POINTS, ('v2f',circle))
  48.         for landmark in Landmark.landmarks:
  49.             dx = self.x - landmark.x
  50.             dy = self.y - landmark.y
  51.             if dx*dx + dy*dy < Robot.SENSOR_RADIUS*Robot.SENSOR_RADIUS:
  52.                 pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2f',[self.x,self.y,landmark.x,landmark.y]))
  53.     def up(self):
  54.         dx,dy = Rotate(Robot.VELOCITY,0,self.theta)
  55.         self.x += dx
  56.         self.y += dy
  57.     def down(self):
  58.         dx,dy = Rotate(Robot.VELOCITY,0,self.theta)
  59.         self.x -= dx
  60.         self.y -= dy
  61.     def left(self):
  62.         self.theta += Robot.ROTATION_VELOCITY
  63.     def right(self):
  64.         self.theta -= Robot.ROTATION_VELOCITY
  65.  
  66.  
  67.  
  68. class Landmark:
  69.     RADIUS = 8.0
  70.     num_landmarks = 0
  71.     landmarks = []
  72.     def __init__(self, x, y):
  73.         self.x=x
  74.         self.y=y
  75.         self.index = Landmark.num_landmarks
  76.         Landmark.num_landmarks += 1
  77.         self.label = pyglet.text.Label(str(self.index),
  78.                                   font_name='Times New Roman',
  79.                                   font_size=12,
  80.                                   x=self.x, y=self.y,
  81.                                   anchor_x='center', anchor_y='center')
  82.         Landmark.landmarks.append(self)
  83.     def GetPoly(self):
  84.         return [self.x + Landmark.RADIUS, self.y, self.x, self.y + Landmark.RADIUS, self.x - Landmark.RADIUS, self.y, self.x, self.y - Landmark.RADIUS]
  85.     def draw(self):
  86.         pyglet.gl.glColor3f(0.5,0,0)
  87.         pyglet.graphics.draw(4, pyglet.gl.GL_POLYGON, ('v2f',self.GetPoly()))
  88.         self.label.draw()
  89.  
  90. robot = Robot()
  91.  
  92. def update(dt):
  93.     game_window.clear()
  94.     robot.draw()
  95.     for landmark in Landmark.landmarks:
  96.         landmark.draw()
  97.     if 'w' in keys and keys['w']:
  98.         robot.up()
  99.     if 'a' in keys and keys['a']:
  100.         robot.left()
  101.     if 's' in keys and keys['s']:
  102.         robot.down()
  103.     if 'd' in keys and keys['d']:
  104.         robot.right()
  105.  
  106. keys = {}
  107. @game_window.event
  108. def on_key_press(symbol, modifiers):
  109.     keys[chr(symbol % 256)] = True
  110. @game_window.event
  111. def on_key_release(symbol, modifiers):
  112.     keys[chr(symbol % 256)] = False
  113.  
  114. if __name__ == '__main__':
  115.     Landmark(400,325)
  116.     Landmark(400,275)
  117.     Landmark(450,325)
  118.     Landmark(450,275)
  119.     Landmark(500,325)
  120.     Landmark(500,275)
  121.     Landmark(550,325)
  122.     Landmark(550,275)
  123.     Landmark(600,325)
  124.     Landmark(600,275)
  125.     Landmark(650,300)
  126.     Landmark(650,250)
  127.     Landmark(650,200)
  128.     Landmark(600,225)
  129.     Landmark(600,175)
  130.     Landmark(550,225)
  131.     Landmark(550,175)
  132.     Landmark(500,225)
  133.     Landmark(500,175)
  134.     Landmark(450,225)
  135.     Landmark(450,175)
  136.     Landmark(400,225)
  137.     Landmark(400,175)
  138.  
  139.     pyglet.clock.schedule_interval(update, 0.01)
  140.     pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement