Bridges-C++  3.2.0
Bridges(C++API)
Public Member Functions | Protected Member Functions | List of all members
bridges::game::NonBlockingGame Class Reference

#include <NonBlockingGame.h>

Inheritance diagram for bridges::game::NonBlockingGame:
bridges::game::GameBase

Detailed Description

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:

using namespace bridges::game;
struct my_game : public NonBlockingGame {
my_game() : NonBlockingGame (1, "myuserid", "myapikey") {}
virtual void initialize() override { }
virtual void GameLoop() override { }
};
int main () {
my_game g;
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:

virtual void initialize() override {
setBGColor(0, 0, NamedColor.lightsalmon);
drawSymbol(0, 0, NamedSymbol.sword, NamedColor.blue);
}

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:

my_game() : NonBlockingGame (1, "myuserid", "myapikey", 16, 64) {}

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() override {
setBGColor(rand()%10, rand()%10, NamedColor.lightsalmon);
}

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.

virtual void gameLoop() override {
if (keyUp())
setBGColor(rand()%10, rand()%10, NamedColor.lightsalmon);
}
See also
There is a tutorial at: http://bridgesuncc.github.io/tutorials/NonBlockingGame.html
Author
Erik Saule
Date
7/21/19, 12/28/20

Public Member Functions

 NonBlockingGame (int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbCol=10)
 
void start ()
 Call this function from main to start the game. Runs exactly once. More...
 

Protected Member Functions

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...
 
- Protected Member Functions inherited from bridges::game::GameBase
 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...
 

Additional Inherited Members

- Protected Attributes inherited from bridges::game::GameBase
bool debug = false
 

Constructor & Destructor Documentation

◆ NonBlockingGame()

bridges::game::NonBlockingGame::NonBlockingGame ( int  assignmentID,
std::string  username,
std::string  apikey,
int  nbRow = 10,
int  nbCol = 10 
)
inline

constructor

Parameters
assignmentIDBridges assignment id
usernameBridges user name
apikeyBridges API authentication key
nbRowGameGrid height
nbColGameGrid width

Member Function Documentation

◆ getFrameRate()

double bridges::game::NonBlockingGame::getFrameRate ( ) const
inlineprotected

What frame rate is the game running at?

Returns
the target framerate. The game could be somewhat slower depending on how computationally expensive the gameloop is and on the speed of the network.

◆ keyA()

bool bridges::game::NonBlockingGame::keyA ( )
inlineprotected

Is A currently pressed?

Returns
true if A is currently pressed

◆ keyD()

bool bridges::game::NonBlockingGame::keyD ( )
inlineprotected

Is D currently pressed?

Returns
true if D is currently pressed

◆ keyDown()

bool bridges::game::NonBlockingGame::keyDown ( )
inlineprotected

Is Down currently pressed?

Returns
true if Down is currently pressed

◆ keyLeft()

bool bridges::game::NonBlockingGame::keyLeft ( )
inlineprotected

Is Left currently pressed?

Returns
true if Left is currently pressed

◆ keyQ()

bool bridges::game::NonBlockingGame::keyQ ( )
inlineprotected

Is Q currently pressed?

Returns
true if S is currently pressed

◆ keyRight()

bool bridges::game::NonBlockingGame::keyRight ( )
inlineprotected

Is Right currently pressed?

Returns
true if Right is currently pressed

◆ keyS()

bool bridges::game::NonBlockingGame::keyS ( )
inlineprotected

Is S currently pressed?

Returns
true if S is currently pressed

◆ keySpace()

bool bridges::game::NonBlockingGame::keySpace ( )
inlineprotected

Is Space currently pressed?

Returns
true if Space is currently pressed

◆ keyUp()

bool bridges::game::NonBlockingGame::keyUp ( )
inlineprotected

Is Up currently pressed?

Returns
true if Up is currently pressed

◆ keyW()

bool bridges::game::NonBlockingGame::keyW ( )
inlineprotected

Is W currently pressed?

Returns
true if W is currently pressed

◆ start()

void bridges::game::NonBlockingGame::start ( )
inline

Call this function from main to start the game. Runs exactly once.


The documentation for this class was generated from the following file: