libtcod/Python: CPU usage through the roof using sys_check_for_event instead of sys_wait_for_event

219 Views Asked by At

I'm building a game in Python 3 using the CFFI port of libtcod.

Handling user input works very well with the sys_wait_for_event method (both keyboard and mouse events are immediately captured), however this prevents me from making the game realtime as the method only returns an event when the user does something, not when I want the game to (for example) move enemies around.

I can switch to sys_check_for_event but somehow this sends CPU usage to 100% and seemingly doesn't capture mouse movements. It's almost as if it's checking for events too often. Trying to slow it down by calling time.sleep every loop doesn't work either.

I use the following code:

import tcod

tcod.console_set_custom_font('terminal16x16_gs_ro.png', 
                             tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_ASCII_INROW)
cod.console_init_root(40, 40, 'My game', False)
tcod.sys_set_fps(60)

key = tcod.Key()
mouse = tcod.Mouse()

while not tcod.console_is_window_closed():
    # listen for user input, which sends CPU usage to 100%
    event = tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse)

    # do stuff
    some_handling_method(key, mouse)              

    # flush consoles to the window
    tcod.console_flush()

How can I fix this issue -

  • is there a way to use sys_wait_for_event until some time has passed, after which I can let the game update its state without user input,

  • is there something wrong with sys_check_for_event or

  • am I using it wrong?

Thanks!

Update I think there's a bug in sys_check_for_event: it returns all events except for mouse movements. I tested this by looking at the (x, y) coordinates in the mouse object - even when these numbers change, the output of the method remains 0 i.e. no events.

0

There are 0 best solutions below