I was watching a lecture on YouTube and making Tetris.
But I got this error.
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
That's the code
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()
I've implemented a block drop.
But it doesn't work after that.
I need your help.
I hope you understood my poor English.