Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
这是我的代码。我要的是地图是否有1,然后在其中创建Wall。但是当我运行该程序时,它显示Wall(col, row). NameError: name 'col' is not defined.如何解决此问题?我寻找了很多答案,但没有一个真正解决。如何告诉计算机使用Wallcol在某个X和Y处添加row的位置?任何帮助表示赞赏。

import pygame as pg
import sys
import os

WIDTH = 800
HEIGHT = 600
TITLE = 'Importing A Map'
CLOCK = pg.time.Clock()
FPS = 60

DARKGRAY = (40, 40, 40)
RED = (255, 0, 0)

game_folder = os.path.dirname(__file__)

# create tiles, (WIDTH = 20 tiles, HEIGHT = 15 tiles)
TILESIZE = 40
GRIDWIDTH = WIDTH / TILESIZE
GRIDHEIGHT = HEIGHT / TILESIZE

screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)

def load_map():
    map_data = []
    with open(path.join(game_folder, 'map.txt'), 'rt') as f:
        for line in f:
            map_data.append(line)

def new_game():
    for row, tiles in enumerate(map_data):
        for col, tile in enumerate(tiles):
            # Here is the part where I have problems.
             if tile == '1':
                Wall(col, row)

def draw_tiles():
    for x in range(0, WIDTH, TILESIZE):
        pg.draw.line(screen, DARKGRAY, (x, 0), (x, HEIGHT))
    for y in range(0, HEIGHT, TILESIZE):
        pg.draw.line(screen, DARKGRAY, (0, y), (WIDTH, y))

class Wall(pg.sprite.Sprite):
    def __init__(self, x, y):
        pg.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.image = pg.Surface((TILESIZE, TILESIZE))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = x * TILESIZE
        self.rect.y = y * TILESIZE

wall = Wall(col, row)

def game_loop():
    pg.init()

    all_sprites = pg.sprite.Group()
    all_sprites.add(wall)

    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False

        draw_tiles()
        all_sprites.update()
        all_sprites.draw(screen)

        pg.display.update()
        CLOCK.tick(FPS)

    pg.quit()

game_loop()

最佳答案

col和row是包含的for循环的局部变量(变量仅存在于for循环内部)。

我不能保证以下代码将完全按照您希望的方式工作,但是它可以使您了解出错的地方。

请注意,我是如何在文档顶部创建wall的“变量声明”,然后将wall变量赋值移动到两个for循环内部,并使wall成为全局变量,以便其他函数可以访问它。

如果这不起作用/您还有其他相关问题,请告诉我,我会尽力帮助。

import pygame as pg
import sys
import os

wall = 0

WIDTH = 800
HEIGHT = 600
TITLE = 'Importing A Map'
CLOCK = pg.time.Clock()
FPS = 60

DARKGRAY = (40, 40, 40)
RED = (255, 0, 0)

game_folder = os.path.dirname(__file__)

# create tiles, (WIDTH = 20 tiles, HEIGHT = 15 tiles)
TILESIZE = 40
GRIDWIDTH = WIDTH / TILESIZE
GRIDHEIGHT = HEIGHT / TILESIZE

screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)

def load_map():
    map_data = []
    with open(path.join(game_folder, 'map.txt'), 'rt') as f:
        for line in f:
            map_data.append(line)

def new_game():
    global wall
    for row, tiles in enumerate(map_data):
        for col, tile in enumerate(tiles):
             if tile == '1':
                Wall(col, row)

             wall = Wall(col, row)

def draw_tiles():
    for x in range(0, WIDTH, TILESIZE):
        pg.draw.line(screen, DARKGRAY, (x, 0), (x, HEIGHT))
    for y in range(0, HEIGHT, TILESIZE):
        pg.draw.line(screen, DARKGRAY, (0, y), (WIDTH, y))

class Wall(pg.sprite.Sprite):
    def __init__(self, x, y):
        pg.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.image = pg.Surface((TILESIZE, TILESIZE))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = x * TILESIZE
        self.rect.y = y * TILESIZE


def game_loop():
    pg.init()

    all_sprites = pg.sprite.Group()
    all_sprites.add(wall)

    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False

        draw_tiles()
        all_sprites.update()
        all_sprites.draw(screen)

        pg.display.update()
        CLOCK.tick(FPS)

    pg.quit()

game_loop()

关于python - 如何在Pygame中将map.txt文件导入为 map ? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45338741/

10-12 02:32