我正在尝试编写一个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
Run Code Online (Sandbox Code Playgroud)
我的问题在于"self.current_shape = Board.draw_shape(the_shape).它说没有定义the_shape但我认为我在create_new_shape中定义了它.
你做了但变量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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
567 次 |
| 最近记录: |