Keystrokes/Controls in libtcod and python?
In the Python/Libtcod tutorial on Roguebasin the basic code for controlling your character uses the up down left and right keys. Is there a way to make it use WSAD or any other keys? Libtcod only allows me to use "special" keys, like the arrow keys, PGDN/PGUP, F1 F2 F3, but not regular alphanumeric keys.
#movement keys
def handle_keys():
global playerx, playery
if libtcod.console_is_key_pressed(libtcod.KEY_UP):
playery -= 1
elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
playery += 1
elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
pl开发者_运维百科ayerx -= 1
elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
playerx += 1
You'll have to do something like this:
key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED)
if key.vk == libtcod.KEY_CHAR:
if key.c == ord('w'):
playery -= 1
elif key.c == ord('s'):
playery += 1
elif key.c == ord('a'):
playerx -= 1
elif key.c == ord('d'):
playerx += 1
Check doc\console\console_check_for_keypress.html
and doc\console\key_t.html
in your libtcod
folder.
You could also use only the ASCII code for each key to save code space:
if key.c == 119: #w
playery -= 1
elif key.c == 115: #s
playery += 1
elif key.c == 97: #a
playerx -= 1
elif key.c == 100: #d
playerx += 1
精彩评论