def main_loop(self): self.collision_engine = CollisionEngine() ft = font.load('Arial', 28) fps_text = font.Text(ft, y=10) #for i in self.scene_objects: #count =30 while not self.has_exit: self.scene_objects[0].setRotation(10) #self.scene_objects[0].setRotation(20) #self.scene_objects[0].setRotation(30) #self.scene_objects[0].setRotation(90) #self.scene_objects[0].setRotation(100) #count =20 #if count >2:count=0 self.dispatch_events() self.clear() #pyglet.gl.glColor4f(1.0,0,0,1.0) glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable (GL_BLEND) #glEnable (GL_LINE_SMOOTH); glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE) glLineWidth (1) self.scene_objects[0].setPosition(self.mouse_position) collisions = self.collision_engine.processCollisions(self.scene_objects) ######Deal with collisions####### for i in collisions: pass print i[0].name + " hit "+i[1].name #i[1].positions.pop() #i[1].setPosition(i[1].positions[1]) debug = True debug = False for i in self.scene_objects: if debug == False: #i.setRotation(45) i.getSprite().draw() if debug == False: glDisable(GL_TEXTURE_2D) for j in i.getCollisionTriangles(): pyglet.gl.glColor4f(1.0,0,0,1.0) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (j[0][0]+i.pos[0],j[0][1]+i.pos[1],j[1][0]+i.pos[0],j[1][1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (j[1][0]+i.pos[0],j[1][1]+i.pos[1],j[2][0]+i.pos[0],j[2][1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (j[2][0]+i.pos[0],j[2][1]+i.pos[1],j[0][0]+i.pos[0],j[0][1]+i.pos[1]))) #draw bounding rectangle pyglet.gl.glColor4f(1.0,1.0,0,1.0) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (i.top_right[0]+i.pos[0],i.top_right[1]+i.pos[1],i.bottom_left[0]+i.pos[0],i.top_right[1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (i.top_right[0]+i.pos[0],i.top_right[1]+i.pos[1],i.top_right[0]+i.pos[0],i.bottom_left[1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (i.bottom_left[0]+i.pos[0],i.bottom_left[1]+i.pos[1],i.top_right[0]+i.pos[0],i.bottom_left[1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (i.bottom_left[0]+i.pos[0],i.bottom_left[1]+i.pos[1],i.bottom_left[0]+i.pos[0],i.top_right[1]+i.pos[1]))) glEnable(GL_TEXTURE_2D) clock.tick() fps_text.text = ("fps: %d") % (clock.get_fps()) fps_text.draw() self.flip()
class GameWindow(window.Window): def __init__(self, *args, **kwargs): window.Window.__init__(self, *args, **kwargs) self.scene_objects = [] self.mouse_position = (0,0) obj1 = SceneObject() obj1.addCollisionTriangle(0,0,40,40,40,0) obj1.addCollisionTriangle(40,40,70,60,20,90) obj1.addCollisionTriangle(0,0,40,40,40,0) #obj1.addCollisionTriangle(0,0,1,1,0,1) obj1.setPosition((100,100)) obj1.name = "Smiley Tile" obj1.loadImage("tile1.png") obj2 = SceneObject() obj2.addCollisionTriangle(1,1,600,1,600,20) obj2.addCollisionTriangle(1,1,1,20,600,20) obj2.addCollisionTriangle(1,400,600,400,600,380) obj2.addCollisionTriangle(1,400,1,380,600,380) obj2.addCollisionTriangle(0,0,0,400,20,400) obj2.addCollisionTriangle(0,0,20,0,20,400) obj2.addCollisionTriangle(600,0,600,400,580,400) obj2.addCollisionTriangle(600,0,580,0,580,400) obj2.setPosition((0,0)) obj2.name = "Green Border" obj2.loadImage("tile2.png") obj3 = SceneObject() obj3.addCollisionTriangle(0,0,40,80,0,80) obj3.addCollisionTriangle(0,0,40,80,40,0) obj3.setPosition((610,150)) obj3.name = "Red Spotty" obj3.loadImage("tile3.png") self.scene_objects.append(obj1) self.scene_objects.append(obj2) self.scene_objects.append(obj3) def main_loop(self): self.collision_engine = CollisionEngine() ft = font.load('Arial', 28) fps_text = font.Text(ft, y=10) #for i in self.scene_objects: #count =30 while not self.has_exit: self.scene_objects[0].setRotation(10) #self.scene_objects[0].setRotation(20) #self.scene_objects[0].setRotation(30) #self.scene_objects[0].setRotation(90) #self.scene_objects[0].setRotation(100) #count =20 #if count >2:count=0 self.dispatch_events() self.clear() #pyglet.gl.glColor4f(1.0,0,0,1.0) glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable (GL_BLEND) #glEnable (GL_LINE_SMOOTH); glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE) glLineWidth (1) self.scene_objects[0].setPosition(self.mouse_position) collisions = self.collision_engine.processCollisions(self.scene_objects) ######Deal with collisions####### for i in collisions: pass print i[0].name + " hit "+i[1].name #i[1].positions.pop() #i[1].setPosition(i[1].positions[1]) debug = True debug = False for i in self.scene_objects: if debug == False: #i.setRotation(45) i.getSprite().draw() if debug == False: glDisable(GL_TEXTURE_2D) for j in i.getCollisionTriangles(): pyglet.gl.glColor4f(1.0,0,0,1.0) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (j[0][0]+i.pos[0],j[0][1]+i.pos[1],j[1][0]+i.pos[0],j[1][1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (j[1][0]+i.pos[0],j[1][1]+i.pos[1],j[2][0]+i.pos[0],j[2][1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (j[2][0]+i.pos[0],j[2][1]+i.pos[1],j[0][0]+i.pos[0],j[0][1]+i.pos[1]))) #draw bounding rectangle pyglet.gl.glColor4f(1.0,1.0,0,1.0) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (i.top_right[0]+i.pos[0],i.top_right[1]+i.pos[1],i.bottom_left[0]+i.pos[0],i.top_right[1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (i.top_right[0]+i.pos[0],i.top_right[1]+i.pos[1],i.top_right[0]+i.pos[0],i.bottom_left[1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (i.bottom_left[0]+i.pos[0],i.bottom_left[1]+i.pos[1],i.top_right[0]+i.pos[0],i.bottom_left[1]+i.pos[1]))) pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (i.bottom_left[0]+i.pos[0],i.bottom_left[1]+i.pos[1],i.bottom_left[0]+i.pos[0],i.top_right[1]+i.pos[1]))) glEnable(GL_TEXTURE_2D) clock.tick() fps_text.text = ("fps: %d") % (clock.get_fps()) fps_text.draw() self.flip() def on_mouse_motion(self,x, y, dx, dy): self.mouse_position = (x,y)