2011-03-13 4 views
0

Possible Duplicate:
I have a compiler error “not defined” although there is a definition파이썬 나가서 설명하자면 NameError : 이름 - 구문 오류

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() 

이 나는이 오류 ..

Traceback (most recent call last): File "chomp.py", line 110, in 

class Wall(Immovable): File "chomp.py", line 124, in Wall for neighbor in neighbors: NameError: name '

이웃이 '

을 정의되지 않은있어 아직도 많은 시간을 할 수없는 지출 무엇이 잘못되었는지 찾아보십시오. 어떤 도움이 필요합니다.

답변

0

Circle() t 라인 122에 관한 wo 라인, 아마도 그 것이다. 후행 쉼표를 기반으로 인수가 누락 된 것 같습니다.

dot_size = GRID_SIZE * 0.2 
Circle(self.screen_point, dot_size, # No closing parentheses 
(x, y) = self.place 
neighbors = [ (x+1, y), (x-1, y)] 
for neighbor in neighbors: 
    self.check_neighbor(neighbor) 
+0

, 여전히 같은 오류가 발생했습니다. –

+0

동일한 오류 @Andy? –

+0

죄송합니다 내 실수. 여전히 도움이 필요해 .Rafe. 고마워. –

0

원 (self.screen_point, dot_size, 그 줄의 끝에

없는 뭔가 내가 편집 한

+0

나는 그것을 편집했지만, 여전히 같은 오류가있다. –

관련 문제