【问题标题】:AttributeError: 'NoneType' object has no attribute 'push'AttributeError: 'NoneType' 对象没有属性 'push'
【发布时间】:2021-09-05 05:16:42
【问题描述】:

我正在观看 YouTube 上的讲座并制作俄罗斯方块。

但是我收到了这个错误。

Traceback (most recent call last):
File "C:\Users\gkakc\PycharmProjects\Tetris\main.py", line 90, in <module>
draw_grid(pen, grid)
File "C:\Users\gkakc\PycharmProjects\Tetris\main.py", line 80, in draw_grid
pen.stamp()
File "C:\Users\gkakc\AppData\Local\Programs\Python\Python39\lib\turtle.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()

我已经实现了一个块丢弃。

但之后就不行了。

我需要你的帮助。

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

【问题讨论】:

    标签: python attributes attributeerror nonetype tetris


    【解决方案1】:

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

    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)
    

    我不知道你的目的是什么。我只添加了为什么会出现此错误的答案

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 2021-12-26
      • 2019-07-23
      • 2018-05-13
      • 2020-09-07
      • 2017-05-03
      • 2023-03-16
      • 2018-07-14
      • 2013-06-16
      相关资源
      最近更新 更多