Game manager with a simple pong game as demo
made by RayTro
uploaded by RayTro
download cartridge


Note : This cartridge contains a pong game. This game is used as a demonstration of using the game manager. I've added some comment to the game code, so you can check on your own how it works. :)

Note2 : It's my first LUA program, so i'm sure the code is very ugly, but it work. :) If you have some advices you're welcome.

If you want the lib to be included, just copy the two functions gm_init and em_init.
If you only want the entity manager, you can just copy the em_init.
The game manager requires the entity manager to work properly.

If you copy the managers, it's not an obligation, but i'll appreciate if you mention me (at least in your code). You are free to edit them at will.


The game manager is a small library (coupled with an entity manager) that i've created for simplifying games.

I've noted that running a game with multiple loops (for example a title screen, then the regular game, an inventory menu ...) can be annoying. Like in the TIC function, when we want to switch between gamestates, this require to write a giant 'if' statement for each game states you have.

So I wrote a game manager, and an entity manager :

The Game Manager :
When you initialize it, he'll create a 'gm' global variable that contains multiple functions.
With it, you can register game states and menus.
A game state has a name and a function. A menu is something more complex that i'll explain later.
The 'gm' has a variable "currentState". You'll have to set it to a valid state name so the game manager can run his function.

The menus :
If you go line 461 to 465, you'll see how menus are registered. the first parameter of newMenu is the name. The second is the menu configuration and the third parameter is a list of items, defined with a label and an action. The action parameter can be a state name (so when the player press A on the item the game manager will switch the gamestate), or a function (that'll be called when the player press A).
Actually, you have three types of menu : 'color', 'sprite' and 'indent'. These types will check the 'selection' parameter, and use it for displaying the selected item.
- the 'color' type require that you give a palette color into the 'selection'.
- the 'sprite' type require that you give a sprite name (see the Entity Manager part below) into the 'selection'.
- the 'indent' type require that you give a number of pixels into the 'selection'.

there are three more parameters into the newMenu function : a 'back' action, that'll be called when the player press B. This parameter can be a game state or a function. The last two parameters are two 'hook' functions, the first will be called when the player move the cursom and the second when he press A on an item.

The Entity Manager :
At first, i call an entity something that have a sprite, a location and some movement logics. for example the ball you'll see on a pong game is an entity. He have a sprite (the ball), a location (x,y) and a movement logic (he bounces on walls).

When you initialize it, he'll create a 'em' global variable that contains multiple functions.
This em contains three main parts :
- a sprite manager, you have to define every sprites you want to use, and give them a name
- an entity manager, you'll have to register them with a name, a sprite name, some coordinates and a function that'll be executed for moving one step.
- some colision management : two functions with one more for debug purpose.

The colision part contains one function that gives a couple of values that form a box with some useful values if you want to check colisions on you own : x1, y1, x2, y2, width, height, centerX, centerY.
The second function is a simple colision checking function between two entities that gives you two values : one that says true or false if there is a colision, the other is on which side the colision occured.
The debug function will display the colision boxes on screen, you can remove it if you want to distribute your cart.

The entity manager has two additional functions : one that'll move every entities one step, and one that'll draw one, or all (depending on the parameter) registered entities.

Note that most functions parameter accept the entity directly, or just his name.


Quick reference :

Game Manager
gm :
currentState = default_state,
newMenu(name, config, items, back, func_onChange, func_submit)
newState(state_name, func)

Entity Manager
em :
const :
UP = 1
DOWN = 2
LEFT = 4
newSprite(id, index, colorkey, scale, flip, rotate, w, h) : sprite
getSprite(id) : sprite
hasSprite(id) : boolean
newEntity(id, spr_id, x, y, func_move, add_attr) : entity
getEntity(id) : entity
hasEntity(id) : boolean
checkColision(e1, e2) : {colision, side}
getBox(e) : {x1, y1, x2, y2, w, h, cx, cy}
debug :



fix player movements


StinkerB06 I have changed the acceleration rate of the players. Now what do you think about the movements ?


I still don't like it. The paddles feel like they're on ice. Can you add an option to make their movements like regular pong?


Okay, I removed the 'ice' effect. I understand that this effect is destroying the gameplay, I just thought that it adds a difficulty to the game. Maybe a powerup that causes this effect is more appropriate. But this pong game don't mean to be a real game, it's just a technical demonstration of the game manager.
By the way, thank you for reviewing my game :)


1:27 omg impossible

Post comment