First of all I'm using pythong 2.7.3 and libtcod 1.5.1
I am following a tutorial found at http://roguebasin.roguelikedevelopment.org/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod,_part_2 and have code matching (as far as I can tell) exactly what it is telling me to do.
My code is below. The general idea is to create a white and yellow @ , the white @ can be controlled with the arrow keys. However when I run this program code I get the following traceback when I try and press an arrow key only.
Traceback (most recent call last): line 70 in <module> for object in object: TypeError: iteration over non-sequence
Press any key to continue etc. Note I'm using Notepad ++ not the IDLE at the suggestion of the tutorial
import libtcodpy as libtcod
#actual size of the widnow
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
LIMIT_FPS = 20
class Object:
#this is a generic object: the player, a monster, an item, the stairs . . .
#it's always represented by a character on screen
def __init__(self, x, y, char, color):
self.x = x
self.y = y
self.char = char
self.color = color
def move(self, dx, dy):
#move by the given amount
self.x += dx
self.y += dy
def draw(self):
#Set the color and then draw the character that represents this object at this position
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):
#erase the character that represents this object
libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)
def handle_keys():
global playerx, playery
key = libtcod.console_wait_for_keypress(True)
if key.vk == libtcod.KEY_ENTER and key.lalt:
#Alt+Enter: toggle fullscreen
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
elif key.vk == libtcod.KEY_ESCAPE:
return True #exit game
#movement keys
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)
###############################################
# Initialisation & Main Loop #
###############################################
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
#create object representign the player
player = Object(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, '@', libtcod.white)
#create an NPC
npc = Object(SCREEN_WIDTH/2 - 5, SCREEN_HEIGHT/2, '@', libtcod.yellow)
#the list of objects with those two
objects = [npc, player]
while not libtcod.console_is_window_closed():
#draw all objects in the list
for object in objects:
object.draw()
#blit the contents of "con" t othe root console and present it
libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
libtcod.console_flush()
#erase all objects at their old locations, before they move
for objects in objects:
object.clear()
#handle keys and exit game if needed
exit = handle_keys()
if exit:
break