我正在使用libtcod在python中编写roguelike。在切换到面向对象之前,运动正常。当我运行我的python游戏时会弹出,但是一旦我尝试移动播放器,它就会冻结。 这是我的代码:
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
它可能与while循环和运动有关......呃我不知道
答案 0 :(得分:1)
也许不是
def move(self, dx, dy):
self.x = dx
self.y = dy
你想要
def move(self, dx, dy):
self.x += dx
self.y += dy
否则,如果按(-1, 0)
,则只需将播放器的位置设置为KEY_LEFT
(屏幕外)。也许这就是为什么你认为它被冻结了。
除此之外,您的代码对我来说还不错。