"IndexError: list index out of range"

This is the place for queries that don't fit in any of the other categories.

"IndexError: list index out of range"

Postby AnimusRex » Wed Sep 11, 2013 10:18 pm

I just can't see for the life of me where the hell it is in my programming.

I get three errors;

Code: Select all
"line 88, in create_v_tunnel
    map[x][y].blocked = False"
"line 138, in make_map
    create_v_tunnel(prev_y, new_y, new_x)"
"line 223, in <module>
    make_map()"



Anyway; here's the rest of my code, I've just been using a roguelike programming tutorial.

Code: Select all
import libtcodpy as lib

screen_width = 130
screen_height = 90

map_width = 120
map_height = 85

room_max_size = 10
room_min_size = 6
max_rooms = 30

limit_fps = 20

color_dark_wall = lib.darkest_sepia
color_dark_ground = lib.sepia

class Tile:
   #a tile of the map and its properties
    def __init__(self, blocked, block_sight = None):
        self.blocked = blocked
 
      #by default, if a tile is blocked, it also blocks sight
        if block_sight is None: block_sight = blocked
        self.block_sight = block_sight

class Rect:
   #a rectangle on the map. used to characterize a room.
    def __init__(self, x, y, w, h):
        self.x1 = x
        self.y1 = y
        self.x2 = x + w
        self.y2 = y + h
      
    def center(self):
        center_x = (self.x1 + self.x2) / 2
        center_y = (self.x1 + self.x2) / 2
        return (center_x, center_y)
      
    def intersect(self, other):
   #Returns true if this rectangle intersects another
        return (self.x1 <= other.x2 and self.x2 >= other.x1 and
                self.y1 <= other.y2 and self.y2 >= other.y1)

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
        if not map[self.x + dx][self.y + dy].blocked:
            self.x += dx
            self.y += dy
         
    def draw(self):
      #set the color and then draw the character that represents this object at its position
        lib.console_set_default_foreground(con, self.color)
        lib.console_put_char(con, self.x, self.y, self.char, lib.BKGND_NONE)
 
    def clear(self):
      #erase the character that represents this object
        lib.console_put_char(con, self.x, self.y, ' ', lib.BKGND_NONE)

def create_room(room):
    global map
   #go through the tiles in the rectangle and make them passable
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[x][y].blocked = False
            map[x][y].block_sight = False

def create_h_tunnel(x1, x2, y):
    global map
    #horizontal tunnel. min() and max() are used in case x1>x2
    for x in range(min(x1, x2), max(x1, x2) + 1):
        map[x][y].blocked = False
        map[x][y].block_sight = False
 
def create_v_tunnel(y1, y2, x):
    global map
    #vertical tunnel
    for y in range(min(y1, y2), max(y1, y2) + 1):
        map[x][y].blocked = False
        map[x][y].block_sight = False

def make_map():
    global map, player
 
    map = [[ Tile(True)
   for y in range(map_height) ]
      for x in range(map_width) ]

    rooms = []
    num_rooms = 0
   
    for r in range(max_rooms):
    #Random width and height
        w = lib.random_get_int(0, room_min_size, room_max_size)
        h = lib.random_get_int(0, room_min_size, room_max_size)
    #Random position without going outside the map
        x = lib.random_get_int(0, 0, map_width - w - 1)
        y = lib.random_get_int(0, 0, map_height - h - 1)

        new_room = Rect(x, y, w, h)
           
    #Check intersections
        failed = False
        for other_room in rooms:
            if new_room.intersect(other_room):
                failed = True
                break
                                                                                           
        if not failed:
                           
            create_room(new_room)
                                                           
            (new_x, new_y) = new_room.center()
                                                           
            if num_rooms == 0:
                player.x = new_x
                player.y = new_y
                                                                                           
            else:
                                #all rooms after the first:
                                #connect it to the previous room with a tunnel
                               
                                #Center coordinate of previous room
                (prev_x, prev_y) = rooms[num_rooms-1].center()
                               
                if lib.random_get_int(0, 0, 1) == 1:
                        #First horizontally then vertically
                    create_h_tunnel(prev_x, new_x, prev_y)
                    create_v_tunnel(prev_y, new_y, new_x)
                else:
                        #The opposite
                    create_v_tunnel(prev_y, new_y, prev_x)
                    create_h_tunnel(prev_x, new_x, new_y)
                                       
            rooms.append(new_room)
            num_rooms += 1



