Skip to content

Torxed/pyglet2d

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyglet2d

Create 2D/GUI by wrapping pyglet functionality as transparent as possible.
Create buttons, player objects or tiled backgrounds easily. A generic 2D/GUI framework.

Documentation

Some useful quick links below:

2d objects:

Below are helper functions:

Usage example

Here's a quick demo of what you can do:
(for more examples, see test.py)

Video example (from screenshot below) screenshot

from pyglet_gui import *
from random import randint

class circle(genericShape):
	def __init__(self, *args, **kwargs):
		## TODO: Add batch
		super(circle, self).__init__('GL_TRIANGLES', *args, **kwargs)

	def update(self):
		self.move(randint(-1, 1), randint(-1, 1))

class window(windowWrapper):
	def __init__(self):
		super(window, self).__init__(vsync=True, fps=True)

		self.add_sprite('circle', circle(x=int(self.width/2), y=int(self.height/2), alpha=0))
		self.add_sprite('webimage', resources.image_from_url('https://hvornum.se/favicon.ico', x=self.width-64, y=self.height-64))

W = window()
W.run()

Helper functions

resources.image_from_url(url, x=0, y=0)

This will fetch, load a image from an URL.
This image will be placed inside a ImageObject and then placed in a genericSprite.

sprite = genericSprite(texture=None)
sprite.move(5, 0) # Move 5 pixels in the X asis to the right
sprite.rotate(90) # Rotate the sprite 90deg (0-360) from current rotation

The genericSprite is a extension of pyglet.sprite.Sprite which just adds additional functionality as transparently as possible. things such as rotation, movement and interaction (click, hover, hover out etc).

The class takes a number of different textures as well,

  • str will be parsed as a file location, and loaded as a image resource and then convert it to a pyglet.sprite.Sprite()
  • bytes will be parsed as PNG color data as-is and treated as a already existing image resource
  • ImageObject will assume it's a pre-loaded image resource and do nothing
  • None will attempt to generate a PNG assuming width and height is supplied to the genericSprite, and load the generated PNG as a image resource.
  • None and no width/height is supplied, or _no_backdrop=True is set. _no_backdrop means we want no background to be generated or rendered for this object. In this case, the sprite will use a placeholder self._texture to satisfy pyglet's internals and not instanciate the pyglet.sprite.Sprite() but keep all the functionality of the sprite, such as changing batch, moving, rotating etc.
genericInteractive(label="", width=120, height=20)

genericInteractive is a extension of genericSprite with some additional options such as themes.
It will try to apply a general set of themes to the object, and is a simplification to create buttons etc.

class themedObject(theme='default')

Inheriting this class will try to wrap whatever the parent object is, in a theme of sorts.
This means adding borders, on_hover events and possibly backdrops or images as the background. Much like CSS to already existing elements in HTML.

genericShape(shapeType='GL_TRIANGLES')

Generic shape handler, create GL_ vertices quicker and easier for simpler things.
Won't handle advanced shapes but will let you work with simpler objects (circles, triangles, squares) as if they were sprites.

Camera movement

Camera movements are built-in to the window. Simply using self.camera will get you what you want.

class window(windowWrapper):
	def __init__(self):
		super(window, self).__init__(vsync=False, fps=True)

		self.add_sprite('button', genericInteractive(label='Click me', x=64, y=self.height-64))

	def key_W(self, symbol, event, modifiers, *args, **kwargs):
		self.camera.move(0, 1)

This will move the camera whenever W is pressed.

About

2D/GUI framework for Pyglet. Create buttons, player objects or tiled backgrounds easily.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages