Created
May 8, 2014 11:39
-
-
Save Torxed/a1f3384d4a718c8ad3b7 to your computer and use it in GitHub Desktop.
Small Pyglet class based graphical application that rotates an image
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
import pyglet | |
from pyglet.gl import * | |
from collections import OrderedDict | |
from time import time | |
from os.path import abspath | |
class House(pyglet.sprite.Sprite): | |
def __init__(self): | |
self.texture = pyglet.image.load(abspath('./image.png')) | |
super(House, self).__init__(self.texture) | |
self.x = 0 | |
self.y = 0 | |
self.rotation = 0 | |
self.name = 'house' | |
self.anchor = 'center' | |
def swap_image(self, image): | |
self.texture = pyglet.image.load(abspath(image)) | |
self.image = self.texture | |
def rotate(self, deg): | |
self.image.anchor_x = self.image.width / 2 | |
self.image.anchor_y = self.image.height / 2 | |
self.rotation = self.rotation+deg | |
if self.anchor != 'center': | |
self.image.anchor_x = 0 | |
self.image.anchor_y = 0 | |
return True | |
def click(self): | |
print('Clicked:',self.name) | |
def work(self, *args): | |
pass | |
def click_check(self, x, y): | |
if x > self.x and x < (self.x + self.width): | |
if y > self.y and y < (self.y + self.height): | |
return self | |
def move(self, x, y): | |
if self.moveable: | |
self.x += x | |
self.y += y | |
def _draw(self): | |
self.draw() | |
class GUI(pyglet.window.Window): | |
def __init__(self): | |
super(GUI, self).__init__(640,340, caption='Test') | |
pyglet.gl.glClearColor(1, 1, 1, 1) | |
self.alive = True | |
self.batches = OrderedDict() | |
self.batches['apples'] = pyglet.graphics.Batch() | |
self.house = House() | |
self.framerate = 0, time() | |
self.count = 0 | |
def render(self, *args): | |
self.clear() | |
self.house.rotate(1) | |
self.house._draw() | |
if time() - self.framerate[1] > 1: | |
print('fps:',self.framerate[0]) | |
self.framerate = 0, time() | |
else: | |
# Not an optimal way to do it, but it will work. | |
self.framerate = self.framerate[0]+1, self.framerate[1] | |
self.flip() | |
def on_draw(self): | |
self.render() | |
def on_close(self): | |
self.alive = False | |
def on_key_press(self, symbol, modkey): | |
pass | |
def on_key_release(self, symbol, modkey): | |
pass | |
def on_mouse_release(self, x, y, button, modifiers): | |
pass | |
def on_mouse_press(self, x, y, button, modifiers): | |
self.count += 1 | |
with open('debug.log', 'w') as fh: | |
fh.write(str(self.count)) | |
def on_mouse_motion(self, x, y, dx, dy): | |
pass | |
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): | |
pass | |
def run(self): | |
while self.alive: | |
event = self.dispatch_events() | |
if event: | |
print(event) | |
self.render() | |
if __name__ == '__main__': | |
x = GUI() | |
pyglet.clock.set_fps_limit(60) | |
x.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment