2012-01-24 76 views
0

我想寫一個代碼,將顯示從板上的俄羅斯方塊的形狀。我不斷收到這個錯誤,不知道如何解決它:Python:顯示從俄羅斯方塊遊戲的形狀

TypeError: unbound method can_move() must be called with O_shape instance as first argument (got Board instance instead)

這裏是我的代碼:

class Board(): 

    def __init__(self, win, width, height): 
     self.width = width 
     self.height = height 

     # create a canvas to draw the tetris shapes on 
     self.canvas = CanvasFrame(win, self.width * Block.BLOCK_SIZE + Block.OUTLINE_WIDTH, 
             self.height * Block.BLOCK_SIZE + Block.OUTLINE_WIDTH) 
     self.canvas.setBackground('light gray') 

     # create an empty dictionary 
     # currently we have no shapes on the board 
     self.grid = {} 

    def draw_shape(self, shape): 
     ''' Parameters: shape - type: Shape 
      Return value: type: bool 

      draws the shape on the board if there is space for it 
      and returns True, otherwise it returns False 
     ''' 
     if shape.can_move(self, 0, 0): 
      shape.draw(self.canvas) 
      return True 
     return False 

class Tetris(): 

    SHAPES = [I_shape, J_shape, L_shape, O_shape, S_shape, T_shape, Z_shape] 
    DIRECTION = {'Left':(-1, 0), 'Right':(1, 0), 'Down':(0, 1)} 
    BOARD_WIDTH = 10 
    BOARD_HEIGHT = 20 

    def __init__(self, win): 
     self.board = Board(win, self.BOARD_WIDTH, self.BOARD_HEIGHT) 
     self.win = win 
     self.delay = 1000 #ms 

     # sets up the keyboard events 
     # when a key is called the method key_pressed will be called 
     self.win.bind_all('<Key>', self.key_pressed) 

     # set the current shape to a random new shape 
     self.current_shape = self.create_new_shape() 

     # Draw the current_shape oan the board 
     self.board.draw_shape(self.current_shape) 


    def create_new_shape(self): 
     ''' Return value: type: Shape 

      Create a random new shape that is centered 
      at y = 0 and x = int(self.BOARD_WIDTH/2) 
      return the shape 
     ''' 

     # YOUR CODE HERE 
     y = 0 
     x = int(self.BOARD_WIDTH/2) 
     the_shape = random.choice(self.SHAPES) 
     return the_shape 
+0

我相信我的問題存在於self.board.draw_shape(self.current_shape)行的某處。我和一位朋友一起去了解了這個問題,他們暗示我在那裏或者在create_new_shape函數中查找了我的問題,但我無法弄清楚如何使其工作。 – user1162643

回答

1

你忘了實例化形狀類。

SHAPES = [I_shape(), J_shape(), L_shape(), O_shape(), S_shape(), T_shape(), Z_shape()] 

但是你應該只有一個Shape類,它的參數將它們變成各種形狀。

+0

他可以爲每個形狀創建一個類(「draw」方法可能會爲每個形狀進行硬編碼)。但是,該類應該在'the_shape = random.choice(self.SHAPES)'行中實例化。 – jsbueno

+0

這是不正確的,SHAPES列表提供了對不同形狀類(每個tetromino一個)的引用的方便列表。然後通過SHAPES []()實例化形狀類。 – Keen

3

您沒有發佈相關方法的代碼(can_move())。
此外,錯誤消息是不言自明的,它需要一個類型爲O_shape的參數,但是您調用該方法並將其傳遞給Board。綁定到一個類

def draw_shape(self, shape): 
''' Parameters: shape - type: Shape 
    Return value: type: bool 

    draws the shape on the board if there is space for it 
    and returns True, otherwise it returns False 
''' 
    if shape.can_move(self, 0, 0): # <-- you probably meant to call shape.can_move(0,0) 
    shape.draw(self.canvas) 
    return True 
    return False 

方法有隱式第一個參數傳遞的實例。