这是我的代码。我想要的是如果地图有1,那么在那里创建一个Wall
。但是当我运行该程序时,它会说Wall(col, row). NameError: name 'col' is not defined.
我该如何解决这个问题?我找了很多答案,但没有人真的解决了。如何使用Wall
和col
告诉计算机在某个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()
答案 0 :(得分:1)
col和row是包含for循环的局部变量(变量只存在于for循环中)。
我不能保证以下代码将完全符合您的要求,但它可以让您了解出错的地方。
注意我是如何在文档顶部创建一个wall的“变量声明”,然后将wall变量赋值移到for-loops中,并使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()