2011-03-13 85 views
0
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 'neighbors' is not defined 

我花了很多时间依然找不到什么是错的,需要一些帮助

+0

行124:迷宫= self.maze – 2011-03-13 05:37:47

+0

行110:长城类(不动产): – 2011-03-13 05:38:55

+0

(对不起,我删除了我的意见后,我找到的代码;我问哪里线110和124分别。)线路124不包含'maze = self.maze';它包含“邻居的邻居”这一行。 – 2011-03-13 05:41:42

回答

1

Circle()的未完成调用可能是错误尝试正确格式化代码的结果。请检查您是否发布了您实际运行的代码,并且追溯是您实际获得的代码(这里有一些缺失!)。

(可怕格式化)回溯中报告的错误是在行for neighbor in neighbors:中未定义。编译器绝对没有办法通过插入的线路来进行插入

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

没有其他类型的错误。

注意:以上是对我在回答时关闭的另一个问题的回应。我正在离开它,以便您知道您为该问题获得的建议是错误的。

我怀疑你的后FIRST questionwidth符合for x in range(width):没有定义),你没有解决您所有的缩进错误,并且Q2和Q3线for neighbor in neighbors:应转向4个空格的地方似乎是正确的。

您的源文件中是否有任何选项卡?如果是这样,摆脱他们。如果您不确定如何保持无标签,请提出一个单独的问题,说什么编辑器,什么操作系统和熟悉该组合的人可以帮助您。

如何找到标签在源文件:

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')] 
+0

非常感谢你!约翰。我终于解决了它。我用另一个编辑器打开它并修改了杂乱的Tab。我知道了。谢谢! – 2011-03-13 06:15:04

+0

@Andy Leman:不要“修改混乱的标签” - 根除所有标签;他们是邪恶的。看到我的更新为单线标签线定位器。 – 2011-03-13 06:53:49

+0

谢谢。所以,我每次只用4个空格,对吧?远离标签? – 2011-03-13 06:59:30

1

我看不出错误立即。你最近有没有移动文件,导入并不是真的在运行你的新代码,而是实际运行一个.pyc文件呢?例如,你最近是否引入了一个与python文件同名的软件包?

 
. 
\- 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.