A game engine for web-playable video games using PixiJS.
This is alpha software I'm making for my online arcade. The end-goal is to make something useable for other people too, but the design isn't finalized yet.
- A simple game loop - scenes get mounted, actors get ticked
- Scenes contain Actors or other Scenes inside of them
- Actors are PixiJS Sprites that can be moved, tinted, scaled, rotated, etc.
- Create Actors by extending base classes, using builder functions, and/or nesting them
- Control Actors with timers or temporary functions attached to the game loop (i.e., beforeTick)
- PixiJS has event-based mouse/touchscreen support with accessibility options like tabbing between elements
- Fork the muffin-game-js repo for a minimal JavaScript project using NPM.
- Check out Pipe Puzzle for a full working example project (it's a clone of Pipe Dream!)
npm install muffin-game
import { createGame, getTexture } from "muffin-game/core/setuptools"
createGame
has 3 positional arguments:- an array of strings: filenames of all the PNGs that must be preloaded
- a callback function to receive resources (PNGs) and return an object of sprite factory functions
- a GameOptions object which uses default values for missing keys:
assetPrefix
: string with URL prefix for assets- default:
assets/
- default:
gameClass
: the game class which gets instantiated- default: Game
postInit
: a function that takes the Game instance as an argument- default: connects HTML element with id=pause_button
- The Game object has static properties:
loadingScene
: This is the prevScene (previous scene) when the game starts or resets (after a game over, for example). It is never mounted, only unmounted.entryScene
: This is typically the MenuScenegameOverScene
: The default GameOverScene setsgame.state.functions.tick
to an empty function, uses a Pauser to stop Pixi.js's interactivity, and resets the game when clicked.pauseScene
: The default PauseScene is similar, but its tick function checks if the letter "P" is pressed, and it goes back to the prevScene whengame.state.flags.paused
is false.
- To create a custom menu scene, try "
Game.entryScene = MyCustomMenuScene
" before calling createGame() - See setup.js from "Pipe Puzzle" for an example
- Look at
index.html
for an example of how to embed the game in a webpage
- Basically you need
<script src="bundle.js">
and a<div id="game">
somewhere /assets
must be accessible on the web- Width and height is determined by CSS applied to the game div. Scenes are resized when the div is resized.
npm install
to get the dependencies- All source code goes in
/src
- Load assets by copy-pasting and/or editing
examples/setup.ts
- Creates scenes by subclassing/editing things inside
/scenes
- Commands:
- Development build:
npm run dev
- Development server:
npm run serve
- Production build:
npm run prod
- Development build:
- Run visual regression tests with Cypress:
- Build and run dev server
- Generate base images for comparison:
npm run test-init
- Run tests against base images:
npm run test
- Anything visible on-screen is a Pixi.js Container. A container is any drawing surface with children: the main "stage" of the game is a Container with the dimensions of the canvas.
- The main Containers you will use are Actors. These are subclasses of Pixi.js Sprites but with extra functionality to make designing games easier.
- Scenes are the main "rooms" or "levels" of a game. Scenes receive events from the game, such as
tick
, and relay these events to their Actors. They can be used to group related Actors together so they can be handled collectively. - Scenes may mount or unmount a Container to add or remove their Actors from it. Scenes can only mount 1 Container at a time.
- The Game object has a current
scene
and a previous SceneprevScene
. Use thechangeScene()
method to change the current Scene properly. - Game object is instantiated when pre-loading assets is complete.
- Use
game.startTimer(func, frames)
to time a function to trigger after a certain number of frames. This method returns the index of the timer created, so you can stop the timer early by usinggame.stopTimer(index)
- The current
tick
function which occurs on every game tick is stored ingame.state.functions.tick
so that you can customize the gameplay loop at a higher level if needed.
- The "loading" text is drawn by
setup.ts
so it happens as soon as possible - Game starts with
LoadingScene
as theprevScene
, asking it to unmount the stage (which removes the loading text and anything else on the stage blindly) - Game then mounts the
MenuScene
-- customize this scene for your game.
- A Grid is any class that owns an array of arrays with helper methods for managing it
- The
GridScene
is a type of Scene which implements the grid interface for its actors (it has an array of arrays of actors) - The
GridRectangle
is part of aTileActor
used to store a grid of textures (it has an array of arrays of rectangular regions)
See assets/README.md for graphics attribution