Bridges-Java
3.4.3
Bridges(Java API)
|
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:
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:
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:
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.
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:
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.
Public Member Functions | |
NonBlockingGame (int assignmentID, String login, String apikey, int rows, int cols) | |
Construct a NonBlockingGame object. It is expected students will never directly construct a NonBlockingGame object but rather derive from it. More... | |
NonBlockingGame (int assignmentID, String login, String apikey, int rows, int cols, boolean deb) | |
void | start () |
Public Member Functions inherited from bridges.games.GameBase | |
GameBase (int assid, String login, String apikey, int rows, int cols) | |
GameBase (int assid, String login, String apikey, int rows, int cols, boolean d) | |
Protected Member Functions | |
boolean | keyLeft () |
Is the LeftArrow key currently pressed? More... | |
boolean | keyLeftJustPressed () |
indicates whether the Left key has just been pressed this current frame. More... | |
boolean | keyLeftStillPressed () |
indicates whether the Left key is still being pressed in this frame. More... | |
boolean | keyLeftJustNotPressed () |
indicates whether the Left key has been just released in this frame More... | |
boolean | keyLeftStillNotPressed () |
indicates whether the Left key not pressed and has not been pressed for more than a frame More... | |
boolean | keyLeftFire () |
indicates whether the current frame is a fire frame for the Left key. More... | |
void | keyLeftSetupFire (int f) |
boolean | keyRight () |
Is the RightArrow key currently pressed? More... | |
boolean | keyRightJustPressed () |
indicates whether the Right key has just been pressed this current frame. More... | |
boolean | keyRightStillPressed () |
indicates whether the Right key is still being pressed in this frame. More... | |
boolean | keyRightJustNotPressed () |
indicates whether the Right key has been just released in this frame More... | |
boolean | keyRightStillNotPressed () |
indicates whether the Right key not pressed and has not been pressed for more than a frame More... | |
boolean | keyRightFire () |
indicates whether the current frame is a fire frame for the Right key. More... | |
void | keyRightSetupFire (int f) |
boolean | keyUp () |
Is the UpArrow key currently pressed? More... | |
boolean | keyUpJustPressed () |
indicates whether the Up key has just been pressed this current frame. More... | |
boolean | keyUpStillPressed () |
indicates whether the Up key is still being pressed in this frame. More... | |
boolean | keyUpJustNotPressed () |
indicates whether the Up key has been just released in this frame More... | |
boolean | keyUpStillNotPressed () |
indicates whether the Up key not pressed and has not been pressed for more than a frame More... | |
boolean | keyUpFire () |
indicates whether the current frame is a fire frame for the Up key. More... | |
void | keyUpSetupFire (int f) |
boolean | keyDown () |
Is the DownArrow key currently pressed? More... | |
boolean | keyDownJustPressed () |
indicates whether the Down key has just been pressed this current frame. More... | |
boolean | keyDownStillPressed () |
indicates whether the Down key is still being pressed in this frame. More... | |
boolean | keyDownJustNotPressed () |
indicates whether the Down key has been just released in this frame More... | |
boolean | keyDownStillNotPressed () |
indicates whether the Down key not pressed and has not been pressed for more than a frame More... | |
boolean | keyDownFire () |
indicates whether the current frame is a fire frame for the Down key. More... | |
void | keyDownSetupFire (int f) |
boolean | keyQ () |
Is the Q key currently pressed? More... | |
boolean | keyQJustPressed () |
indicates whether the Q key has just been pressed this current frame. More... | |
boolean | keyQStillPressed () |
indicates whether the Q key is still being pressed in this frame. More... | |
boolean | keyQJustNotPressed () |
indicates whether the Q key has been just released in this frame More... | |
boolean | keyQStillNotPressed () |
indicates whether the Q key not pressed and has not been pressed for more than a frame More... | |
boolean | keyQFire () |
indicates whether the current frame is a fire frame for the Q key. More... | |
void | keyQSetupFire (int f) |
boolean | keySpace () |
Is the SpaceBar key currently pressed? More... | |
boolean | keySpaceJustPressed () |
indicates whether the Space key has just been pressed this current frame. More... | |
boolean | keySpaceStillPressed () |
indicates whether the Space key is still being pressed in this frame. More... | |
boolean | keySpaceJustNotPressed () |
indicates whether the Space key has been just released in this frame More... | |
boolean | keySpaceStillNotPressed () |
indicates whether the Space key not pressed and has not been pressed for more than a frame More... | |
boolean | keySpaceFire () |
indicates whether the current frame is a fire frame for the Space key. More... | |
void | keySpaceSetupFire (int f) |
boolean | keyW () |
Is the W key currently pressed? More... | |
boolean | keyWJustPressed () |
indicates whether the W key has just been pressed this current frame. More... | |
boolean | keyWStillPressed () |
indicates whether the W key is still being pressed in this frame. More... | |
boolean | keyWJustNotPressed () |
indicates whether the W key has been just released in this frame More... | |
boolean | keyWStillNotPressed () |
indicates whether the W key not pressed and has not been pressed for more than a frame More... | |
boolean | keyWFire () |
indicates whether the current frame is a fire frame for the W key. More... | |
void | keyWSetupFire (int f) |
boolean | keyA () |
Is the A key currently pressed? More... | |
boolean | keyAJustPressed () |
indicates whether the A key has just been pressed this current frame. More... | |
boolean | keyAStillPressed () |
indicates whether the A key is still being pressed in this frame. More... | |
boolean | keyAJustNotPressed () |
indicates whether the A key has been just released in this frame More... | |
boolean | keyAStillNotPressed () |
indicates whether the A key not pressed and has not been pressed for more than a frame More... | |
boolean | keyAFire () |
indicates whether the current frame is a fire frame for the A key. More... | |
void | keyASetupFire (int f) |
boolean | keyS () |
Is the S key currently pressed? More... | |
boolean | keySJustPressed () |
indicates whether the S key has just been pressed this current frame. More... | |
boolean | keySStillPressed () |
indicates whether the S key is still being pressed in this frame. More... | |
boolean | keySJustNotPressed () |
indicates whether the S key has been just released in this frame More... | |
boolean | keySStillNotPressed () |
indicates whether the S key not pressed and has not been pressed for more than a frame More... | |
boolean | keySFire () |
indicates whether the current frame is a fire frame for the S key. More... | |
void | keySSetupFire (int f) |
boolean | keyD () |
Is the D key currently pressed? More... | |
boolean | keyDJustPressed () |
indicates whether the D key has just been pressed this current frame. More... | |
boolean | keyDStillPressed () |
indicates whether the D key is still being pressed in this frame. More... | |
boolean | keyDJustNotPressed () |
indicates whether the D key has been just released in this frame More... | |
boolean | keyDStillNotPressed () |
indicates whether the D key not pressed and has not been pressed for more than a frame More... | |
boolean | keyDFire () |
indicates whether the current frame is a fire frame for the D key. More... | |
void | keyDSetupFire (int f) |
double | getFrameRate () |
What frame rate is the game running at? More... | |
void | setFrameRate (double fps) |
change the 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 |
Package Functions inherited from bridges.games.GameBase | |
void | registerKeypress (KeypressListener kl) |
register a new KeypressListener More... | |
void | render () |
Renders the game. More... | |
bridges.games.NonBlockingGame.NonBlockingGame | ( | int | assignmentID, |
String | login, | ||
String | apikey, | ||
int | rows, | ||
int | cols | ||
) |
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).
assignmentID | bridges assignment ID |
login | login on the bridges server |
apikey | apikey of the account specified in login |
rows | number of rows in the game |
cols | number of columns in the game |
bridges.games.NonBlockingGame.NonBlockingGame | ( | int | assignmentID, |
String | login, | ||
String | apikey, | ||
int | rows, | ||
int | cols, | ||
boolean | deb | ||
) |
|
protected |
What frame rate is the game running at?
|
protected |
Is the A key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the A key.
|
protected |
indicates whether the A key has been just released in this frame
|
protected |
indicates whether the A key has just been pressed this current frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the A key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the A key is still being pressed in this frame.
|
protected |
Is the D key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the D key.
|
protected |
indicates whether the D key has been just released in this frame
|
protected |
indicates whether the D key has just been pressed this current frame.
|
protected |
Is the DownArrow key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the Down key.
|
protected |
indicates whether the Down key has been just released in this frame
|
protected |
indicates whether the Down key has just been pressed this current frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the Down key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the Down key is still being pressed in this frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the D key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the D key is still being pressed in this frame.
|
protected |
Is the LeftArrow key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the Left key.
|
protected |
indicates whether the Left key has been just released in this frame
|
protected |
indicates whether the Left key has just been pressed this current frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the Left key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the Left key is still being pressed in this frame.
|
protected |
Is the Q key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the Q key.
|
protected |
indicates whether the Q key has been just released in this frame
|
protected |
indicates whether the Q key has just been pressed this current frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the Q key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the Q key is still being pressed in this frame.
|
protected |
Is the RightArrow key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the Right key.
|
protected |
indicates whether the Right key has been just released in this frame
|
protected |
indicates whether the Right key has just been pressed this current frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the Right key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the Right key is still being pressed in this frame.
|
protected |
Is the S key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the S key.
|
protected |
indicates whether the S key has been just released in this frame
|
protected |
indicates whether the S key has just been pressed this current frame.
|
protected |
Is the SpaceBar key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the Space key.
|
protected |
indicates whether the Space key has been just released in this frame
|
protected |
indicates whether the Space key has just been pressed this current frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the Space key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the Space key is still being pressed in this frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the S key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the S key is still being pressed in this frame.
|
protected |
Is the UpArrow key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the Up key.
|
protected |
indicates whether the Up key has been just released in this frame
|
protected |
indicates whether the Up key has just been pressed this current frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the Up key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the Up key is still being pressed in this frame.
|
protected |
Is the W key currently pressed?
|
protected |
indicates whether the current frame is a fire frame for the W key.
|
protected |
indicates whether the W key has been just released in this frame
|
protected |
indicates whether the W key has just been pressed this current frame.
|
protected |
f | how many frames between two fire events |
|
protected |
indicates whether the W key not pressed and has not been pressed for more than a frame
|
protected |
indicates whether the W key is still being pressed in this frame.
|
protected |
change the frame rate is the game running at
The fps is caps at 30 fps and can't drop below 1 fps
void bridges.games.NonBlockingGame.start | ( | ) |
Call this function to start the game engine.
Reimplemented from bridges.games.GameBase.