-1

テトリスのランダムな形状をボードに描画する Python プログラムを作成しようとしています。これが私のコードです:

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

    self.current_shape = self.create_new_shape()

    # Draw the current_shape oan the board 
    self.current_shape = Board.draw_shape(the_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
    '''

    y = 0
    x = int(self.BOARD_WIDTH/2)
    self.shapes = [O_shape,
                  T_shape,
                  L_shape,
                  J_shape,
                  Z_shape,
                  S_shape,
                  I_shape]

    the_shape = random.choice(self.shapes)
    return the_shape

私の問題は「self.current_shape = Board.draw_shape(the_shape)にあります。the_shapeは定義されていませんが、create_new_shapeで定義したと思いました.

4

3 に答える 3

5

あなたはしましたが、変数the_shapeはその関数のスコープに対してローカルです。create_new_shape()結果をフィールドに保存するときは、それを使用して形状を参照する必要があります。

self.current_shape = self.create_new_shape()

# Draw the current_shape oan the board 
self.current_shape = Board.draw_shape(self.current_shape)
于 2012-01-24T19:31:52.380 に答える
1

the_shapeは関数に対してローカルでcreate_new_shapeあり、関数が終了すると名前はスコープ外になります。

于 2012-01-24T19:31:24.523 に答える