-
-
Save Artanis/430509 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from random import uniform | |
from pyglet import clock, font, image, window | |
from pyglet.gl import * | |
class Entity(object): | |
def __init__(self, id, size, x, y, rot): | |
self.id = id | |
self.size = size | |
self.x = x | |
self.y = y | |
self.rot = rot | |
def draw(self): | |
glLoadIdentity() | |
glTranslatef(self.x, self.y, 0.0) | |
glRotatef(self.rot, 0, 0, 1) | |
glScalef(self.size, self.size, 1.0) | |
glBegin(GL_TRIANGLES) | |
glColor4f(1.0, 0.0, 0.0, 0.0) | |
glVertex2f(0.0, 0.5) | |
glColor4f(0.0, 0.0, 1.0, 1.0) | |
glVertex2f(0.2, -0.5) | |
glColor4f(0.0, 0.0, 1.0, 1.0) | |
glVertex2f(-0.2, -0.5) | |
glEnd() | |
class World(object): | |
def __init__(self): | |
self.ents = {} | |
self.nextEntId = 0 | |
clock.schedule_interval(self.spawnEntity, 0.25) | |
def spawnEntity(self, dt): | |
size = uniform(1.0, 100.0) | |
x = uniform(-100.0, 100.0) | |
y = uniform(-100.0, 100.0) | |
rot = uniform(0.0, 360.0) | |
ent = Entity(self.nextEntId, size, x, y, rot) | |
self.ents[ent.id] = ent | |
self.nextEntId += 1 | |
return ent | |
def tick(self): | |
for ent in self.ents.values(): | |
ent.x += 0.1 | |
def draw(self): | |
glClear(GL_COLOR_BUFFER_BIT) | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
for ent in self.ents.values(): | |
ent.draw() | |
class Camera(object): | |
def __init__(self, win, x=0.0, y=0.0, rot=0.0, zoom=1.0): | |
self.win = win | |
self.x = x | |
self.y = y | |
self.rot = rot | |
self.zoom = zoom | |
def worldProjection(self): | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
widthRatio = self.win.width / self.win.height | |
gluOrtho2D( | |
-self.zoom * widthRatio, | |
self.zoom * widthRatio, | |
-self.zoom, | |
self.zoom) | |
def hudProjection(self): | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluOrtho2D(0, self.win.width, 0, self.win.height) | |
class Hud(object): | |
def __init__(self, win): | |
helv = font.load('Helvetica', win.width / 15.0) | |
self.text = font.Text( | |
helv, | |
'Hello, World!', | |
x=win.width / 2, | |
y=win.height / 2, | |
halign=font.Text.CENTER, | |
valign=font.Text.CENTER, | |
color=(1, 1, 1, 0.5), | |
) | |
self.fps = clock.ClockDisplay() | |
def draw(self): | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
self.text.draw() | |
self.fps.draw() | |
class App(object): | |
def __init__(self): | |
self.world = World() | |
self.win = window.Window(fullscreen=False, vsync=True) | |
self.camera = Camera(self.win, zoom=100.0) | |
self.hud = Hud(self.win) | |
clock.set_fps_limit(60) | |
def mainLoop(self): | |
while not self.win.has_exit: | |
self.win.dispatch_events() | |
self.world.tick() | |
self.camera.worldProjection() | |
self.world.draw() | |
self.camera.hudProjection() | |
self.hud.draw() | |
clock.tick() | |
self.win.flip() | |
app = App() | |
app.mainLoop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from random import uniform | |
import math | |
from pyglet import clock, window | |
from pyglet.gl import * | |
class Entity(object): | |
def __init__(self): | |
pass | |
def tick(self): | |
pass | |
def draw(self): | |
pass | |
class Block(Entity): | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
def draw(self): | |
glLoadIdentity() | |
glTranslatef(self.x, self.y, 0.0) | |
glBegin(GL_QUADS) | |
glColor4f(1.0, 1.0, 1.0, 1.0) | |
glVertex2f(0, 0) | |
glVertex2f(1, 0) | |
glVertex2f(1, -1) | |
glVertex2f(0, -1) | |
glEnd() | |
def poly_circle(radius=1, step=36, include_origin=False): | |
""" Generate points composing a circle of given radius and | |
precision. | |
""" | |
if include_origin is True: | |
yield (0, 0) | |
for angle in range(0,360,step): | |
radian = math.radians(angle) | |
yield (math.sin(radian) * radius, math.cos(radian) * radius) | |
class Player(Entity): | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
self.size = 1.0 | |
def tick(self): | |
# Gravity | |
#self.y -= 1 | |
pass | |
def draw(self): | |
glLoadIdentity() | |
glTranslatef(self.x - self.size/2.0, self.y - self.size/2.0, 0.0) | |
glBegin(GL_LINE_LOOP) | |
glColor4f(1.0, 0.0, 0.0, 1.0) | |
for x, y in poly_circle(self.size / 2.0, 5): | |
glVertex2f(x, y) | |
glEnd() | |
class Grid(Entity): | |
def __init__(self, x, y, size, window): | |
self.x = x | |
self.y = y | |
self.size = size | |
self.window = window | |
def draw(self): | |
glLoadIdentity() | |
glTranslatef(-self.size/2.0, -self.size/2.0, 0.0) | |
glBegin(GL_LINES) | |
glColor4f(0.25, 0.25, 0.25, 1.0) | |
for i in range(int(self.window.width/2)): | |
glVertex2f(i, -self.window.width/2.0) | |
glVertex2f(i, self.window.width/2.0) | |
glVertex2f(-i, -self.window.width/2.0) | |
glVertex2f(-i, self.window.width/2.0) | |
for i in range(int(self.window.height/2)): | |
glVertex2f(self.window.height/2.0, i) | |
glVertex2f(-self.window.height/2.0, i) | |
glVertex2f(self.window.height/2.0, -i) | |
glVertex2f(-self.window.height/2.0, -i) | |
glEnd() | |
class Universe(object): | |
def __init__(self): | |
self.entities = [] | |
def add_entity(self, entity): | |
self.entities.append(entity) | |
def tick(self): | |
for entity in self.entities: | |
entity.tick() | |
def draw(self): | |
glClear(GL_COLOR_BUFFER_BIT) | |
glMatrixMode(GL_MODELVIEW) | |
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) | |
glLoadIdentity() | |
for entity in self.entities: | |
entity.draw() | |
class Hud(object): | |
def __init__(self, window): | |
self.window = window | |
self.fps = clock.ClockDisplay() | |
def draw(self): | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
self.fps.draw() | |
class Camera(object): | |
def __init__(self, window, x=0.0, y=0.0, rotation=0.0, zoom=1.0): | |
self.x = x | |
self.y = y | |
self.window = window | |
self.rotation = rotation | |
self.zoom = zoom | |
def world_projection(self): | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
widthRatio = self.window.width / self.window.height | |
glOrtho( | |
-self.zoom * widthRatio, self.zoom * widthRatio, | |
-self.zoom, self.zoom, | |
-1, 1) | |
def hud_projection(self): | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
glOrtho( | |
0.0, self.window.height, | |
0.0, self.window.width, | |
-1, 1) | |
class App(object): | |
def __init__(self): | |
self.window = window.Window(fullscreen=False, vsync=True) | |
clock.set_fps_limit(60) | |
self.camera = Camera(self.window, 0.0, 0.0, 0.0, 10.0) | |
#self.hud = Hud(self.window) | |
self.universe = Universe() | |
self.universe.add_entity(Grid(0.0, 0.0, 4, self.window)) | |
for i in range(24): | |
self.universe.add_entity(Block(i-12, -8)) | |
self.universe.add_entity(Player(0,0)) | |
def main(self): | |
while not self.window.has_exit: | |
self.window.dispatch_events() | |
self.camera.world_projection() | |
self.universe.draw() | |
#self.camera.hud_projection() | |
#self.hud.draw() | |
clock.tick() | |
self.universe.tick() | |
self.window.flip() | |
if __name__ == "__main__": | |
app = App() | |
app.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment