KeyBinding Module

This module implement a simple way to store and represent key binding for Bomberman game. The class KeyBinding can be used by any other modules.

Currently, the representation of keyboard caracter is the one used by PyGame (SDL python binding).

For example :

  • K_a to K_z represent the letter a to z,
  • K_TAB, K_SPACE, K_SLASH etc,
  • a complete list is stored in the variable LIST_of_authorized_keys.
KeyBinding.LIST_of_authorized_keys = ['K_0', 'K_1', 'K_2', 'K_3', 'K_4', 'K_5', 'K_6', 'K_7', 'K_8', 'K_9', 'K_AMPERSAND', 'K_ASTERISK', 'K_AT', 'K_BACKQUOTE', 'K_BACKSLASH', 'K_BACKSPACE', 'K_BREAK', 'K_CAPSLOCK', 'K_CARET', 'K_CLEAR', 'K_COLON', 'K_COMMA', 'K_DELETE', 'K_DOLLAR', 'K_DOWN', 'K_END', 'K_EQUALS', 'K_ESCAPE', 'K_EURO', 'K_EXCLAIM', 'K_F1', 'K_F10', 'K_F11', 'K_F12', 'K_F13', 'K_F14', 'K_F15', 'K_F2', 'K_F3', 'K_F4', 'K_F5', 'K_F6', 'K_F7', 'K_F8', 'K_F9', 'K_FIRST', 'K_GREATER', 'K_HASH', 'K_HELP', 'K_HOME', 'K_INSERT', 'K_KP0', 'K_KP1', 'K_KP2', 'K_KP3', 'K_KP4', 'K_KP5', 'K_KP6', 'K_KP7', 'K_KP8', 'K_KP9', 'K_KP_DIVIDE', 'K_KP_ENTER', 'K_KP_EQUALS', 'K_KP_MINUS', 'K_KP_MULTIPLY', 'K_KP_PERIOD', 'K_KP_PLUS', 'K_LALT', 'K_LAST', 'K_LCTRL', 'K_LEFT', 'K_LEFTBRACKET', 'K_LEFTPAREN', 'K_LESS', 'K_LMETA', 'K_LSHIFT', 'K_LSUPER', 'K_MENU', 'K_MINUS', 'K_MODE', 'K_NUMLOCK', 'K_PAGEDOWN', 'K_PAGEUP', 'K_PAUSE', 'K_PERIOD', 'K_PLUS', 'K_POWER', 'K_PRINT', 'K_QUESTION', 'K_QUOTE', 'K_QUOTEDBL', 'K_RALT', 'K_RCTRL', 'K_RETURN', 'K_RIGHT', 'K_RIGHTBRACKET', 'K_RIGHTPAREN', 'K_RMETA', 'K_RSHIFT', 'K_RSUPER', 'K_SCROLLOCK', 'K_SEMICOLON', 'K_SLASH', 'K_SPACE', 'K_SYSREQ', 'K_TAB', 'K_UNDERSCORE', 'K_UNKNOWN', 'K_UP', 'K_a', 'K_b', 'K_c', 'K_d', 'K_e', 'K_f', 'K_g', 'K_h', 'K_i', 'K_j', 'K_k', 'K_l', 'K_m', 'K_n', 'K_o', 'K_p', 'K_q', 'K_r', 'K_s', 'K_t', 'K_u', 'K_v', 'K_w', 'K_x', 'K_y', 'K_z']

The list of all valid keys. Can be simply created with :

>>> a = []
>>> for i in dir(pygame):
>>>  if i[2:] == 'K_': a.append(i)
>>> return a
KeyBinding.DICT_of_authorized_keys = {'K_KP_MINUS': 269, 'K_F1': 282, 'K_F2': 283, 'K_F3': 284, 'K_COLON': 58, 'K_F5': 286, 'K_F6': 287, 'K_F7': 288, 'K_F8': 289, 'K_F9': 290, 'K_COMMA': 44, 'K_PAUSE': 19, 'K_F4': 285, 'K_AMPERSAND': 38, 'K_CLEAR': 12, 'K_KP_PLUS': 270, 'K_KP_EQUALS': 272, 'K_SPACE': 32, 'K_INSERT': 277, 'K_HOME': 278, 'K_LSUPER': 311, 'K_GREATER': 62, 'K_FIRST': 0, 'K_KP_PERIOD': 266, 'K_RALT': 307, 'K_BREAK': 318, 'K_RIGHTBRACKET': 93, 'K_RSHIFT': 303, 'K_LSHIFT': 304, 'K_LEFTPAREN': 40, 'K_LAST': 323, 'K_9': 57, 'K_KP_ENTER': 271, 'K_8': 56, 'K_HASH': 35, 'K_DOWN': 274, 'K_END': 279, 'K_RSUPER': 312, 'K_ASTERISK': 42, 'K_LCTRL': 306, 'K_BACKSLASH': 92, 'K_MINUS': 45, 'K_EXCLAIM': 33, 'K_HELP': 315, 'K_POWER': 320, 'K_ESCAPE': 27, 'K_BACKSPACE': 8, 'K_MENU': 319, 'K_UNDERSCORE': 95, 'K_QUOTEDBL': 34, 'K_KP_MULTIPLY': 268, 'K_LEFTBRACKET': 91, 'K_LALT': 308, 'K_KP_DIVIDE': 267, 'K_NUMLOCK': 300, 'K_RMETA': 309, 'K_LEFT': 276, 'K_RIGHT': 275, 'K_EQUALS': 61, 'K_SYSREQ': 317, 'K_SEMICOLON': 59, 'K_QUESTION': 63, 'K_UNKNOWN': 0, 'K_EURO': 321, 'K_PERIOD': 46, 'K_r': 114, 'K_DELETE': 127, 'K_CARET': 94, 'K_LMETA': 310, 'K_TAB': 9, 'K_MODE': 313, 'K_SLASH': 47, 'K_F12': 293, 'K_F13': 294, 'K_F10': 291, 'K_F11': 292, 'K_F14': 295, 'K_F15': 296, 'K_y': 121, 'K_x': 120, 'K_z': 122, 'K_q': 113, 'K_p': 112, 'K_s': 115, 'K_n': 110, 'K_u': 117, 'K_t': 116, 'K_w': 119, 'K_v': 118, 'K_i': 105, 'K_h': 104, 'K_k': 107, 'K_j': 106, 'K_m': 109, 'K_l': 108, 'K_o': 111, 'K_UP': 273, 'K_a': 97, 'K_c': 99, 'K_b': 98, 'K_e': 101, 'K_d': 100, 'K_g': 103, 'K_f': 102, 'K_AT': 64, 'K_PAGEUP': 280, 'K_CAPSLOCK': 301, 'K_LESS': 60, 'K_PRINT': 316, 'K_RETURN': 13, 'K_SCROLLOCK': 302, 'K_DOLLAR': 36, 'K_PAGEDOWN': 281, 'K_1': 49, 'K_0': 48, 'K_3': 51, 'K_2': 50, 'K_5': 53, 'K_4': 52, 'K_7': 55, 'K_6': 54, 'K_PLUS': 43, 'K_BACKQUOTE': 96, 'K_QUOTE': 39, 'K_RIGHTPAREN': 41, 'K_RCTRL': 305, 'K_KP8': 264, 'K_KP9': 265, 'K_KP4': 260, 'K_KP5': 261, 'K_KP6': 262, 'K_KP7': 263, 'K_KP0': 256, 'K_KP1': 257, 'K_KP2': 258, 'K_KP3': 259}

A dict of values associated to valid keys. Can be simply created with

DICT_of_authorized_keys = {}
tmp = 0
for k in LIST_of_authorized_keys:
 exec("tmp=%s" % k)
 DICT_of_authorized_keys[k] = tmp
KeyBinding.print_keynum_as_str(keynum)[source]

Trying to print the keynum as a key name. The list of valid pygame keynames are in LIST_of_authorized_keys.

class KeyBinding.KeyBinding(up=['K_UP'], down=['K_DOWN'], left=['K_LEFT'], right=['K_RIGHT'], bomb=['K_SPACE'], help=['K_h'])[source]

Bases: object

A Class to store and represent key binding for Bomberman game. Keys are represented as a list of a pygame representation of a keyboard touch

Attributes:
  • up – the keys to go up / north.
  • down – the keys to go down / south.
  • left – the keys to go left / east.
  • right – the keys to go right / west.
  • bomb – the keys to drop bombs.
  • help – the keys to print help.

This class can be used both by the client and the server.

Example:
>>> kbl=KeyBinding()
>>> kbl.help=['K_DELETE'] # Override the default value.
>>> kbl.bomb.append('K_DOLLAR') # Add a new key to drop bombs !
>>> # To print a simple explanation about the keys :
>>> print kbl.help()
>>> # To redefine the keys, one by one :
>>> kbl.ask()
__init__(up=['K_UP'], down=['K_DOWN'], left=['K_LEFT'], right=['K_RIGHT'], bomb=['K_SPACE'], help=['K_h'])[source]

Simple constructor of a key binding.

up = None

the keys to go up / north.

down = None

the keys to go down / south.

left = None

the keys to go left / east.

right = None

the keys to go right / west.

bomb = None

the keys to drop bombs.

help = None

the keys to print help.

value_up = None

the values of keys to go up / north.

value_down = None

the values of keys to go down / south.

value_left = None

the values of keys to go left / east.

value_right = None

the values of keys to go right / west.

value_bomb = None

the values of keys to drop bombs.

value_help = None

the values of keys to print help.

__str__(self) → str[source]

A pretty string for the key bindings.

__repr__()

__str__(self) -> str A pretty string for the key bindings.

get_help()[source]

Just return a string explaining the key binding list.

ask()[source]

A way to ask interactivly, in text mod in console, to redefine the key binding list.

ok(inp, action='up')[source]

A shortcut to : inp in key.action. Allow to check if the key [inp] is valid for the move [action].

action is one of : up, down, left, right, bomb, help.

values()[source]

Make 6 attributes value_up,..,value_help.

event_key_is_ok(key, action='up')[source]

To check if the Pygame event key is valid for the move [action].

action is one of : up, down, left, right, bomb, help.

__weakref__

list of weak references to the object (if defined)

KeyBinding.classic()[source]

Return a simple key binding, like the one used in WoW or Skyrim (classic with direction with Z up, Q left, S down, D right).

KeyBinding.arrows()[source]

Return a simple key binding, like the one used in Lionheart or Mario.

Table Of Contents

Previous topic

Board Module

Next topic

ParseMessageIn Module

This Page