0

Today I progressed further into this Python roguelike tutorial, and got to the inventory. As of now, I can pick up items and use them. The only problem is, when accessing the inventory, it's only visible for a split second, even though I used the console_wait_for_keypress(True) function. I'm not sure as to why it disappears. Here's the code that displays a menu(in this case, the inventory):

def menu(header,options,width):
    if len(options)>26: raise ValueError('Cannot have a menu with more than 26 options.')

    header_height=libtcod.console_get_height_rect(con,0,0,width,SCREEN_HEIGHT,header)
    height=len(options)+header_height

    window=libtcod.console_new(width,height)

    libtcod.console_set_default_foreground(window,libtcod.white)
    libtcod.console_print_rect_ex(window,0,0,width,height,libtcod.BKGND_NONE,libtcod.LEFT,header)

    y=header_height
    letter_index=ord('a')
    for option_text in options:
        text='('+chr(letter_index)+')'+option_text
        libtcod.console_print_ex(window,0,y,libtcod.BKGND_NONE,libtcod.LEFT,text)
        y+=1
        letter_index+=1

    x=SCREEN_WIDTH/2-width/2
    y=SCREEN_HEIGHT/2-height/2
    libtcod.console_blit(window,0,0,width,height,0,x,y,1.0,0.7)

    libtcod.console_flush()
    key=libtcod.console_wait_for_keypress(True)

    index=key.c-ord('a')
    if index>=0 and index<len(options): return index
    return None

I'd appreciate anyone's help or input to this problem.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
Jeremy Darrach
  • 273
  • 1
  • 6
  • 18
  • "The python roguelike tutorial"? A specific one? I don't think whatever you're working from is as well-known as you expect. – Karl Knechtel Mar 09 '13 at 03:16
  • Oh yeah, yesterday I made a post about this tutorial... here's the link to the tutorial: http://roguebasin.roguelikedevelopment.org/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod – Jeremy Darrach Mar 09 '13 at 03:20

2 Answers2

0

It may be related to an old version of the library that has an event when you press a key, and another event when you release it. So this could cause it to appear and disappear when you release the key.

So try to see if the screen stays on if you keep the key pressed.

Abalieno
  • 159
  • 1
  • 1
  • 5
  • When I keep the key pressed, it's like the inventory screen pops up and then disappears really quickly. Then when I let go of the key, the inventory screen stays up (only after holding the key in for like a few seconds). This is a real puzzler for me. – Jeremy Darrach Mar 09 '13 at 15:04
0

wait_for_keypress does indeed trigger on both press and release events. To fix this, replace wait_for_keypress with the sys_wait_for_event, specifying to trigger only on press events.

Documentation

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Keko
  • 1