Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器:列表索引超出范围|蛇游戏_Python_Macos_Index Error - Fatal编程技术网

Python 索引器:列表索引超出范围|蛇游戏

Python 索引器:列表索引超出范围|蛇游戏,python,macos,index-error,Python,Macos,Index Error,我正在学习Python,这就是为什么我要编写snake游戏。我想在游戏区内放张地图。我以字符串形式输入地图,然后将其拆分为行列表。问题是我得到的错误索引错误:列表索引超出范围。有人能帮我吗 import os import random import readchar POS_X = 0 POS_Y = 1 NUM_MAP_OBJECTS = 11 obstacle_definition = """\ ############### #########

我正在学习Python,这就是为什么我要编写snake游戏。我想在游戏区内放张地图。我以字符串形式输入地图,然后将其拆分为行列表。问题是我得到的错误索引错误:列表索引超出范围。有人能帮我吗

import os
import random
import readchar

POS_X = 0
POS_Y = 1

NUM_MAP_OBJECTS = 11

obstacle_definition = """\
###############    #########
        #########      #####
####        #######    #####
######  ############  ######
########       #####  ######
###########    #############
####        #########
#########    ###############
#######               ######
############    ############
############    ############
              ##############
#       ########        ####
#############       ########
##################      ####\
"""

my_position = [6, 3]
tail_length = 0
tail = []
map_objects = []

end_game = False
died = False

# Create obstacle map
obstacle_definition = [list(row) for row in obstacle_definition.split("\n")]

MAP_WIDTH = len(obstacle_definition[0])
MAP_HEIGHT = len(obstacle_definition)

# Main Loop
while not end_game:
    os.system("clear")
    # Generate random objects on the map
    while len(map_objects) < NUM_MAP_OBJECTS:
        new_position = [random.randint(0, MAP_WIDTH), random.randint(0, MAP_HEIGHT)]

        if new_position not in map_objects and new_position != my_position:
            map_objects.append(new_position)

    # Draw map
    print("+" + "-" * MAP_WIDTH * 3 + "+")

    for coordinate_y in range(MAP_HEIGHT):
        print("|", end="")

        for coordinate_x in range(MAP_WIDTH):

            char_to_draw = "  "
            object_in_cell = None
            tail_in_cell = None

            for map_object in map_objects:
                if map_object[POS_X] == coordinate_x and map_object[POS_Y] == coordinate_y:
                    char_to_draw = " *"
                    object_in_cell = map_object

            for tail_piece in tail:
                if tail_piece[POS_X] == coordinate_x and tail_piece[POS_Y] == coordinate_y:
                    char_to_draw = " @"
                    tail_in_cell = tail_piece

            if my_position[POS_X] == coordinate_x and my_position[POS_Y] == coordinate_y:
                char_to_draw = " @"

                if object_in_cell:
                    map_objects.remove(object_in_cell)
                    tail_length += 1

                if tail_in_cell:
                    end_game = True
                    died = True

            if obstacle_definition[coordinate_y][coordinate_x] == "#":
                char_to_draw = "##"

            print("{}".format(char_to_draw), end="")
        print("|")

    print("+" + "-" * MAP_WIDTH * 3 + "+")
    # print(f"La cola: {tail}")

    # Ask user where he wnats to move
    # direction = input("¿Dónde te quieres mover? [WASD]: ")
    direction = readchar.readchar()
    print(direction)

    if direction == "w":
        tail.insert(0, my_position.copy())
        tail = tail[:tail_length]
        my_position[POS_Y] -= 1
        my_position[POS_Y] %= MAP_HEIGHT
    elif direction == 's':
        tail.insert(0, my_position.copy())
        tail = tail[:tail_length]
        my_position[POS_Y] += 1
        my_position[POS_Y] %= MAP_HEIGHT
    elif direction == "a":
        tail.insert(0, my_position.copy())
        tail = tail[:tail_length]
        my_position[POS_X] -= 1
        my_position[POS_X] %= MAP_WIDTH
    elif direction == "d":
        tail.insert(0, my_position.copy())
        tail = tail[:tail_length]
        my_position[POS_X] += 1
        my_position[POS_X] %= MAP_WIDTH
    elif direction == "q":
        end_game = True

    os.system("clear")

if died:
    print(".: ¡Has muerto! :c :.")

导入操作系统
随机输入
导入readchar
位置X=0
位置Y=1
NUM_MAP_OBJECTS=11
障碍_定义=“”\
###############    #########
#########      #####
####        #######    #####
######  ############  ######
########       #####  ######
###########    #############
####        #########
#########    ###############
#######               ######
############    ############
############    ############
##############
#       ########        ####
#############       ########
##################      ####\
"""
我的位置=[6,3]
尾部长度=0
尾=[]
映射对象=[]
结束游戏=错误
死=假
#创建障碍地图
障碍物定义=[障碍物定义中的行的列表(行)。拆分(“\n”)]
地图宽度=长度(障碍物定义[0])
地图高度=长度(障碍物定义)
#主回路
虽然没有结束游戏:
操作系统(“清除”)
#在地图上生成随机对象
而len(映射对象)
错误如下:

line 85, in <module>
    if obstacle_definition[coordinate_y][coordinate_x] == "#":
IndexError: list index out of range
第85行,在
如果障碍物定义[coordinate_y][coordinate_x]==“#”:
索引器:列表索引超出范围

问题在于您的
障碍物定义
变量:

obstacle_definition = """\
###############    #########
        #########      #####
####        #######    #####
######  ############  ######
########       #####  ######
###########    #############
####        #########          << this line is the problem!
#########    ###############
#######               ######
############    ############
############    ############
              ##############
#       ########        ####
#############       ########
##################      ####\
"""

您应该学习如何调试代码。您缺少第6行的snake地图中的空格

obstacle_definition = """\
###############    #########
        #########      #####
####        #######    #####
######  ############  ######
########       #####  ######
###########    #############
####        #########         <--- no spaces here, line just ends after the last '#'
#########    ###############
#######               ######
############    ############
############    ############
              ##############
#       ########        ####
#############       ########
##################      ####\
"""

非常感谢你,兄弟
obstacle_definition = """\
###############    #########
        #########      #####
####        #######    #####
######  ############  ######
########       #####  ######
###########    #############
####        #########         <--- no spaces here, line just ends after the last '#'
#########    ###############
#######               ######
############    ############
############    ############
              ##############
#       ########        ####
#############       ########
##################      ####\
"""
obstacle_definition = """\
###############    #########
        #########      #####
####        #######    #####
######  ############  ######
########       #####  ######
###########    #############
####        #########       
#########    ###############
#######               ######
############    ############
############    ############
              ##############
#       ########        ####
#############       ########
##################      ####\
"""