Bridges-Java-3.0.1  3.0.1
Bridges(JavaAPI)
Public Member Functions | Protected Member Functions | List of all members
bridges.games.NonBlockingGame Class Referenceabstract
Inheritance diagram for bridges.games.NonBlockingGame:
bridges.games.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:

import bridges.game.*;
import bridges.base.*;
class my_game extends NonBlockingGame {
public my_game() { super (1, "myuserid", "myapikey", 10, 10); }
public void initialize() { }
public void gameLoop() { }
public static void main (String args[]) {
my_game g = new my_game();
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 5 parameters to the constructor of NonBlockingGame(). The first three parameters are the classic parameters that the constructor of bridges.connect.Bridges takes (e.g., assignmentID, username, apikey), the last two are the size of the game board.

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:

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

Note that the size of the board was set 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 changing the parameters to the NonBlockingGame constructor. However, the game board 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:

public my_game() { super(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.

public void gameLoop() {
setBGColor(Math.random()*10, Math.random()*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.

public void gameLoop() {
if (keyUp())
setBGColor(Math.random()*10, Math.rand()*10, NamedColor.lightsalmon);
}
See also
See tutorial at http://bridgesuncc.github.io/tutorials/NonBlockingGame.html
Author
Erik Saule
Date
7/21/19

Public Member Functions

 NonBlockingGame (int assignmentID, String login, String apikey, int cols, int rows)
 Construct a NonBlockingGame object. It is expected students will never directly construct a NonBlockingGame object but rather derive from it. More...
 
void start ()
 
- Public Member Functions inherited from bridges.games.GameBase
 GameBase (int assid, String login, String apikey, int cols, int rows)
 
abstract void start ()
 Call this function from main to start the game. More...
 

Protected Member Functions

boolean keyLeft ()
 Is the LeftArrow key currently pressed? More...
 
boolean keyRight ()
 Is the RightArrow key currently pressed? More...
 
boolean keyUp ()
 Is the UpArrow key currently pressed? More...
 
boolean keyDown ()
 Is the DownArrow key currently pressed? More...
 
boolean keyQ ()
 Is the Q key currently pressed? More...
 
boolean keySpace ()
 Is the SpaceBar key currently pressed? More...
 
boolean keyW ()
 Is the W key currently pressed? More...
 
boolean keyA ()
 Is the A key currently pressed? More...
 
boolean keyS ()
 Is the S key currently pressed? More...
 
boolean keyD ()
 Is the D key currently pressed? More...
 
double getFrameRate ()
 What frame rate is the game running at? More...
 
- Protected Member Functions inherited from bridges.games.GameBase
abstract void initialize ()
 This function is called once when the game starts. More...
 
abstract void gameLoop ()
 This function is called once per frame of the game. More...
 
void quit ()
 Call this function to stop the game. More...
 
void setTitle (String title)
 Set the title of the game. More...
 
void setDescription (String desc)
 Set a short description of the game. More...
 
void setBGColor (int row, int col, NamedColor c)
 Change the background color of a cell. 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...
 
void drawSymbol (int row, int col, NamedSymbol s, NamedColor c)
 Draw a symbol on the game. More...
 
int getBoardWidth ()
 How wide is the Game Board? More...
 
int getBoardHeight ()
 How tall is the Game Board? More...
 
void terminateNetwork ()
 terminal all network connections Note that it takes a minute for all threads to gracefully exit More...
 

Additional Inherited Members

- Protected Attributes inherited from bridges.games.GameBase
boolean debug = false
 
boolean gameStarted = true
 

Constructor & Destructor Documentation

◆ NonBlockingGame()

bridges.games.NonBlockingGame.NonBlockingGame ( int  assignmentID,
String  login,
String  apikey,
int  cols,
int  rows 
)

Construct a NonBlockingGame object. It is expected students will never directly construct a NonBlockingGame object but rather derive from it.

The created grid can not be larger than 1024 cells in total (e.g., 32x32, or 2x512 are ok).

Parameters
assignmentIDbridges assignment ID
loginlogin on the bridges server
apikeyapikey of the account specified in login
colsnumber of columns in the game
rowsnumber of rows in the game

Member Function Documentation

◆ getFrameRate()

double bridges.games.NonBlockingGame.getFrameRate ( )
protected

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()

boolean bridges.games.NonBlockingGame.keyA ( )
protected

Is the A key currently pressed?

Returns
true if "a" is pressed?

◆ keyD()

boolean bridges.games.NonBlockingGame.keyD ( )
protected

Is the D key currently pressed?

Returns
true if "d" is pressed

◆ keyDown()

boolean bridges.games.NonBlockingGame.keyDown ( )
protected

Is the DownArrow key currently pressed?

Returns
true if "down" is pressed

◆ keyLeft()

boolean bridges.games.NonBlockingGame.keyLeft ( )
protected

Is the LeftArrow key currently pressed?

Returns
true if "left" is pressed

◆ keyQ()

boolean bridges.games.NonBlockingGame.keyQ ( )
protected

Is the Q key currently pressed?

Returns
true if "q" is pressed

◆ keyRight()

boolean bridges.games.NonBlockingGame.keyRight ( )
protected

Is the RightArrow key currently pressed?

Returns
true if "right" is pressed

◆ keyS()

boolean bridges.games.NonBlockingGame.keyS ( )
protected

Is the S key currently pressed?

Returns
true if "s" is pressed

◆ keySpace()

boolean bridges.games.NonBlockingGame.keySpace ( )
protected

Is the SpaceBar key currently pressed?

Returns
true if SpaceBar is pressed

◆ keyUp()

boolean bridges.games.NonBlockingGame.keyUp ( )
protected

Is the UpArrow key currently pressed?

Returns
true if "up" is pressed

◆ keyW()

boolean bridges.games.NonBlockingGame.keyW ( )
protected

Is the W key currently pressed?

Returns
true if "w" is pressed

◆ start()

void bridges.games.NonBlockingGame.start ( )

Call this function to start the game engine.


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