This class provides the features necessary to implement simple non blocking games.
More...
|
| boolean | keyLeft () |
| | Is the LeftArrow key currently pressed?
|
| |
| boolean | keyLeftJustPressed () |
| | indicates whether the Left key has just been pressed this current frame.
|
| |
| boolean | keyLeftStillPressed () |
| | indicates whether the Left key is still being pressed in this frame.
|
| |
| boolean | keyLeftJustNotPressed () |
| | indicates whether the Left key has been just released in this frame
|
| |
| boolean | keyLeftStillNotPressed () |
| | indicates whether the Left key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keyLeftFire () |
| | indicates whether the current frame is a fire frame for the Left key.
|
| |
| void | keyLeftSetupFire (int f) |
| |
| boolean | keyRight () |
| | Is the RightArrow key currently pressed?
|
| |
| boolean | keyRightJustPressed () |
| | indicates whether the Right key has just been pressed this current frame.
|
| |
| boolean | keyRightStillPressed () |
| | indicates whether the Right key is still being pressed in this frame.
|
| |
| boolean | keyRightJustNotPressed () |
| | indicates whether the Right key has been just released in this frame
|
| |
| boolean | keyRightStillNotPressed () |
| | indicates whether the Right key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keyRightFire () |
| | indicates whether the current frame is a fire frame for the Right key.
|
| |
| void | keyRightSetupFire (int f) |
| |
| boolean | keyUp () |
| | Is the UpArrow key currently pressed?
|
| |
| boolean | keyUpJustPressed () |
| | indicates whether the Up key has just been pressed this current frame.
|
| |
| boolean | keyUpStillPressed () |
| | indicates whether the Up key is still being pressed in this frame.
|
| |
| boolean | keyUpJustNotPressed () |
| | indicates whether the Up key has been just released in this frame
|
| |
| boolean | keyUpStillNotPressed () |
| | indicates whether the Up key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keyUpFire () |
| | indicates whether the current frame is a fire frame for the Up key.
|
| |
| void | keyUpSetupFire (int f) |
| |
| boolean | keyDown () |
| | Is the DownArrow key currently pressed?
|
| |
| boolean | keyDownJustPressed () |
| | indicates whether the Down key has just been pressed this current frame.
|
| |
| boolean | keyDownStillPressed () |
| | indicates whether the Down key is still being pressed in this frame.
|
| |
| boolean | keyDownJustNotPressed () |
| | indicates whether the Down key has been just released in this frame
|
| |
| boolean | keyDownStillNotPressed () |
| | indicates whether the Down key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keyDownFire () |
| | indicates whether the current frame is a fire frame for the Down key.
|
| |
| void | keyDownSetupFire (int f) |
| |
| boolean | keyQ () |
| | Is the Q key currently pressed?
|
| |
| boolean | keyQJustPressed () |
| | indicates whether the Q key has just been pressed this current frame.
|
| |
| boolean | keyQStillPressed () |
| | indicates whether the Q key is still being pressed in this frame.
|
| |
| boolean | keyQJustNotPressed () |
| | indicates whether the Q key has been just released in this frame
|
| |
| boolean | keyQStillNotPressed () |
| | indicates whether the Q key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keyQFire () |
| | indicates whether the current frame is a fire frame for the Q key.
|
| |
| void | keyQSetupFire (int f) |
| |
| boolean | keySpace () |
| | Is the SpaceBar key currently pressed?
|
| |
| boolean | keySpaceJustPressed () |
| | indicates whether the Space key has just been pressed this current frame.
|
| |
| boolean | keySpaceStillPressed () |
| | indicates whether the Space key is still being pressed in this frame.
|
| |
| boolean | keySpaceJustNotPressed () |
| | indicates whether the Space key has been just released in this frame
|
| |
| boolean | keySpaceStillNotPressed () |
| | indicates whether the Space key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keySpaceFire () |
| | indicates whether the current frame is a fire frame for the Space key.
|
| |
| void | keySpaceSetupFire (int f) |
| |
| boolean | keyW () |
| | Is the W key currently pressed?
|
| |
| boolean | keyWJustPressed () |
| | indicates whether the W key has just been pressed this current frame.
|
| |
| boolean | keyWStillPressed () |
| | indicates whether the W key is still being pressed in this frame.
|
| |
| boolean | keyWJustNotPressed () |
| | indicates whether the W key has been just released in this frame
|
| |
| boolean | keyWStillNotPressed () |
| | indicates whether the W key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keyWFire () |
| | indicates whether the current frame is a fire frame for the W key.
|
| |
| void | keyWSetupFire (int f) |
| |
| boolean | keyA () |
| | Is the A key currently pressed?
|
| |
| boolean | keyAJustPressed () |
| | indicates whether the A key has just been pressed this current frame.
|
| |
| boolean | keyAStillPressed () |
| | indicates whether the A key is still being pressed in this frame.
|
| |
| boolean | keyAJustNotPressed () |
| | indicates whether the A key has been just released in this frame
|
| |
| boolean | keyAStillNotPressed () |
| | indicates whether the A key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keyAFire () |
| | indicates whether the current frame is a fire frame for the A key.
|
| |
| void | keyASetupFire (int f) |
| |
| boolean | keyS () |
| | Is the S key currently pressed?
|
| |
| boolean | keySJustPressed () |
| | indicates whether the S key has just been pressed this current frame.
|
| |
| boolean | keySStillPressed () |
| | indicates whether the S key is still being pressed in this frame.
|
| |
| boolean | keySJustNotPressed () |
| | indicates whether the S key has been just released in this frame
|
| |
| boolean | keySStillNotPressed () |
| | indicates whether the S key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keySFire () |
| | indicates whether the current frame is a fire frame for the S key.
|
| |
| void | keySSetupFire (int f) |
| |
| boolean | keyD () |
| | Is the D key currently pressed?
|
| |
| boolean | keyDJustPressed () |
| | indicates whether the D key has just been pressed this current frame.
|
| |
| boolean | keyDStillPressed () |
| | indicates whether the D key is still being pressed in this frame.
|
| |
| boolean | keyDJustNotPressed () |
| | indicates whether the D key has been just released in this frame
|
| |
| boolean | keyDStillNotPressed () |
| | indicates whether the D key not pressed and has not been pressed for more than a frame
|
| |
| boolean | keyDFire () |
| | indicates whether the current frame is a fire frame for the D key.
|
| |
| void | keyDSetupFire (int f) |
| |
| double | getFrameRate () |
| | What frame rate is the game running at?
|
| |
| void | setFrameRate (double fps) |
| | change the frame rate is the game running at
|
| |
| abstract void | initialize () |
| | This function is called once when the game starts.
|
| |
| abstract void | gameLoop () |
| | This function is called once per frame of the game.
|
| |
| void | quit () |
| | Call this function to stop the game.
|
| |
| void | setTitle (String title) |
| | Set the title of the game.
|
| |
| void | setDescription (String desc) |
| | Set a short description of the game.
|
| |
| void | setBGColor (int row, int col, NamedColor c) |
| | Change the background color of a cell.
|
| |
| NamedColor | getBGColor (int row, int col) |
| | What color is this cell?
|
| |
| NamedSymbol | getSymbol (int row, int col) |
| | What object is in this cell?
|
| |
| NamedColor | getSymbolColor (int row, int col) |
| | What color is object in this cell?
|
| |
| void | drawSymbol (int row, int col, NamedSymbol s, NamedColor c) |
| | Draw a symbol on the game.
|
| |
| int | getBoardWidth () |
| | How wide is the Game Board?
|
| |
| int | getBoardHeight () |
| | How tall is the Game Board?
|
| |
| void | terminateNetwork () |
| | terminal all network connections Note that it takes a minute for all threads to gracefully exit
|
| |
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 48x48 cells (or any combinations less than 48x48 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.*;
public MyGame() { super (1, "myuserid", "myapikey", 10, 10); }
public void initialize() { }
public void gameLoop() { }
public static void main (String args[]) {
MyGame g = new MyGame();
g.start();
}
}
This class provides the features necessary to implement simple non blocking games.
Definition NonBlockingGame.java:157
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 48x48 cells in total, so a 15x15 board is possible, a 96x24 rectangular board is also possible. But a 200x20 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 MyGame() { 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);
}
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:
void initialize() override {
keyUpSetupFire(20);
}
void gameLoop() override {
if (keyUpFire())
setBGColor(rand()%10, rand()%10, NamedColor.lightsalmon);
}
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.
virtual void gameLoop() override {
if (keyUpJustPressed())
setBGColor(0, 0, NamedColor.lightsalmon);
if (keyUpStillPressed())
setBGColor(0, 1, NamedColor.lightsalmon);
if (keyUpJustNotPressed())
setBGColor(0, 2, NamedColor.lightsalmon);
if (keyUpStillNotPressed())
setBGColor(0, 3, NamedColor.lightsalmon);
}
- See also
- See tutorial at https://bridgesuncc.github.io/tutorials/NonBlockingGame.html
- Author
- Erik Saule
- Date
- 7/21/19, 12/02/22, 1/11/2023