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 () {
g.start();
}
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:
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.
The gameLoop can also 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 indicate 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.
- See also
- There is a tutorial at: http://bridgesuncc.github.io/tutorials/NonBlockingGame.html
- Author
- Erik Saule
- Date
- 7/21/19, 12/28/20
|
double | getFrameRate () const |
| What frame rate is the game running at? More...
|
|
bool | keyLeft () |
| Is Left currently pressed? More...
|
|
bool | keyRight () |
| Is Right currently pressed? More...
|
|
bool | keyUp () |
| Is Up currently pressed? More...
|
|
bool | keyDown () |
| Is Down currently pressed? More...
|
|
bool | keyW () |
| Is W currently pressed? More...
|
|
bool | keyA () |
| Is A currently pressed? More...
|
|
bool | keyS () |
| Is S currently pressed? More...
|
|
bool | keyD () |
| Is D currently pressed? More...
|
|
bool | keyQ () |
| Is Q currently pressed? More...
|
|
bool | keySpace () |
| Is Space currently pressed? More...
|
|
| GameBase (int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbColumn=10) |
| Protected constructed prevens direct creation. More...
|
|
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...
|
|