Giter Club home page Giter Club logo

pyglet2d's Introduction

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.

pyglet2d's People

Contributors

torxed avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

paintedwolfpack

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.