def render_all():

   #draw all objects in the list
    global color_dark_wall, color_light_wall
    global color_dark_ground, color_light_ground

    for y in range(map_height):
        for x in range(map_width):
            wall = map[x][y].block_sight
            if wall:
                lib.console_set_char_background(con, x, y, color_dark_wall, lib.BKGND_SET )
            else:
                lib.console_set_char_background(con, x, y, color_dark_ground, lib.BKGND_SET )
            
    for object in objects:
        object.draw()
            
    lib.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)

def handle_keys():
   #key = libtcod.console_check_for_keypress()  #real-time
   key = lib.console_wait_for_keypress(True)  #turn-based

   if key.vk == lib.KEY_ENTER and key.lalt:
      #Alt+Enter: toggle fullscreen
      lib.console_set_fullscreen(not lib.console_is_fullscreen())

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

   #movement keys
   if lib.console_is_key_pressed(lib.KEY_KP8):
      player.move(0, -1)

   elif lib.console_is_key_pressed(lib.KEY_KP9):
      player.move(1, -1)

   elif lib.console_is_key_pressed(lib.KEY_KP6):
      player.move(1, 0)

   elif lib.console_is_key_pressed(lib.KEY_KP3):
      player.move(1, 1)

   elif lib.console_is_key_pressed(lib.KEY_KP2):
      player.move(0, 1)

   elif lib.console_is_key_pressed(lib.KEY_KP1):
      player.move(-1, 1)

   elif lib.console_is_key_pressed(lib.KEY_KP4):
      player.move(-1, 0)

   elif lib.console_is_key_pressed(lib.KEY_KP7):
      player.move(-1, -1)

   elif lib.console_is_key_pressed(lib.KEY_KP5):
      player.move(0, 0)



#MAIN LOOP BELOW

lib.console_set_custom_font('terminal12x12_gs_ro.png', lib.FONT_TYPE_GREYSCALE | lib.FONT_LAYOUT_ASCII_INROW)
lib.console_init_root(screen_width, screen_height, 'python/libtcod tutorial', False)
lib.sys_set_fps(limit_fps)

con = lib.console_new(screen_width, screen_height)

player = Object(screen_width/2, screen_height/2, 'M', lib.silver)

audiolog1 = Object(55, 23, 'L', lib.white)

objects = (player, audiolog1)

make_map()

while not lib.console_is_window_closed():

    render_all()
   
    lib.console_flush()

#Erase all objects at their old location
    for object in objects:
        object.clear()

    exit = handle_keys()
    if exit:
        break
Last edited by micseydel on Thu Sep 12, 2013 12:25 am, edited 1 time in total.
Reason: First post lock.
AnimusRex
 
Posts: 2
Joined: Wed Sep 11, 2013 10:14 pm

Re: "IndexError: list index out of range"

Postby micseydel » Thu Sep 12, 2013 1:36 am

It would help us to help you if you posted the full traceback, as well as a 10 line or shorter snippet of runnable code that reproduces the problem.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1303
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: "IndexError: list index out of range"

Postby Mekire » Thu Sep 12, 2013 1:37 am

What is the actual exception? Is it an IndexError? If so, add a print line in the create_v_tunnel function to find out exactly what the values of x and y are when the program crashes. I unfortunately don't have access to an interpreter right now so I can't really test anything.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: "IndexError: list index out of range" [SOLVED]

Postby AnimusRex » Thu Sep 12, 2013 2:50 am

I've got it figured out; thanks Mekire, that's basically what I did.
AnimusRex
 
Posts: 2
Joined: Wed Sep 11, 2013 10:14 pm

Re: "IndexError: list index out of range"

Postby vijaykumbhani » Fri Sep 13, 2013 4:43 am

you use list in your program
but interpreter can not find particular your's choice index
then your enter index in not in list...
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India


Return to General Coding Help

Who is online

Users browsing this forum: Majestic-12 [Bot] and 3 guests

cron