Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 我在学习pygame教程时遇到了未定义名称的问题_Python_Python 3.x_Error Handling_Pygame - Fatal编程技术网

Python 我在学习pygame教程时遇到了未定义名称的问题

Python 我在学习pygame教程时遇到了未定义名称的问题,python,python-3.x,error-handling,pygame,Python,Python 3.x,Error Handling,Pygame,下面是关于如何使用Pygame制作平铺游戏的教程:。当我运行该程序时,出现以下错误: NameError:未定义名称“行” 我不知道您会将row定义为什么,我还想知道它是否与教程的编写年份有关(2011年意味着在较新版本的python中工作的东西不再工作) 引发此错误的代码行如下: for x, row in enumerate(row): 我相信守则是这样的: import pygame import pygame.locals def load_tile_table(filename,

下面是关于如何使用Pygame制作平铺游戏的教程:。当我运行该程序时,出现以下错误:

NameError:未定义名称“行”

我不知道您会将row定义为什么,我还想知道它是否与教程的编写年份有关(2011年意味着在较新版本的python中工作的东西不再工作) 引发此错误的代码行如下:

for x, row in enumerate(row):

我相信守则是这样的:

import pygame
import pygame.locals

def load_tile_table(filename, width, height):
    image = pygame.image.load(filename).convert()
    image_width, image_height = image.get_size()
    tile_table = []
    for tile_x in range(0, image_width/width):
        line = []
        tile_table.append(line)
        for tile_y in range(0, image_height/height):
            rect = (tile_x*width, tile_y*height, width, height)
            line.append(image.subsurface(rect))
    return tile_table

if __name__=='__main__':
    pygame.init()
    screen = pygame.display.set_mode((128, 98))
    screen.fill((255, 255, 255))
    table = load_tile_table("ground.png", 24, 16)
    for x, row in enumerate(table):
        for y, tile in enumerate(row):
            screen.blit(tile, (x*32, y*24))
    pygame.display.flip()
    while pygame.event.wait().type != pygame.locals.QUIT:
        pass
如果我们看一下,这个循环是

对于x,枚举(表)中的行:
对于y,在枚举(行)中平铺:

是来自第一个循环的iterable。在此之后,第二个循环使用
获取每个磁贴,并添加一个磁贴精灵


因此,您的意思是枚举(表)中x的行
而不是枚举(行)中x的行

嗨!请查看此链接以获得最少、完整和可验证的代码示例:谢谢!但是现在有另一个错误,当它说screen.blit(tile,(x*32,y*24))'NameError:name'tile'没有定义'
screen.blit(tile,(x*32,y*24))
应该在循环下
对于y,枚举(行)中的tile:
对于x,枚举(表)中的行:。基本上,只需
对于x,枚举(表)中的行:对于y,枚举(行)中的平铺:screen.blit(平铺,(x*32,y*24))
@James如果您认为您的问题的答案适合您的需要,那么每个答案都有一个检查按钮。这会通知每个人问题已解决,并将您的问题标记为已解决。