简体   繁体   中英

Python program not responding when I run it

Im making a roguelike in python using Libtcod. When I run the code, the window pops up, drawing the symbol on the screen but the window then freezes. It then says it is not responding. I do not understand whats happening. Here is the code:

import libtcodpy as libtcod;

SCREEN_WIDTH = 80;
SCREEN_HEIGHT = 50;
LIMIT_FPS = 20;

libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD);

libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'Lets Crawl', False);

libtcod.sys_set_fps(LIMIT_FPS);

playerx = SCREEN_WIDTH/2;
playery = SCREEN_HEIGHT/2;

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):
        playerx -= 1

    elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
        playerx += 1


while not libtcod.console_is_window_closed():
    #libtcod.console_set_default_foreground(0, libtcod.white);
    libtcod.console_put_char(0, 1, 1, 'b');
    libtcod.console_flush();

Your game is not responding because it runs an infinite loop (your while loop).

You should call libtcod.console_wait_for_keypress(True) or libtcod.console_check_for_keypress() somewhere in your loop to actually be able to handle user input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM