Skip to content Skip to sidebar Skip to footer

Pyglet Script Works In Windows But Not In Osx

I am working in a pyglet script which displays an animation, it works as expected in Windows, but when I execute it in osx I only get a blank screen. I know it works because I've

Solution 1:

This is a (working) example code where flip is utilized:

import pyglet
from pyglet.gl import *
from math import radians, cos, sin, degrees, atan2
from time import time
from os.path import abspath

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_LINE_SMOOTH)
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE)
pyglet.options['audio'] = ('alsa', 'openal', 'silent')
key = pyglet.window.key

class BasicObject(pyglet.sprite.Sprite):
    def __init__(*args, **dictWars):
        if type(args) == tuple and len(args) == 2 and len(dictWars) == 0 and type(args[1]) == dict:
            args, dictWars = args

        self = args[0]
        self.name = args[1]
        self.render = True
        self.anchor = 'center'

        self.last_update = time()

        self.texture = pyglet.image.load(abspath(dictWars['texture']))
        super(BasicObject, self).__init__(self.texture)

        self.x, self.y = 0, 0
        self.rotation = 0

        if 'x' in dictWars:
            self.x = dictWars['x']
        if 'y' in dictWars:
            self.y = dictWars['y']

    def swap_image(self, image, filePath=True):
        if filePath:
            self.texture = pyglet.image.load(abspath(image))
        else:
            self.texture = image
        self.image = self.texture

    def draw_line(self, xy, dxy, color=(0.2, 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 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

    def click(self):
        print('Clicked:',self.name)

    def work(self, *args):
        if time() - self.last_update > 0.1:
            self.rotate(10)

    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 _draw(self):
        self.work()
        # self.draw_line((x,y), (dx, dy))
        self.draw()

class GUI(pyglet.window.Window):
    def __init__(self):
        super(GUI, self).__init__(640,340, caption='My app')
        self.alive = True
        self.keys_down = {}

        self.myImage = BasicObject('TestImage', texture='/path/to/texture.png')

    def render(self, *args):
        pyglet.gl.glClearColor(1, 1, 1, 1)
        self.clear()
        # .. This is where you draw your objects, for instance
        self.myImage._draw()
        self.flip()

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = False

    def on_key_press(self, symbol, modkey):
        self.keys_down[symbol] = time()

    def on_key_release(self, symbol, modkey):
        if symbol in self.keys_down:
            del(self.keys_down[symbol])

    def on_mouse_release(self, x, y, button, modifiers):
        pass

    def on_mouse_press(self, x, y, button, modifiers):
        print(button,'pressed',(x,y))


    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()

            for symbol in self.keys_down:
                if symbol == key.ESCAPE:
                    self.alive = None
                    break
                elif symbol == key.LEFT:
                    pass #Arrowkey Left
                elif symbol == key.RIGHT:
                    pass #Arrowkey Right
                elif symbol == key.UP:
                    pass #Arrowkey Up
                elif symbol == key.DOWN:
                    pass #Arrowkey Down
                elif symbol == 65515:
                    pass # Win key
                else:
                    print(symbol)
            self.render()


if __name__ == '__main__':
    x = GUI()
    pyglet.clock.set_fps_limit(120)
    x.run()

This is also the code i use in all my projects, something that's been evolving to fit my needs but also something that's proved to be working in most scenarios.


Post a Comment for "Pyglet Script Works In Windows But Not In Osx"