Bridges-Python
3.4.4
Bridges(Python API)
|
brief 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: code{.py} def initialize(): set_bg_color(0, 0, NamedColor.lightsalmon);
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{.py}
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.
code{.py} def game_oop(): set_BG_Color(rand()%10, rand()%10, NamedColor.lightsalmon); endcode
The gameLoop can also probe the state of some keys of the keyboard using functions key_up(), key_left(),
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.
code{.py} def gameLoop(): if key_up()
NonBlockingGame tutorial at: https://bridgesuncc.github.io/tutorials/NonBlockingGame.html
Public Member Functions | |
def | __init__ (self, assid, login, apikey) |
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 currently pressed? More... | |
def | key_right (self) |
Is right currently pressed? More... | |
def | key_up (self) |
Is up currently pressed? More... | |
def | key_down (self) |
Is down currently pressed? More... | |
def | key_q (self) |
Is q currently pressed? More... | |
def | key_space (self) |
Is space currently pressed? More... | |
def | key_w (self) |
Is w currently pressed? More... | |
def | key_a (self) |
Is a currently pressed? More... | |
def | key_s (self) |
Is s currently pressed? More... | |
def | key_d (self) |
Is right currently pressed? More... | |
Public Member Functions inherited from bridges.gamebase3D.GameBase3D | |
def | game_base_init (self, id, log, key) |
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 | render (self) |
renders the scene More... | |
Public Attributes | |
time_of_last_frame | |
ih | |
game_started | |
Public Attributes inherited from bridges.gamebase3D.GameBase3D | |
firsttime | |
bridges | |
sock | |
scene | |
game_started | |
Additional Inherited Members | |
Static Public Attributes inherited from bridges.gamebase3D.GameBase3D | |
bool | debug = True |
def bridges.non_blocking_game3D.NonBlockingGame3D.__init__ | ( | self, | |
assid, | |||
login, | |||
apikey | |||
) |
PROTECTED constructor prevents the object from being directly created.
Since GameBase is meant to be a purely internal class, that seems appropriate.
assid | assignment number |
login | user name |
apikey | authentication id |
rows | height of game grid |
cols | width of game grid |
Reimplemented from bridges.gamebase3D.GameBase3D.
def bridges.non_blocking_game3D.NonBlockingGame3D.control_framerate | ( | self | ) |
def bridges.non_blocking_game3D.NonBlockingGame3D.fps | ( | self | ) |
Get the frame rate at which the game running.
def bridges.non_blocking_game3D.NonBlockingGame3D.fps | ( | self, | |
frames | |||
) |
Set the frame rate at which the game running.
frames | frame rate to be set. |
def bridges.non_blocking_game3D.NonBlockingGame3D.key_a | ( | self | ) |
Is a currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_d | ( | self | ) |
Is right currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_down | ( | self | ) |
Is down currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_left | ( | self | ) |
Is left currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_q | ( | self | ) |
Is q currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_right | ( | self | ) |
Is right currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_s | ( | self | ) |
Is s currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_space | ( | self | ) |
Is space currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_up | ( | self | ) |
Is up currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.key_w | ( | self | ) |
Is w currently pressed?
def bridges.non_blocking_game3D.NonBlockingGame3D.sleep_timer | ( | self, | |
timems = None |
|||
) |
def bridges.non_blocking_game3D.NonBlockingGame3D.start | ( | self | ) |
Call this function from main to start game.
Reimplemented from bridges.gamebase3D.GameBase3D.
bridges.non_blocking_game3D.NonBlockingGame3D.game_started |
bridges.non_blocking_game3D.NonBlockingGame3D.ih |
bridges.non_blocking_game3D.NonBlockingGame3D.time_of_last_frame |