Bridges-Python-3.0.2  3.0.2
Bridges(PythonAPI)
Public Member Functions | Public Attributes | List of all members
bridges.non_blocking_game.NonBlockingGame Class Reference
Inheritance diagram for bridges.non_blocking_game.NonBlockingGame:
bridges.gamebase.GameBase

Detailed Description

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); draw_symbol(0, 0, NamedSymbol.sword, NamedColor.blue); endcode

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} my_game = NonBlockingGame (1, "myuserid", "myapikey", 16, 64) endcode

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_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.

code{.py} def gameLoop(): if key_up() set_bg_color(rand()%10, rand()%10, NamedColor.lightsalmon); endcode

Author
Erik Saule
Date
72119

NonBlockingGame tutorial at: http://bridgesuncc.github.io/tutorials/NonBlockingGame.html

Public Member Functions

def __init__ (self, assid, login, apikey, cols, rows)
 
def fps (self)
 What frame rate is the game running at? More...
 
def fps (self, frames)
 
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.gamebase.GameBase
def __init__ (self, assid, login, apikey, cols, rows)
 
def game_base_init (self, id, log, key, c, r)
 
def close (self)
 
def register_keypress (self, kl)
 
def start (self)
 
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...
 

Public Attributes

 time_of_last_frame
 
 ih
 
 game_started
 
- Public Attributes inherited from bridges.gamebase.GameBase
 grid_state
 
 firsttime
 
 bridges
 
 grid
 
 sock
 
 game_started
 

Additional Inherited Members

- Static Public Attributes inherited from bridges.gamebase.GameBase
bool debug = True
 

Constructor & Destructor Documentation

◆ __init__()

def bridges.non_blocking_game.NonBlockingGame.__init__ (   self,
  assid,
  login,
  apikey,
  cols,
  rows 
)

Member Function Documentation

◆ control_framerate()

def bridges.non_blocking_game.NonBlockingGame.control_framerate (   self)

◆ fps() [1/2]

def bridges.non_blocking_game.NonBlockingGame.fps (   self)

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

◆ fps() [2/2]

def bridges.non_blocking_game.NonBlockingGame.fps (   self,
  frames 
)

◆ key_a()

def bridges.non_blocking_game.NonBlockingGame.key_a (   self)

Is a currently pressed?

Returns
true if a is currently pressed

◆ key_d()

def bridges.non_blocking_game.NonBlockingGame.key_d (   self)

Is right currently pressed?

Returns
true d right is currently pressed

◆ key_down()

def bridges.non_blocking_game.NonBlockingGame.key_down (   self)

Is down currently pressed?

Returns
true if down is currently pressed

◆ key_left()

def bridges.non_blocking_game.NonBlockingGame.key_left (   self)

Is left currently pressed?

Returns
true if left is currently pressed

◆ key_q()

def bridges.non_blocking_game.NonBlockingGame.key_q (   self)

Is q currently pressed?

Returns
true if q is currently pressed

◆ key_right()

def bridges.non_blocking_game.NonBlockingGame.key_right (   self)

Is right currently pressed?

Returns
true if right is currently pressed

◆ key_s()

def bridges.non_blocking_game.NonBlockingGame.key_s (   self)

Is s currently pressed?

Returns
true if s is currently pressed

◆ key_space()

def bridges.non_blocking_game.NonBlockingGame.key_space (   self)

Is space currently pressed?

Returns
true if space is currently pressed

◆ key_up()

def bridges.non_blocking_game.NonBlockingGame.key_up (   self)

Is up currently pressed?

Returns
true if up is currently pressed

◆ key_w()

def bridges.non_blocking_game.NonBlockingGame.key_w (   self)

Is w currently pressed?

Returns
true if w is currently pressed

◆ sleep_timer()

def bridges.non_blocking_game.NonBlockingGame.sleep_timer (   self,
  timems = None 
)

◆ start()

def bridges.non_blocking_game.NonBlockingGame.start (   self)

Call this function from main to start game.

Member Data Documentation

◆ game_started

bridges.non_blocking_game.NonBlockingGame.game_started

◆ ih

bridges.non_blocking_game.NonBlockingGame.ih

◆ time_of_last_frame

bridges.non_blocking_game.NonBlockingGame.time_of_last_frame

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