def DrawGLScene(): mb = mbhandler.queue.get() q = Vec3(0, -1, 0).rotateTo( Vec3(-mb['accelerometer']['x'], mb['accelerometer']['z'], mb['accelerometer']['y'])) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glTranslatef(0.0, 0.0, -6.0) glMultMatrixf(q.tomatrix()) # Draw Cube (multiple quads) glBegin(GL_QUADS) glColor3f(0.0, 1.0, 0.0) # Green, +ve y-axis glVertex3f(1.0, 1.0, -1.0) glVertex3f(-1.0, 1.0, -1.0) glVertex3f(-1.0, 1.0, 1.0) glVertex3f(1.0, 1.0, 1.0) glColor3f(1.0, 0.0, 0.0) # Red, -ve y-axis glVertex3f(1.0, -1.0, 1.0) glVertex3f(-1.0, -1.0, 1.0) glVertex3f(-1.0, -1.0, -1.0) glVertex3f(1.0, -1.0, -1.0) glColor3f(0.0, 0.0, 1.0) # Blue, +ve z-axis, Identity matrix glVertex3f(1.0, 1.0, 1.0) glVertex3f(-1.0, 1.0, 1.0) glVertex3f(-1.0, -1.0, 1.0) glVertex3f(1.0, -1.0, 1.0) glColor3f(1.0, 1.0, 0.0) # Yellow, -ve z-axis glVertex3f(1.0, -1.0, -1.0) glVertex3f(-1.0, -1.0, -1.0) glVertex3f(-1.0, 1.0, -1.0) glVertex3f(1.0, 1.0, -1.0) glColor3f(0.0, 1.0, 1.0) # Cyan, -ve x-axis glVertex3f(-1.0, 1.0, 1.0) glVertex3f(-1.0, 1.0, -1.0) glVertex3f(-1.0, -1.0, -1.0) glVertex3f(-1.0, -1.0, 1.0) glColor3f(1.0, 0.0, 1.0) # Magenta, +ve x-axis glVertex3f(1.0, 1.0, -1.0) glVertex3f(1.0, 1.0, 1.0) glVertex3f(1.0, -1.0, 1.0) glVertex3f(1.0, -1.0, -1.0) glEnd() glutSwapBuffers()
def mouse_drag(self, x, y, dx, dy, button, modifiers): if pyglet.window.mouse.LEFT == button: if self.enemy: if modifiers & pyglet.window.key.MOD_ALT: self.enemy.hold() self.enemy.fire_rate = max( 0.001, self.enemy.fire_rate + dy * 0.03) self.enemy.fire_rate = min(1.5, self.enemy.fire_rate) self.enemy.fire_later() elif modifiers & pyglet.window.key.MOD_CTRL: self.enemy.orientation = self.enemy.position - Vec3(x, y) else: self.enemy.position = Vec3(x, y)
def _get_closest(self, x, y): closest = None dist = self.SPLINEGRABDIST d = dist + 1 for guide in self.spline.guides: try: d = Vec3(x, y).get_distance(Vec3(guide.x, guide.y)) except ValueError, e: if __debug__: print e.message if __debug__: print x, y, guide.x, guide.y continue if d < dist: dist = d closest = guide
def _get_closest(self, x, y): """ returns either a Vec3 or None""" closest = None dist = self.POINTGRABDIST d = dist + 1 for point in self.obstacle.vertices: try: d = Vec3(x, y).get_distance(Vec3(point.x, point.y)) except ValueError, e: if __debug__: print e.message if __debug__: print x, y, point.x, point.y continue if d < dist: dist = d closest = point
def __init__(self, app, level_number=0): super(EditorScene, self).__init__(app) self.states = [] self.splines = [] self.flocks = [] self.obstacles = [] self.enemies = [] self.triggers = [] self.bullets = [] self.statusline = None self.status_changer = None self.set_statusline("Now editing level %d." % level_number) # Camera's in-world cordinate self.campos = Vec3(0.0, 0.0) self.bg_image = pyglet.image.load("data/textures/background.png") self.bg_texture = pyglet.image.TileableTexture.create_for_image( self.bg_image) self.level_number = level_number self.textures = [] for root, dirs, files in os.walk('data/textures/level'): for name in files: filename = os.path.join(root, name) if filename.lower().endswith('.png'): self.textures.append( (filename, pyglet.image.load(filename).texture)) self.current_texture = self.textures[0] self.texture_batch = pyglet.graphics.Batch() self.load_level()
def fire(self, *args, **kwargs): self.hold() pyglet.clock.schedule_once(self.fire, self.fire_rate) if self.position.get_distanceSQ(self.scene.campos + Vec3(400, 300)) < 449 * 449: b = Bullet(self.scene, self.position.clone(), self.orientation) self.scene.add_dynamic(b)
def update_points2(self): self.position = sum(self.vertices, Vec3(0, 0)) / float( len(self.vertices)) if len(self.vertices) > 2: self.bounding_radius = 0 for idx, vertex in enumerate(self.vertices): v = (vertex - self.position).length if v > self.bounding_radius: self.bounding_radius = v self.update_uvmap() gpoints = [] points = [] colors = [] for vertex in self.vertices: gpoints.extend([vertex.x, vertex.y]) screen = vertex + self.scene.campos colors.extend([screen.x / 400.0, .6, screen.y / 300.0]) #self.gvl.resize(len(gpoints) / 2) #self.gvl.vertices = gpoints #self.gvl.colors = colors tpoints = [] tuvpoints = [] tcolors = [] for idx, vertex in enumerate(self.vertices): if idx < 2: continue else: tpoints.extend([ self.vertices[0].x, self.vertices[0].y, self.vertices[idx - 1].x, self.vertices[idx - 1].y, self.vertices[idx].x, self.vertices[idx].y ]) tuvpoints.extend([ self.uvmap[0][0], self.uvmap[0][1], self.uvmap[idx - 1][0], self.uvmap[idx - 1][1], self.uvmap[idx][0], self.uvmap[idx][1], ]) tcolors.extend([ colors[0], colors[1], colors[2], colors[(idx - 1) * 2], colors[((idx - 1) * 2) + 1], colors[((idx - 1) * 2) + 2], colors[idx * 2], colors[idx * 2 + 1], colors[idx * 2 + 2] ]) #print "Vertices: ", len(self.vertices), self.vertices #print "TPoints: ", len(tpoints), tpoints #print "TColors: ", len(tcolors), tcolors self.pvl.resize(len(tpoints) / 2) self.pvl.vertices = tpoints self.pvl.tex_coords = tuvpoints self.pvl.colors = tcolors return True
def key_press(self, key, modifiers): if pyglet.window.key.ENTER == key: self.editor.set_statusline("Left enemy-mode.", temp=True) self.editor.pop_state() elif pyglet.window.key.N == key: self.editor.enemies.append( enemies.Enemy(self.editor, 1.0, self.editor.lastmousepos, Vec3(0, 0)))
def enter(self): if self.trigger: self.lookat(self.trigger.position) else: self.trigger = lib.obstacle.Trigger( self.editor.campos + Vec3(400, 300), 100) self.guide = None self.editor.set_statusline( "Entered triger-edit-mode. New trigger created.", temp=True)
def on_mouse_press(self, x, y, button, modifiers): if __debug__: print "Campos:", self.campos if __debug__: print "Raw X,Y:", x, y x += self.campos.x y += self.campos.y if __debug__: print "Adjusted X,Y:", x, y self.lastmousepos = Vec3(x, y) if self.states: self.states[-1].mouse_press(x, y, button, modifiers)
def add_vertex(self, x, y): # first three point dont need check, they always form a valid convec triangle vertex = Vec3(x, y) self.vertices.append(vertex) if not self.check_vertex_valid(): self.vertices.remove(vertex) return False self.update_points2() return True
def mouse_drag(self, x, y, dx, dy, button, modifiers): if pyglet.window.mouse.LEFT == button: if self.trigger: self.trigger.position.x = x self.trigger.position.y = y self.trigger.update_points() elif pyglet.window.mouse.RIGHT == button: if self.trigger: self.trigger.bounding_radius = (self.trigger.position - Vec3(x, y)).length self.trigger.update_points()
def on_mouse_drag(self, x, y, dx, dy, button, modifiers): x += self.campos.x y += self.campos.y self.lastmousepos = Vec3(x, y) # Builtin if pyglet.window.mouse.MIDDLE == button: rate = 1.0 if modifiers & pyglet.window.key.MOD_SHIFT: rate = self.DRAGSPEEDMOD self.pan_camera(-dx, -dy, rate=rate) elif self.states: self.states[-1].mouse_drag(x, y, dx, dy, button, modifiers)
def mouse_release(self, x, y, button, modifiers): if pyglet.window.mouse.LEFT == button: if self.enemy: self.enemy.hold() if not modifiers: self.enemy.position = Vec3(x, y) self.enemy = None elif pyglet.window.mouse.RIGHT == button: enemy = self._get_closest(x, y) if enemy: self.editor.enemies.remove(enemy) self.enemy = None
def _get_closest(self, x, y): closest = None dist = self.ENEMYGRABDIST d = dist + 1 for enemy in self.editor.enemies: try: d = Vec3(x, y).get_distance(enemy.position) except ValueError, e: if __debug__: print e.message if __debug__: print x, y, enemy.position.x, enemy.position.y continue if d < dist: dist = d closest = enemy
def update_points(self): # TODO: This should take one point (the changed point) and only update its neighbors. self.position = sum(self.vertices, Vec3(0, 0)) / float( len(self.vertices)) if len(self.vertices) > 2: self.bounding_radius = 0 for idx, vertex in enumerate(self.vertices): v = (vertex - self.position).length if v > self.bounding_radius: self.bounding_radius = v points = [] gpoints = [] colors = [] #for guide in reversed(self.vertices): for guide in self.vertices: gpoints.extend([guide.x, guide.y]) screen = guide + self.scene.campos colors.extend([screen.x / 400.0, .6, screen.y / 300.0]) self.gvl.resize(len(gpoints) / 2) self.gvl.vertices = gpoints self.gvl.colors = colors self.gvlopen.resize(len(gpoints) / 2) self.gvlopen.vertices = gpoints self.gvlopen.colors = colors points = gpoints if points: color = [1., 1., 1.] #points.extend([gpoints[-2], gpoints[-1]]) self.pvl.resize(len(points) / 2) self.pvl.vertices = points self.pvl.colors = color * (len(points) / 2) self.pvl.tex_coords = self.update_uvmap() self.pvlopen.resize(len(points) / 2) self.pvlopen.vertices = points self.pvlopen.tex_coords = self.uvmap self.pvlopen.colors = color * (len(points) / 2) return True
import os import pyglet from pyglet.gl import * import cerealizer import lib from lib import obstacle from lib import enemies from lib import spline from lib.vectors import Vec3 CAMOFF = Vec3(400, 300) #------------------------------------------------------------------------------ class EditorState(object): def __init__(self, editor): self.editor = editor def enter(self): pass def exit(self): pass def update(self, dt): pass
def mouse_press(self, x, y, button, modifiers): if pyglet.window.mouse.LEFT == button: self.point2 = Vec3(x, y)
def pan_camera(self, dx, dy, rate=1.0): self.campos += rate * (Vec3(dx, dy)) self.bg_texture.anchor_x += self.DRAGSPEEDBGMOD * rate * dx self.bg_texture.anchor_y += self.DRAGSPEEDBGMOD * rate * dy
def update(self, dt): if self.position.get_distanceSQ(self.scene.campos + Vec3(400, 300)) > 449 * 449: self.destroy() super(Bullet, self).update(dt)
def on_mouse_motion(self, x, y, dx, dy): x += self.campos.x y += self.campos.y self.lastmousepos = Vec3(x, y)
def on_mouse_release(self, x, y, button, modifiers): x += self.campos.x y += self.campos.y self.lastmousepos = Vec3(x, y) if self.states: self.states[-1].mouse_release(x, y, button, modifiers)