开发者

I have a compiler error "not defined" although there is a definition

  from gasp import *
GRID_SIZE = 30
MARGIN = GRID_SIZE

BACKGROUND_COLOR = color.BLACK    # Colors we use
WALL_COLOR = (0.6 * 255, 0.9 * 255, 0.9 * 255)

# The shape of the maze.  Each character
# represents a different type of object
#   % - Wall
#   . - Food
#   o - Capsule
#   G - Ghost
#   P - Chomp
# Other characters are ignored


the_layout = [
  "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",    
  "%.....%.................%.....%",
  "%o%%%.%.%%%.%%%%%%%.%%%.%.%%%o%",
  "%.%.....%......%......%.....%.%",
  "%...%%%.%.%%%%.%.%%%%.%.%%%...%",
  "%%%.%...%.%.........%.%...%.%%%",
  "%...%.%%%.%.%%% %%%.%.%%%.%...%",
  "%.%%%.......%GG GG%.......%%%.%",
  "%...%.%%%.%.%%%%%%%.%.%%%.%...%",
  "%%%.%...%.%.........%.%...%.%%%",
  "%...%%%.%.%%%%.%.%%%%.%.%%%...%",
  "%.%.....%......%......%.....%.%",
  "%o%%%.%.%%%.%%%%%%%.%%%.%.%%%o%",
  "%.....%........P........%.....%",
  "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"]


class Immovable:
    def is_a_wall(self):
        return False

class Nothing(Immovable):
    pass

class Maze:
    def __init__(self):
        self.have_window = False
        self.game_over = False
        self.set_layout(the_layout)
        set_speed(20)

    def set_layout(self, layout):
        height = len(layout)                   
        width = len(layout[0])                
        self.make_window(width, height)
        self.make_map(width, height)         
        max_y = height - 1
        for x in range( width ):     
            for y in range(height):
                char = layout[max_y - y][x]   
                self.make_object((x, y), char) 

    def make_window(self, width, height):
        grid_width = (width -1) * GRID_SIZE
        grid_height = (height - 1) * GRID_SIZE
        screen_width = 2 * MARGIN + grid_width
        screen_height = 2 *  MARGIN + grid_height
        begin_graphics(screen_width, screen_height,"Chomp",BACKGROUND_COLOR)

    def to_screen(self, point):
        (x,y) = point
        x = x * GRID_SIZE + MARGIN
        y = y * GRID_SIZE + MARGIN
        return(x,y)

    def make_map(self, width, height):
        self.width = width
        self.height = height
        self.map = []
        for y in range(width):
            new_row = []
            for x in range(width):
                new_row.append(Nothing())
            self.map.append(new_row)

    def make_object(self,point,charactor):
        (x,y) = point
        if charactor == "%":
            self.map[y][x] = Wall(self,point)

    def finished(self):
        return self.game_over

    def play(self):
        update_when('next_tick')

    def done(self):
        end_graphics()
        self.map = []

    def object_at(self,point):
        (x,y) = point
        if y < 0 or y >= self.height:
            return Nothing()
        if x < 0 or x >= self.width:
            return Nothing()
        return self.map[y][x]




class Wall(Immovable):
    def __init__(self, maze, point):
        self.place = point                          # Store our position
        self.screen_point = maze.to_screen(point)
        self.maze = maze                            # Keep hold of Maze
        self.draw()

    def draw(self):
        (screen_x, screen_y) = self.screen_point
        dot_size = GRID_SIZE * 0.2
        Circle(self.screen_point, dot_size,   
                color = WALL_COLOR, filled = 1)
        (x, y) = self.place
        neighbors = [ (x+1, y), (x-1, y)]
        for neighbor in neighbors:
            self.check_neighbor(neighbor)

    def check_neighbor(self,neighbor):
        maze = self.maze
        object = maze.object_at(neighbor)

        if object.is_a_wall():
            here = self.screen_point
            there = maze.to_screen(neighbor)
            Line(here, there, color = WALL_COLOR,thickness = 2)

    def is_a_wall(self):
        return True

the_maze = Maze()

while not the_maze.finished():
    the_maze.play()
    the_maze.done()

I got this error..

Traceback (most recent call last):  
File "chomp.py", line 110, in class Wall(Immovable):
File "chomp.py", line 124, in开发者_JAVA百科 Wall for neighbor in neighbors:
NameError: name 'neighbors' is not defined

I spent lot of time still can't find what's wrong, need some help


The unfinished call to Circle() is probably the result of an error trying to format the code properly. Please check that you post the code that you actually ran, and the traceback is the one that you actually got (there's some of this one missing!).

The error reported in the (horribly formatted) traceback is that neighbors is undefined in the line for neighbor in neighbors:. There is absolutely no way the compiler would munch its way through the intervening lines

(x, y) = self.place
neighbors = [ (x+1, y), (x-1, y)]

without some other kind of error.

Note: the above was in response to another question that was closed as I was answering it. I am leaving it in so that you know that the advice that you got for that question was wrong.

I suspect that after your FIRST question (width not defined in line for x in range( width ):), you didn't fix all your indentation errors, and the Q2 and Q3 line for neighbor in neighbors: should be shifted 4 spaces to the right of where it appears to be.

Do you have any tabs in your source files? If so, get rid of them. If you are not sure about how to stay tab-free, ask a separate question, say what editor and what OS and maybe someone familiar with that combination can help you.

How to find tabs in your source file:

C:\junk>\python27\python -c "print[x for x in enumerate(open('sometabs.py'),1)if'\t'in x[1]]"
[(1, 'foo\tbar\n'), (3, '\t\toof\n')]


I don't see where the error is immediately. Have you recently moved around files such that an import isn't really running your new code, but actually running a .pyc file instead? For example, did you recently introduce a package with the same name as a python file?

.
\- main.py           Has an "import stuff"
\- stuff.py          This is the code you think is being run.
\- stuff
   \- __init__.py    This code is being run.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