Skip to content

Instantly share code, notes, and snippets.

@Torxed
Created June 12, 2016 11:53
Show Gist options
  • Save Torxed/932eda0ffa739dd4e6a0bb051a9a63a4 to your computer and use it in GitHub Desktop.
Save Torxed/932eda0ffa739dd4e6a0bb051a9a63a4 to your computer and use it in GitHub Desktop.
Working custom pyglet class.
import pyglet, math
from pyglet.gl import *
key = pyglet.window.key
from pyglet import app
from pyglet.libs.win32 import _user32
from pyglet.libs.win32.constants import *
from pyglet.libs.win32.winkey import *
from pyglet.libs.win32.types import *
#window = pyglet.window.Window(width=800, height=600)
class main(pyglet.window.Window):
def __init__ (self):
super(main, self).__init__(800, 800, fullscreen = False)
self.x, self.y = 0, 0
self.alive = 1
width, height = 800,600
self.bg = pyglet.image.load('./grid.png')
self.bg.scale = max(max(width, self.bg.width)/min(width, self.bg.width), max(self.bg.height, height)/min(self.bg.height, height))
self.bg.texture.width = width
self.bg.texture.height = height
self.bg.width = width
self.bg.height = height
self.pos = 0
self.lines = [[114,23], [361,122], [134,222], [250,432], [323,133], [145,155]]
def on_draw(self):
self.render()
def on_close(self):
self.alive = 0
def draw_line(self, xy, dxy, color=(1, 0.2, 0.2, 1)):
glColor4f(color[0], color[1], color[2], color[3])
glBegin(GL_LINES)
glVertex2f(xy[0], xy[1])
glVertex2f(dxy[0], dxy[1])
glEnd()
def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
pass
def on_mouse_press(self, x, y, button, modifiers):
pass
def on_mouse_release(self, x, y, button, modifiers):
pass
def on_mouse_motion(self, x, y, dx, dy):
pass
def on_key_release(self, symbol, modifiers):
pass
def on_key_press(self, symbol, modifiers):
if symbol == key.ESCAPE: # [ESC]
self.alive = 0
def render(self):
self.clear()
self.bg.blit(0,0)
prev = None
self.pos += 1
if self.pos >= len(self.lines): self.pos = 0
for line in self.lines[:self.pos]:
if prev is None:
prev = [line, ]
continue
self.draw_line(prev[-1], line)
prev.append(line)
self.flip()
def run(self):
while self.alive == 1:
self.render()
# -----------> This is key <----------
# This is what replaces pyglet.app.run()
# but is required for the GUI to not freeze
#
#app.platform_event_loop.start()
#self._allow_dispatch_event = True
#self.dispatch_pending_events()
#msg = MSG()
#while _user32.PeekMessageW(byref(msg), 0, 0, 0, PM_REMOVE):
# _user32.TranslateMessage(byref(msg))
# _user32.DispatchMessageW(byref(msg))
# break
#self._allow_dispatch_event = False
print('Dispatching')
event = self.dispatch_events()
print('Done')
x = main()
x.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment