1

I am writing a roguelike in python with libtcod. movement worked fine before I switched to object orientation. When I run my python game it pops up but as soon as I try to move the player, it freezes. Here is my code:

import libtcodpy as libtcod;

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

class Object:
    def __init__(self, x, y, char, color):
        self.x = x
        self.y = y
        self.char = char
        self.color = color

    def move(self, dx, dy):
        self.x = dx
        self.y = dy

    def draw(self):
        #libtcod.console_set_default_foreground(con, self.color)
        libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)

    def clear(self):
        libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)

def handle_keys():
    key = libtcod.console_check_for_keypress()
    if key.vk == libtcod.KEY_ENTER and key.lalt:
        libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

    elif key.vk == libtcod.KEY_ESCAPE:
        return True  #exit game

    if libtcod.console_is_key_pressed(libtcod.KEY_UP):
        player.move(0, -1)

    elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
        player.move(0, 1)

    elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
        player.move(-1, 0)

    elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
        player.move(1, 0)

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);
con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)

player = Object(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, '@', libtcod.white)
#npc = Object(SCREEN_WIDTH/2 - 5, SCREEN_HEIGHT/2, '@', libtcod.yellow)
objects = [player]

while not libtcod.console_is_window_closed():

    for object in objects:
        object.draw()


    #libtcod.console_check_for_keypress()
    libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
    libtcod.console_flush();

    for object in objects:
        object.clear()

    exit = handle_keys()
    if exit:
        break

It might have something to do with the while loop and movement... ugh i don't know

sloth
  • 99,095
  • 21
  • 171
  • 219
Josh King
  • 177
  • 2
  • 2
  • 7

1 Answers1

1

Maybe instead of

def move(self, dx, dy):
    self.x = dx
    self.y = dy

you want

def move(self, dx, dy):
    self.x += dx
    self.y += dy

Otherwise, you just set the position of the player to (-1, 0) (outside the screen) if you press KEY_LEFT for example. Maybe that is why you think it is frozen.

Beside that, your code is working fine for me.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • Ugh I'm retarded. Thank you. Are you familiar with Libtcod? I cannot use any colors – Josh King Feb 21 '13 at 09:50
  • Do you know why the colors are not working at all? everything is white even when i set it to another color – Josh King Feb 21 '13 at 09:52
  • If I uncomment the line `libtcod.console_set_default_foreground(con, self.color)` colors work as expected. – sloth Feb 21 '13 at 09:59
  • Traceback (most recent call last): File "C:\Users\Josh\Desktop\RLDEV\RLGame.py", line 57, in object.draw() File "C:\Users\Josh\Desktop\RLDEV\RLGame.py", line 19, in draw libtcod.console_set_default_foreground(con, self.color) AttributeError: 'module' object has no attribute 'console_set_default_foreground ' Press any key to continue . . . – Josh King Feb 21 '13 at 10:01
  • which version do you have installed? It should work with 1.5.2. IIRC the python bindings changed between 1.5.1 <-> 1.5.2 or something like that. – sloth Feb 21 '13 at 10:05
  • I have 1.5.0. Can i instal 1.5.1 without anything bad happening? – Josh King Feb 21 '13 at 10:07
  • I have [written a game that used 1.5.1](http://hg.barrelburst.net/domeriarl/src), and the [function to set the color seems to be `console_set_foreground_color`](http://hg.barrelburst.net/domeriarl/src/d9a99e7ca119d12d4251b4741034e631c23d991f/src/objects/actor.py?at=default#cl-45) instead of `console_set_default_foreground`. Maybe you want to try that first. – sloth Feb 21 '13 at 10:10
  • i downloaded 1.5.1 and switched the files. Colors are now working. thanks for your help man. You will probably be seeing me again :D – Josh King Feb 21 '13 at 10:14