属性错误: 'NoneType'对象没有属性'push'



我在YouTube上看一个讲座,然后做俄罗斯方块。

但是我得到了这个错误。

Traceback (most recent call last):
File "C:UsersgkakcPycharmProjectsTetrismain.py", line 90, in <module>
draw_grid(pen, grid)
File "C:UsersgkakcPycharmProjectsTetrismain.py", line 80, in draw_grid
pen.stamp()
File "C:UsersgkakcAppDataLocalProgramsPythonPython39libturtle.py", line 3077, in stamp
self.undobuffer.push(("stamp", stitem))
AttributeError: 'NoneType' object has no attribute 'push'
Process finished with exit code 1

这就是代码

import turtle
import time
# wn = Window
wn = turtle.Screen()
wn.title("Tetris by LeeSooHyung")
wn.bgcolor("black")
wn.setup(width=600, height=800)
wn.tracer(0)
delay = 0.05
class Shape():
def __init__(self):
self.x = 5
self.y = 0
self.color = 4
def move_left(self, grid):
if self.x > 0:
grid[self.y][self.x] = 0
self.x -= 1
def move_right(self, grid):
if self.x < 11:
grid[self.y][self.x] = 0
self.x += 1
grid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 2, 3, 4, 0, 0, 7, 1, 2, 3, 4]
]
pen = turtle.Turtle()
pen.penup()
pen.speed(0)
pen.shape("square")
pen.setundobuffer(None)
def draw_grid(pen, grid):
top = 230
left = -110
colors = ["black", "lightblue", "blue", "orange", "yellow", "green", "purple", "red"]
for y in range(len(grid)):
for x in range(len(grid[0])):
screen_x = left + (x * 20)
screen_y = top - (y * 20)
color_number = grid[y][x]
color = colors[color_number]
pen.color(color)
pen.goto(screen_x, screen_y)
pen.stamp()

# Create the basic shape for the start of the game
shape = Shape()
# Put the shape in the grid
grid[shape.y][shape.x] = shape.color
# Draw the intial grid
draw_grid(pen, grid)
wn.listen()
wn.onkeypress(shape.move_left(grid), "a")
wn.onkeypress(shape.move_right(grid), "d")
# Main game loop
while True:
wn.update()
# Shape move down
# Open row
if shape.y == 23: # if shape is in the bottom row
shape = Shape()
elif grid[shape.y + 1][shape.x] == 0: # if Nothing there
grid[shape.y][shape.x] = 0 # Delete the shape of the previous position
shape.y += 1
grid[shape.y][shape.x] = shape.color
else:
shape = Shape()
draw_grid(pen, grid)
time.sleep(delay)
# We have to keep the window open, so I loop it.
wn.mainloop()

我已经实现了block drop。

但之后就不工作了。

我需要你的帮助。

我希望你能理解我蹩脚的英语。

尝试为setundobuffer附加任何值,如果setundobuffer中的size值为none则self.undobuffer的值也为none,这就是为什么显示此错误

def setundobuffer(self, size):
"""Set or disable undobuffer.
Argument:
size -- an integer or None
If size is an integer an empty undobuffer of given size is installed.
Size gives the maximum number of turtle-actions that can be undone
by the undo() function.
If size is None, no undobuffer is present.
Example (for a Turtle instance named turtle):
>>> turtle.setundobuffer(42)
"""
if size is None:
self.undobuffer = None
else:
self.undobuffer = Tbuffer(size)
我不知道你的目的是什么。我只添加了为什么会出现这个错误的答案

最新更新