在Python程序中出现namererror,尽管它似乎设置正确

2024-03-28 16:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我从任务Python书中复制了这个程序:

room_map = [[1,1,1,1,1],
    [1,0,0,0,1],
    [1,0,1,0,1],
    [1,0,0,0,1],
    [1,0,0,0,1],
    [1,0,0,0,1],
    [1,1,1,1,1]]

WIDTH = 800 # window size
HEIGHT = 800

top_left_x = 100
top_left_y = 150

DEMO_OBJECTS = [images.floor, images.pillar]

room_height = 7
room_width = 5

def draw():
    for y in range(room_height):
        for x in range(room_width):
            image_to_draw = DEMO_OBJECTS[room_map[y][x]]
            screen.blit(image_to_draw,
                        (top_left_x + (x * 30),
                        top_left_y + (y * 30) - image_to_draw.get_height()))

当我运行它时,会出现以下错误:

Traceback (most recent call last):
  File "/home/pi/escape/listing3-5.py", line 15, in <module>
    DEMO_OBJECTS = [images.floor, images.pillar]
NameError: name 'images' is not defined

我想也许我应该导入图像或一些类似的包,但书中没有提到这样做,所以我不确定。python文件位于正确的位置,包含我的图像的images文件夹位于同一文件夹中。我错过什么明显的东西了吗?你知道吗


Tags: toinimagemapobjectsdemotopwidth
1条回答
网友
1楼 · 发布于 2024-03-28 16:12:18

我找到了解决办法。我试着用空闲的时间运行程序。我应该用PyGameZero来运行它。它在我执行命令pgzrun listing3-5.py时工作。你知道吗

我想Pygame Zero已经定义了images。你知道吗

相关问题 更多 >