Вот полный код
from tkinter import *
from tkinter.ttk import *
import random
tk = Tk()
tk.title('Тетрис')
tk.geometry('550x650+50+50')
tk.configure(bg = '#000000')
tk.resizable(0, 0)
canvas1 = Canvas(tk, width = 330, height = 600)
canvas1.pack(expand = 1, anchor = W)
canvas2 = Canvas(tk, width = 150, height = 150)
canvas2.place(x = 365, y = 25)
canvas3 = Canvas(tk, width = 150, height = 100, bg = '#000000')
canvas3.place(x = 365, y = 200)
canvas4 = Canvas(tk)
canvas4.place(x = 365, y = 350)
for l in range(0, 330, 30):
canvas1.create_line(l, 0, l, 600, fill = "#000000")
for p in range(0, 600, 30):
canvas1.create_line(0, p, 330, p, fill = "#000000")
for g in range(0, 150, 30):
canvas2.create_line(g, 0, g, 150, fill = '#000000')
for j in range(0, 150, 30):
canvas2.create_line(0, j, 150, j, fill = '#000000')
canvas3.create_text(75, 25, text='SCORE:', font=('Courier', 15), fill='red')
ms = Message(canvas4)
ms.configure(text = """Увага!!! Для цієї гри необхідно, аби ви весь час натискали на стрілки або Enter. Для запуску гри натисніть будь-яку з означених клавіш""")
ms.pack()
class new_figure:
W = ('#c46210', '#648c11', '#e97451', '#af002a', '#008000', '#5f9ebb', '#867e36', '#5d8aa8', '#ef9999', '#d755b1', '#8a2be2', '#556b2f', '#df00ff')
S = random.choice(W)
block = canvas2.create_rectangle(60, 0, 90, 30, fill = S)
def newcolor():
T = random.choice(new_figure.W)
return T
def newblock():
new_figure.block = canvas2.create_rectangle(60, 0, 90, 30, fill = new_figure.newcolor())
class old_figure:
Q = ('#c46210', '#648c11', '#e97451', '#af002a', '#008000', '#5f9ebb', '#867e36', '#5d8aa8', '#ef9999', '#d755b1', '#8a2be2', '#556b2f', '#df00ff')
H = random.choice(Q)
block = canvas1.create_rectangle(150, 0, 180, 30, fill = H)
def
init(self, canvas1):
self.canvas1 = canvas1
self.x = 0
self.y = 0
self.id = old_figure.block
self.canvas1_width = self.canvas1.winfo_width()
self.canvas1_height = self.canvas1.winfo_height()
self.canvas1.pack()
self.movement()
def movement(self):
self.canvas1.move(
self.id, self.x, self.y)
self.canvas1.after(1000, self.movement)
def left(self, event):
pos = self.canvas1.coords(
self.id)
if min(pos[::2]) == 0 or max(pos[1::2]) == 600:
self.x = 0
self.y = 0
else:
self.x = 0
self.y = 0
self.canvas1.move(
self.id, -30, 30)
def right(self, event):
pos = self.canvas1.coords(
self.id)
if max(pos[::2]) == 330 or max(pos[1::2]) == 600:
self.x = 0
self.y = 0
else:
self.x = 0
self.y = 0
self.canvas1.move(
self.id, 30, 30)
def down(self, event):
pos = self.canvas1.coords(
self.id)
pos1 = canvas2.coords(new_figure.block)
if pos[3] == 600:
self.x = 0
self.y = 0
self.id = self.canvas1.create_rectangle(int(pos1[0]) + 90, int(pos1[1]), int(pos1[2]) + 90, int(pos1[3]), fill = new_figure.newcolor())
new_figure.newblock()
elif pos[3] == 570:
self.canvas1.move(
self.id, 0, 30)
else:
self.x = 0
self.y = 0
self.canvas1.move(
self.id, 0, 60)
def up(self, event):
pos = self.canvas1.coords(
self.id)
pos1 = canvas2.coords(new_figure.block)
if pos[3] == 600:
self.x = 0
self.y = 0
self.id = self.canvas1.create_rectangle(int(pos1[0]) + 90, int(pos1[1]), int(pos1[2]) + 90, int(pos1[3]), fill = new_figure.newcolor())
new_figure.newblock()
else:
self.x = 0
self.y = 0
self.canvas1.move(
self.id, 0, 30)