2012-01-24 2 views
0

게시판에 tetris의 모양을 표시하는 코드를 작성하려고합니다. 이 오류가 계속 그것을 해결하는 방법을 모른다 :Python : Tetris 게임에서 모양 표시

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

당신은 당신의 형상 클래스를 인스턴스화하는 것을 잊었다 :

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

여기 내 코드입니다.

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 

클래스에 바인딩 된 메서드는 암시 적으로 첫 번째 매개 변수로 전달되는 인스턴스를 갖습니다.