Skip to content

Python Game Engine - Make games with Pyxel using Pygame but with less boilerplate code!

Notifications You must be signed in to change notification settings

lullaby6/Pyxes.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pyxes

Pyxes is a library that adds functionality and structure to pygame, you can still use all your pygame code but with less of a lot boilerplate code!

Index


Game

Initializating game

from pyxes import Game

game = Game()

game.run()

Default props

game = Game(width = 640, height = 480, bg_color = Colors['black'], bg_alpha = 255, title = 'Title', cursor = True, fps = 60, quit_on_escape = False, default_scene = Scene())

Title

print(self.title)
game.set_title('My Game Title')

Icon

game.set_icon('icon.png')
print(self.icon_path) # icon file path
print(self.icon_image) # icon pygame.image

Size

print(game.width, game.height)
game.set_size(1280, 720)

Background Color

game.bg_color = (123, 255, 100) # default is (0, 0, 0) (black)

Background Color Alpha

game.bg_alpha = 128 # default is 255

Pause

All update, drawing and event functions will be paused, except Scene events.

print(game.pause)
game.set_pause(True) # can be True or False, by default is False
game.toggle_pause()

Fullscreen

print(game.fullscreen)
game.set_fullscreen(False) # can be True or False, by default is True
game.toggle_fullscreen()

FPS

print(game.fps)
game.set_fps(30)

Cursor visibility

print(self.cursor)
game.set_cursor_visibility(False) # can be True or False, by default is True
game.toggle_cursor_visibility()
game.hidde_cursor()
game.show_cursor()

Delta Time

print(game.delta_time)

Screenshot

game.screenshot() # default screenshots directory is 'screenshots/'
game.screenshot('my_screenshots')
game.screenshot(os.path.join(__file__, 'my_screenshots'))

Quit on Escape

game.quit_on_escape = True # can be True or False, by default is False

Custom Event

Example

The 'print_name' function is executed for the current scene and all game objects actives.

game.custom_event('print_name', 'Lullaby')

class Player (GameObject):
    def print_name(self, name):
        print(f'My name is: {name}')

Scenes

Creating a scene

from pyxes import Game, Scene

class MainScene (Scene):
    def load(self):
        print('main scene loaded!')

game = Game()

game.set_scene('main', MainScene())

game.run()

Scene methods

my_scene = Scene() # instancing a base scene

game.add_scene('scene_name', my_scene) # add scene to game.scenes
game.change_scene('scene_name') # change the active/current scene
game.set_scene('scene_name', my_scene) # a shortcut for add_scene and change_scene
game.get_active_scene()
game.remove_scene('scene_name') # delete a scene
game.reset_scene() # reset current scene

Scene default props

my_scene = Scene(ignore_pause = False)

Extend Scene class

class MainScene (Scene):
    def __init__(self):
        super().__init__()

    def load(self):
        print('main scene loaded!')

    def update(self)
        print('main scene loop.', self.game.delta_time)

Reset scene

active_scene = game.get_active_scene()
active_scene.reset()

Sort Game Objects by Z-Index

This function will be executed automatically when a GameObject is created.

active_scene = game.get_active_scene()
active_scene.sort_game_objects_by_z()

Scene ignore pause

my_scene = Scene(ignore_pause = True)

or

class MyScene (Scene):
    def load(self):
        self.ignore_pause = True

my_scene = MyScene()

GameObject

Creating a GameObject

from pyxes import Game, Scene, GameObject

class Player (GameObject):
    def key_down(self, event, keyname):
        if keyname == 'w': self.y -= 10
        if keyname == 'a': self.x -= 10
        if keyname == 's': self.y += 10
        if keyname == 'd': self.x += 10

class MainScene (Scene):
    def load(self):
        self.add_game_object('player', Player())

game = Game(default_scene=MainScene())

game.run()

GameObject default props

MyObject = GameObject(x = 0, y = 0, z = 0, width = 10, height = 10, color = Colors['white'], alpha = 255, tags = [], gui = False, ignore_pause = False, active = True, visible = True)

game.get_active_scene().instant_game_object(MyObject)

GameObject methods

active_scene = game.get_active_scene()
player = GameObject()

active_scene.add_game_object('player', player) # create a GameObject with 'player' name
active_scene.instant_game_object(player) # create a GameObject without specifying a name
active_scene.remove_game_object('player') # remove GameObject by name
player = active_scene.get_game_object('player')

Extend GameObject class

class MyGameObject (GameObject):
    def __init__(self):
        super().__init__()

    def load(self):
        print(self.id, 'my-gameobject loaded!')

    def update(self)
        print(self.name, 'my-gameobject scene loop.', self.game.delta_time)

Coordinates

player = GameObject(x=0, y=0, z=0)

or

player.x = 25
player.y = 25
player.set_z(1)

Color

player = GameObject(color=(255, 255, 255))

or

player.color = Colors['red']

Alpha color

player = GameObject(aplha=128)

or

player.alpha = 128

GameObject size

player = GameObject(width=25, height=25)

or

player.set_size(25, 25) # width, height

or

player.width = 50
player.height = 25

GUI

GameObjects has 'gui' enabled will be fixed in the window.

GUI = GameObject(width=200, height=50, gui=True)

Game Object Reset

player.reset()

Tags

active_scene.instant_game_object(GameObject(tags=['enemy']))

enemies = active_scene.get_game_objects_by_tag('enemy')

Tags methods

player.add_tag('player')
player.remove_tag('player')

if player.has_tag('player'):
    print(player, 'is a player!')

print(player.get_tags())

Visible

player.visible = False # by default is True

Active

player.active = False # by default is True

GameObject ignore pause

player.ignore_pause = True # by default is False

Z-Index

print(player.z)
player.set_z(10)

Events

All events

Events for Scenes and GameObjects.

  • key_down
  • key_up
  • mouse_down
  • mouse_up
  • mouse_motion
  • mouse_wheel
  • joy_axis_motion
  • joy_button_down
  • joy_button_up
  • quit
  • fullscreen
  • resize
  • expose
  • focus

Extra events

  • on_pause

GameObjects exclusive events

  • on_collide
  • on_click

Event Example

class MainScene (Scene):
    def key_down(self, event, key_name):
        if key_name == 'p': self.game.toggle_pause()

Return to the Index