This class provides the features necessary to implement simple non blocking games.
The games that can be created out of NonBlockingGame are based on a simple board grid of at most 1024 cells (e.g., 32x32, or any combinations less than 1024 cells). Each cell has a background color, and a colored symbol.
This class is used by having another class derive from it and implement the two functions: initialize() and GameLoop(). initialize() is called exactly once, on the first frame of the game. It is used to make first time initializations of the game state (such as setting the board in its initial position, for instance in chess). However, GameLoop() is called at every frame of the game. The game starts when the function start() is called on the object you created.
For this reason the simplest game that can run is created by:
virtual void GameLoop() override { }
};
int main () {
}
virtual void initialize()=0
This function is called once when the game starts.
NonBlockingGame(int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbCol=10)
Definition: NonBlockingGame.h:245
This game does not do anything, but it is the minimal code that will run a game. Note that the constructor of my_game passes 3 parameters to the constructor of NonBlockingGame(). These three parameters are the classic parameters that the constructor of bridges::Bridges takes (e.g., assignmentID, username, apikey).
To change the board, two functions are used. setBGColor() change the background color of a particular cell. It takes three parameters, the first two identify the cell of the board to change, and the last one is a color from a color palette provided by NamedColor. drawSymbol() takes four parameters, the first two identify the cell of the board to change, the third is a symbol from a symbol palette provided by NamedSymbol, the fourth is the color that symbol shold be drawn in and provided by NamedColor.
For instance, a very simple initialize() could look like:
}
void drawSymbol(int row, int col, NamedSymbol symb, NamedColor nc)
Draw an object on the game.
Definition: GameBase.h:131
void setBGColor(int row, int col, NamedColor nc)
Change the background color of a cell.
Definition: GameBase.h:121
NamedSymbol
Definition: GameGrid.h:164
NamedColor
Definition: GameGrid.h:14
Note that the size of the board is set by default at 10x10 and that drawing on a cell that does not exist will lead to an error. One can specify a gameboard of a different size by passing additional parameters to the NonBlockingGame constructor. However, the game can not be more than 1024 cells in total, so a 15x15 board is possible, a 32x32 board is the largest square board possible, and a rectangular 64x16 rectangular board is also possible. But a 100x20 board would be 2000 cells and is not possible. For instance a board of 16 rows and 64 columns can be created defining the my_game constructor as:
The bridges game engine will call the GameLoop() function at each frame of the game. You can write this function to modify the state of the game board using setBGColor() and drawSymbol(). For instance, the following GameLoop() will color the board randomly one cell at a time.
}
virtual void gameLoop()=0
This function is called once per frame of the game.
There are three ways for your game to take input.
The gameLoop can probe the state of some keys of the keyboard using functions keyUp(), keyLeft(), keyDown(), keyRight(), keyW(), keyA(), keyS(), keyD(), keySpace(), and keyQ(). These functions return a boolean that indicates whether the key is pressed at that frame or not. For instance, the following code will only color the board randomly when the up arrow is pressed.
}
bool keyUp()
Is Up currently pressed?
Definition: NonBlockingGame.h:384
The previous way will have an action executed at each frame of the game if the key stays pressed. This could be cumbersome for some games and you may want a key press to be triggered with a cooldown (so that it activates only every so many frames). You can configure how many frames will pass between two activations of the key with keyUpSetupFire() and you can tell whether it is a fire frame with keyUpFire(). There are similar functions for all keys that are recognized by Bridges. See the following code for a simple usage:
}
}
bool keyUpFire()
indicates whether the current frame is a fire frame for the Up key.
Definition: NonBlockingGame.h:409
void keyUpSetupFire(int f)
configures how many frames between two fire events for the Up key.
Definition: NonBlockingGame.h:416
Bridges supports a third way to use inputs that enables you to know the first frame a key is pressed and the first frame a key is no longer pressed. You can also know whether a key is still being pressed (after the first frame it is being pressed) and whether it is still not pressed (after the first frame it is no longer pressed). The four functions are keyUpJustPressed(), keyUpStillPressed(), keyUpJustNotPressed(), keyUpStillNotPressed(). The following code examplifies the usage of these functions.
}
bool keyUpStillPressed()
indicates whether the Up key is still being pressed this current frame.
Definition: NonBlockingGame.h:394
bool keyUpJustNotPressed()
indicates whether the Up key has just been released this current frame.
Definition: NonBlockingGame.h:399
bool keyUpJustPressed()
indicates whether the Up key has just been pressed this current frame.
Definition: NonBlockingGame.h:389
bool keyUpStillNotPressed()
indicates whether the Up key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:404
- See also
- There is a tutorial at: https://bridgesuncc.github.io/tutorials/NonBlockingGame.html
- Author
- Erik Saule
- Date
- 7/21/2019, 12/28/2020, 1/11/2023
|
double | getFrameRate () const |
| What frame rate is the game running at? More...
|
|
bool | keyLeft () |
| Is Left currently pressed? More...
|
|
bool | keyLeftJustPressed () |
| indicates whether the Left key has just been pressed this current frame. More...
|
|
bool | keyLeftStillPressed () |
| indicates whether the Left key is still being pressed this current frame. More...
|
|
bool | keyLeftJustNotPressed () |
| indicates whether the Left key has just been released this current frame. More...
|
|
bool | keyLeftStillNotPressed () |
| indicates whether the Left key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keyLeftFire () |
| indicates whether the current frame is a fire frame for the Left key. More...
|
|
void | keyLeftSetupFire (int f) |
| configures how many frames between two fire events for the Left key. More...
|
|
bool | keyRight () |
| Is Right currently pressed? More...
|
|
bool | keyRightJustPressed () |
| indicates whether the right key has just been pressed this current frame. More...
|
|
bool | keyRightStillPressed () |
| indicates whether the Right key is still being pressed this current frame. More...
|
|
bool | keyRightJustNotPressed () |
| indicates whether the Right key has just been released this current frame. More...
|
|
bool | keyRightStillNotPressed () |
| indicates whether the Right key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keyRightFire () |
| indicates whether the current frame is a fire frame for the Right key. More...
|
|
void | keyRightSetupFire (int f) |
| configures how many frames between two fire events for the Right key. More...
|
|
bool | keyUp () |
| Is Up currently pressed? More...
|
|
bool | keyUpJustPressed () |
| indicates whether the Up key has just been pressed this current frame. More...
|
|
bool | keyUpStillPressed () |
| indicates whether the Up key is still being pressed this current frame. More...
|
|
bool | keyUpJustNotPressed () |
| indicates whether the Up key has just been released this current frame. More...
|
|
bool | keyUpStillNotPressed () |
| indicates whether the Up key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keyUpFire () |
| indicates whether the current frame is a fire frame for the Up key. More...
|
|
void | keyUpSetupFire (int f) |
| configures how many frames between two fire events for the Up key. More...
|
|
bool | keyDown () |
| Is Down currently pressed? More...
|
|
bool | keyDownJustPressed () |
| indicates whether the Down key has just been pressed this current frame. More...
|
|
bool | keyDownStillPressed () |
| indicates whether the Down key is still being pressed this current frame. More...
|
|
bool | keyDownJustNotPressed () |
| indicates whether the Down key has just been released this current frame. More...
|
|
bool | keyDownStillNotPressed () |
| indicates whether the Down key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keyDownFire () |
| indicates whether the current frame is a fire frame for the Down key. More...
|
|
void | keyDownSetupFire (int f) |
| configures how many frames between two fire events for the Down key. More...
|
|
bool | keyW () |
| Is W currently pressed? More...
|
|
bool | keyWJustPressed () |
| indicates whether the W key has just been pressed this current frame. More...
|
|
bool | keyWStillPressed () |
| indicates whether the W key is still being pressed this current frame. More...
|
|
bool | keyWJustNotPressed () |
| indicates whether the W key has just been released this current frame. More...
|
|
bool | keyWStillNotPressed () |
| indicates whether the W key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keyWFire () |
| indicates whether the current frame is a fire frame for the W key. More...
|
|
void | keyWSetupFire (int f) |
| configures how many frames between two fire events for the W key. More...
|
|
bool | keyA () |
| Is A currently pressed? More...
|
|
bool | keyAJustPressed () |
| indicates whether the A key has just been pressed this current frame. More...
|
|
bool | keyAStillPressed () |
| indicates whether the A key is still being pressed this current frame. More...
|
|
bool | keyAJustNotPressed () |
| indicates whether the A key has just been released this current frame. More...
|
|
bool | keyAStillNotPressed () |
| indicates whether the A key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keyAFire () |
| indicates whether the current frame is a fire frame for the A key. More...
|
|
void | keyASetupFire (int f) |
| configures how many frames between two fire events for the A key. More...
|
|
bool | keyS () |
| Is S currently pressed? More...
|
|
bool | keySJustPressed () |
| indicates whether the S key has just been pressed this current frame. More...
|
|
bool | keySStillPressed () |
| indicates whether the S key is still being pressed this current frame. More...
|
|
bool | keySJustNotPressed () |
| indicates whether the S key has just been released this current frame. More...
|
|
bool | keySStillNotPressed () |
| indicates whether the S key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keySFire () |
| indicates whether the current frame is a fire frame for the S key. More...
|
|
void | keySSetupFire (int f) |
| configures how many frames between two fire events for the S key. More...
|
|
bool | keyD () |
| Is D currently pressed? More...
|
|
bool | keyDJustPressed () |
| indicates whether the D key has just been pressed this current frame. More...
|
|
bool | keyDStillPressed () |
| indicates whether the D key is still being pressed this current frame. More...
|
|
bool | keyDJustNotPressed () |
| indicates whether the D key has just been released this current frame. More...
|
|
bool | keyDStillNotPressed () |
| indicates whether the D key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keyDFire () |
| indicates whether the current frame is a fire frame for the D key. More...
|
|
void | keyDSetupFire (int f) |
| configures how many frames between two fire events for the D key. More...
|
|
bool | keyQ () |
| Is Q currently pressed? More...
|
|
bool | keyQJustPressed () |
| indicates whether the Q key has just been pressed this current frame. More...
|
|
bool | keyQStillPressed () |
| indicates whether the Q key is still being pressed this current frame. More...
|
|
bool | keyQJustNotPressed () |
| indicates whether the Q key has just been released this current frame. More...
|
|
bool | keyQStillNotPressed () |
| indicates whether the Q key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keyQFire () |
| indicates whether the current frame is a fire frame for the Q key. More...
|
|
void | keyQSetupFire (int f) |
| configures how many frames between two fire events for the Q key. More...
|
|
bool | keySpace () |
| Is Space currently pressed? More...
|
|
bool | keySpaceJustPressed () |
| indicates whether the Space key has just been pressed this current frame. More...
|
|
bool | keySpaceStillPressed () |
| indicates whether the Space key is still being pressed this current frame. More...
|
|
bool | keySpaceJustNotPressed () |
| indicates whether the Space key has just been released this current frame. More...
|
|
bool | keySpaceStillNotPressed () |
| indicates whether the Space key is not pressed and has not been pressed for more than a frame. More...
|
|
bool | keySpaceFire () |
| indicates whether the current frame is a fire frame for the Space key. More...
|
|
void | keySpaceSetupFire (int f) |
| configures how many frames between two fire events for the Space key. More...
|
|
| GameBase (int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbColumn=10) |
| Protected constructed prevens direct creation. More...
|
|
virtual | ~GameBase ()=default |
|
virtual void | initialize ()=0 |
| This function is called once when the game starts. More...
|
|
virtual void | gameLoop ()=0 |
| This function is called once per frame of the game. More...
|
|
void | registerKeyListener (KeypressListener *p) |
| register a new KeypressListener More...
|
|
void | render () |
| Renders the game. More...
|
|
bool | gameover () const |
|
void | quit () |
| calling this function causes the game to end. More...
|
|
void | setBGColor (int row, int col, NamedColor nc) |
| Change the background color of a cell. More...
|
|
void | drawSymbol (int row, int col, NamedSymbol symb, NamedColor nc) |
| Draw an object on the game. More...
|
|
void | setTitle (std::string title) |
| Set the title of the game. More...
|
|
void | setDescription (std::string desc) |
| Set a short description of the game. More...
|
|
NamedColor | getBGColor (int row, int col) |
| What color is this cell? More...
|
|
NamedSymbol | getSymbol (int row, int col) |
| What object is in this cell? More...
|
|
NamedColor | getSymbolColor (int row, int col) |
| What color is object in this cell? More...
|
|
int | getBoardWidth () |
| How wide is the Game Board? More...
|
|
int | getBoardHeight () |
| How tall is the Game Board? More...
|
|