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.
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:
def initialize():
set_bg_color(0, 0, NamedColor.lightsalmon);
@code
draw_symbol(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:
@code
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.
def game_loop():
set_BG_Color(rand()%10, rand()%10, NamedColor.lightsalmon);
The gameLoop can also probe the state of some keys of the keyboard using functions key_up(), key_left(),
key_down(), key_right(), key_w(), key_a(), key_s(),
key_d(), key_space(), and key_q(). 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.
def game_loop():
if key_up():
@code
set_bg_color(rand()%10, 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:
def initialize() override:
@code
keyUpSetupFire(20);
gameLoop(): if # will only be true once every 20 frames
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.
gameLoop():
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);
- Author
- Erik Saule
- Date
- 7-21-19, 1-12-23
- See also
- NonBlockingGame tutorial at: https://bridgesuncc.github.io/tutorials/NonBlockingGame.html
|
def | __init__ (self, assid, login, apikey, rows, cols, debug=False) |
| PROTECTED constructor prevents the object from being directly created. More...
|
|
def | fps (self) |
| Get the frame rate at which the game running. More...
|
|
def | fps (self, frames) |
| Set the frame rate at which the game running. More...
|
|
def | sleep_timer (self, timems=None) |
|
def | control_framerate (self) |
|
def | start (self) |
| Call this function from main to start game. More...
|
|
def | key_left (self) |
| Is 'left' arrow key currently pressed? More...
|
|
def | key_left_just_pressed (self) |
| indicates whether the Left key has just been pressed this current frame More...
|
|
def | key_left_still_pressed (self) |
| indicates whether the Left key is still being pressed this current frame More...
|
|
def | key_left_just_not_pressed (self) |
| indicates whether the Left key has just been released this current frame More...
|
|
def | key_left_still_not_pressed (self) |
| indicates whether the Left key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_left_fire (self) |
| indicates whether the current frame is a fire frame for the Left key More...
|
|
def | key_left_setup_fire (self, int f) |
|
def | key_right (self) |
| Is 'right' arrow currently pressed? More...
|
|
def | key_right_just_pressed (self) |
| indicates whether the Right key has just been pressed this current frame More...
|
|
def | key_right_still_pressed (self) |
| indicates whether the Right key is still being pressed this current frame More...
|
|
def | key_right_just_not_pressed (self) |
| indicates whether the Right key has just been released this current frame More...
|
|
def | key_right_still_not_pressed (self) |
| indicates whether the Right key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_right_fire (self) |
| indicates whether the current frame is a fire frame for the Right key More...
|
|
def | key_right_setup_fire (self, int f) |
|
def | key_up (self) |
| Is 'up' arrow currently pressed? More...
|
|
def | key_up_just_pressed (self) |
| indicates whether the Up key has just been pressed this current frame More...
|
|
def | key_up_still_pressed (self) |
| indicates whether the Up key is still being pressed this current frame More...
|
|
def | key_up_just_not_pressed (self) |
| indicates whether the Up key has just been released this current frame More...
|
|
def | key_up_still_not_pressed (self) |
| indicates whether the Up key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_up_fire (self) |
| indicates whether the current frame is a fire frame for the Up key More...
|
|
def | key_up_setup_fire (self, int f) |
|
def | key_down (self) |
| Is 'down' arrow key currently pressed? More...
|
|
def | key_down_just_pressed (self) |
| indicates whether the Down key has just been pressed this current frame More...
|
|
def | key_down_still_pressed (self) |
| indicates whether the Down key is still being pressed this current frame More...
|
|
def | key_down_just_not_pressed (self) |
| indicates whether the Down key has just been released this current frame More...
|
|
def | key_down_still_not_pressed (self) |
| indicates whether the Down key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_down_fire (self) |
| indicates whether the current frame is a fire frame for the Down key More...
|
|
def | key_down_setup_fire (self, int f) |
|
def | key_q (self) |
| Is 'q' currently pressed? More...
|
|
def | key_q_just_pressed (self) |
| indicates whether the Q key has just been pressed this current frame More...
|
|
def | key_q_still_pressed (self) |
| indicates whether the Q key is still being pressed this current frame More...
|
|
def | key_q_just_not_pressed (self) |
| indicates whether the Q key has just been released this current frame More...
|
|
def | key_q_still_not_pressed (self) |
| indicates whether the Q key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_q_fire (self) |
| indicates whether the current frame is a fire frame for the Q key More...
|
|
def | key_q_setup_fire (self, int f) |
|
def | key_space (self) |
| Is 'space' key currently pressed? More...
|
|
def | key_space_just_pressed (self) |
| indicates whether the Space key has just been pressed this current frame More...
|
|
def | key_space_still_pressed (self) |
| indicates whether the Space key is still being pressed this current frame More...
|
|
def | key_space_just_not_pressed (self) |
| indicates whether the Space key has just been released this current frame More...
|
|
def | key_space_still_not_pressed (self) |
| indicates whether the Space key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_space_fire (self) |
| indicates whether the current frame is a fire frame for the Space key More...
|
|
def | key_space_setup_fire (self, int f) |
|
def | key_w (self) |
| Is 'w' currently pressed? More...
|
|
def | key_w_just_pressed (self) |
| indicates whether the W key has just been pressed this current frame More...
|
|
def | key_w_still_pressed (self) |
| indicates whether the W key is still being pressed this current frame More...
|
|
def | key_w_just_not_pressed (self) |
| indicates whether the W key has just been released this current frame More...
|
|
def | key_w_still_not_pressed (self) |
| indicates whether the W key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_w_fire (self) |
| indicates whether the current frame is a fire frame for the W key More...
|
|
def | key_w_setup_fire (self, int f) |
|
def | key_a (self) |
| Is 'a' currently pressed? More...
|
|
def | key_a_just_pressed (self) |
| indicates whether the A key has just been pressed this current frame More...
|
|
def | key_a_still_pressed (self) |
| indicates whether the A key is still being pressed this current frame More...
|
|
def | key_a_just_not_pressed (self) |
| indicates whether the A key has just been released this current frame More...
|
|
def | key_a_still_not_pressed (self) |
| indicates whether the A key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_a_fire (self) |
| indicates whether the current frame is a fire frame for the A key More...
|
|
def | key_a_setup_fire (self, int f) |
|
def | key_s (self) |
| Is 's' key currently pressed? More...
|
|
def | key_s_just_pressed (self) |
| indicates whether the S key has just been pressed this current frame More...
|
|
def | key_s_still_pressed (self) |
| indicates whether the S key is still being pressed this current frame More...
|
|
def | key_s_just_not_pressed (self) |
| indicates whether the S key has just been released this current frame More...
|
|
def | key_s_still_not_pressed (self) |
| indicates whether the S key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_s_fire (self) |
| indicates whether the current frame is a fire frame for the S key More...
|
|
def | key_s_setup_fire (self, int f) |
|
def | key_d (self) |
| Is 'd' key currently pressed? More...
|
|
def | key_d_just_pressed (self) |
| indicates whether the D key has just been pressed this current frame More...
|
|
def | key_d_still_pressed (self) |
| indicates whether the D key is still being pressed this current frame More...
|
|
def | key_d_just_not_pressed (self) |
| indicates whether the D key has just been released this current frame More...
|
|
def | key_d_still_not_pressed (self) |
| indicates whether the D key is not pressed and has not been pressed for more than a frame More...
|
|
def | key_d_fire (self) |
| indicates whether the current frame is a fire frame for the D key More...
|
|
def | key_d_setup_fire (self, int f) |
|
def | game_base_init (self, id, log, key, rows, cols, debug=False) |
| Initializes the gamebase object. More...
|
|
def | close (self) |
| close the socket connection (and game) More...
|
|
def | register_keypress (self, kl) |
|
def | initialize (self) |
|
def | game_loop (self) |
|
def | quit (self) |
| calling this function causes the game to end. More...
|
|
def | set_title (self, title) |
| sets title of game More...
|
|
def | set_description (self, desc) |
| sets description of the game More...
|
|
def | get_bg_color (self, row, col) |
| gets background color of a cell More...
|
|
def | set_bg_color (self, row, col, color) |
| sets background color of a cell More...
|
|
def | get_symbol (self, row, col) |
| gets symbol of the cell at row, col More...
|
|
def | get_symbol_color (self, row, col) |
| gets symbol color of the cell at row, col More...
|
|
def | draw_symbol (self, row, col, s, c) |
| draw symbol s with color col at cell (row, col) More...
|
|
def | render (self) |
| renders the board More...
|
|
def | board_width (self) |
| setter/getter property for board width More...
|
|
def | board_height (self) |
| setter/getter property for board height More...
|
|