from tkinter import *
import random
root = Tk()
root.title("Tetris")
root.geometry(("%dx%d")%(400, 510))
mainFrame = Frame(root, width = 200, height = 510)
pointFrame = Frame(root, width = 200, height = 500, bg = '#272822')
can = Canvas(mainFrame, bg = "#272822",width = 200, height = 500)
mainFrame.grid(row = 0, column = 0)
pointFrame.grid(row = 0, column = 1)
can.pack()
class Cube():
def __init__(self, xBeginCoords, yBeginCoords):
self.singleCube = can.create_rectangle(xBeginCoords, yBeginCoords, \
xBeginCoords + 20, \
yBeginCoords + 20 ,\
outline = '#272822')
self.isActive = False
self.maxHeight = 25
#self.create_figure()
def stop_figure(self):
for x in self.figureCoords:
allCubes[x[0]][x[1]].give_true()
self.get_max()
self.check_line()
self.create_figure()
def get_max(self):
self.possibleHeight = min(self.figureCoords[x][0] for x in range(4))
if self.maxHeight > self.possibleHeight:
self.maxHeight = self.possibleHeight
def give_true(self):
self.isActive = True
def give_end_color(self):
can.itemconfig(self.singleCube, fill = "#0ec0e2", outline = 'white')
self.give_true()
def take_color(self):
can.itemconfig(self.singleCube, fill = '#272822', outline = '#272822')
self.isActive = False
def give_color(self, ftype):
if ftype == 0:
can.itemconfig(self.singleCube, fill = "#fd7521", outline = 'white') # it came to me accidentally
if ftype == 1:
can.itemconfig(self.singleCube, fill = "#ae739a", outline = 'white')
if ftype == 2:
can.itemconfig(self.singleCube, fill = "#f92672", outline = 'white')
if ftype == 3:
can.itemconfig(self.singleCube, fill = "#a6e22a", outline = 'white')
if ftype == 4:
can.itemconfig(self.singleCube, fill = "#66d9ef", outline = 'white')
def create_figure(self):
self.figureType = random.randint(0, 4)
self.rotate = 0
# self.figureType = 0 #temporary
if self.figureType == 0:
self.figureCoords = [[0, 3], [0, 4], [0, 5], [1, 4]]
for x in self.figureCoords:
allCubes[x[0]][x[1]].give_color(self.figureType)
if self.figureType == 1:
self.figureCoords = [[0, 3], [0, 4], [0, 5], [0, 6]]
for x in self.figureCoords:
allCubes[x[0]][x[1]].give_color(self.figureType)
if self.figureType == 2:
self.figureCoords = [[0, 3], [0, 4], [1, 3], [1, 4]]
for x in self.figureCoords:
allCubes[x[0]][x[1]].give_color(self.figureType)
if self.figureType == 3:
self.figureCoords = [[0, 3], [0, 4], [1, 4], [1, 5]]
for x in self.figureCoords:
allCubes[x[0]][x[1]].give_color(self.figureType)
if self.figureType == 4:
self.figureCoords = [[0, 3], [0, 4], [1, 4], [2, 4]]
self.move_down()
class Moving_Cubes(Cube):
def move_down(self):
if self.maxHeight == 0:
exit()
try:
for x in self.figureCoords:
allCubes[x[0]+1][x[1]]
except IndexError:
self.stop_figure()
return
else:
for x in self.figureCoords:
if allCubes[x[0]+1][x[1]].isActive == True:
#
self.stop_figure()
return
for x in self.figureCoords:
allCubes[x[0]][x[1]].take_color()
for x in self.figureCoords:
allCubes[x[0]+1][x[1]].give_color(self.figureType)
x[0] += 1
can.after(100, self.move_down)
def move_left(self, event):
for x in self.figureCoords:
if x[1] == 0 or allCubes[x[0]][x[1]-1].isActive == True:
return
else:
for x in self.figureCoords:
allCubes[x[0]][x[1]].take_color()
for x in self.figureCoords:
allCubes[x[0]][x[1]-1].give_color(self.figureType)
x[1] -= 1
def move_right(self, event):
for x in self.figureCoords:
if x[1] == 9 or allCubes[x[0]][x[1]+1].isActive == True:
return
else:
for x in self.figureCoords:
allCubes[x[0]][x[1]].take_color()
for x in self.figureCoords:
allCubes[x[0]][x[1]+1].give_color(self.figureType)
x[1] += 1
def check_line(self):
self.y = min(self.figureCoords)
while self.y[0] < 25:
x = 0
while x < 10:
if allCubes[self.y[0]][x].isActive == False:
break
elif x == 9:
for z in range(0, 10):
allCubes[self.y[0]][z].take_color()
allCubes[self.y[0]][z].isActive = False
self.move_the_line_down()
x += 1
self.y[0] += 1
def move_the_line_down(self):
self.begin_moving = self.y[0]-1
while self.begin_moving >= self.maxHeight:
for x in range(10):
if allCubes[self.begin_moving][x].isActive == True:
allCubes[self.begin_moving][x].take_color()
allCubes[self.begin_moving + 1][x].give_end_color()
self.begin_moving -= 1
def rotate_figure(self, event):
if self.figureType == 0: #harder than i thought
if self.rotate == 0:
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
allCubes[self.figureCoords[0][0] - 1][self.figureCoords[0][1] + 1].give_color(self.figureType)
self.figureCoords[0][0] -= 1
self.figureCoords[0][1] += 1
self.rotate += 1
elif self.rotate == 1:
allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
allCubes[self.figureCoords[3][0] - 1][self.figureCoords[3][1] - 1].give_color(self.figureType)
self.figureCoords[3][0] -= 1
self.figureCoords[3][1] -= 1
self.rotate += 1
elif self.rotate == 2:
allCubes[self.figureCoords[2][0]][self.figureCoords[2][1]].take_color()
allCubes[self.figureCoords[2][0] + 1][self.figureCoords[2][1] - 1].give_color(self.figureType)
self.figureCoords[2][0] += 1
self.figureCoords[2][1] -= 1
self.rotate += 1
elif self.rotate == 3: #oh fuck. what a shame
allCubes[self.figureCoords[2][0]][self.figureCoords[2][1]].take_color()
allCubes[self.figureCoords[2][0] - 1][self.figureCoords[2][1] + 1].give_color(self.figureType)
self.figureCoords[2][0] -= 1
self.figureCoords[2][1] += 1
allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
allCubes[self.figureCoords[3][0] + 1][self.figureCoords[3][1] + 1].give_color(self.figureType)
self.figureCoords[3][0] += 1
self.figureCoords[3][1] += 1
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
allCubes[self.figureCoords[0][0] + 1][self.figureCoords[0][1] - 1].give_color(self.figureType)
self.figureCoords[0][0] += 1
self.figureCoords[0][1] -= 1
self.rotate = 0
if self.figureType == 1:
if self.rotate == 0:
for x in range(1, 4):
allCubes[self.figureCoords[x][0]][self.figureCoords[x][1]].take_color()
allCubes[self.figureCoords[x][0] - x][self.figureCoords[x][1] - x].give_color(self.figureType)
self.figureCoords[x][0] -= x
self.figureCoords[x][1] -= x
self.rotate = 1
else:
for x in range(1, 4):
allCubes[self.figureCoords[x][0]][self.figureCoords[x][1]].take_color()
allCubes[self.figureCoords[x][0] + x][self.figureCoords[x][1] + x].give_color(self.figureType)
self.figureCoords[x][0] += x
self.figureCoords[x][1] += x
self.rotate = 0
if self.figureType == 2:
return
if self.figureType == 3:
if self.rotate == 0:
allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
allCubes[self.figureCoords[3][0] - 2][self.figureCoords[3][1]].give_color(self.figureType)
self.figureCoords[3][0] -= 2
allCubes[self.figureCoords[2][0]][self.figureCoords[2][1]].take_color()
allCubes[self.figureCoords[2][0] - 1][self.figureCoords[2][1] + 1].give_color(self.figureType)
self.figureCoords[2][0] -= 1
self.figureCoords[2][1] += 1
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
allCubes[self.figureCoords[0][0] + 1][self.figureCoords[0][1] + 1].give_color(self.figureType)
self.figureCoords[0][0] += 1
self.figureCoords[0][1] += 1
self.rotate = 1
else:
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
allCubes[self.figureCoords[0][0] - 1][self.figureCoords[0][1] - 1].give_color(self.figureType)
self.figureCoords[0][0] -= 1
self.figureCoords[0][1] -= 1
allCubes[self.figureCoords[2][0]][self.figureCoords[2][1]].take_color()
allCubes[self.figureCoords[2][0] + 1][self.figureCoords[2][1] - 1].give_color(self.figureType)
self.figureCoords[2][0] += 1
self.figureCoords[2][1] -= 1
allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
allCubes[self.figureCoords[3][0] + 2][self.figureCoords[3][1]].give_color(self.figureType)
self.figureCoords[3][0] += 2
self.rotate = 0
if self.figureType == 4: #please kill me
if self.rotate == 0:
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1] + 2].give_color(self.figureType)
self.figureCoords[0][1] += 2
allCubes[self.figureCoords[1][0]][self.figureCoords[1][1]].take_color()
allCubes[self.figureCoords[1][0] + 1][self.figureCoords[1][1] + 1].give_color(self.figureType)
self.figureCoords[1][0] += 1
self.figureCoords[1][1] += 1
allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
allCubes[self.figureCoords[3][0] - 1][self.figureCoords[3][1] - 1].give_color(self.figureType)
self.figureCoords[3][0] -= 1
self.figureCoords[3][1] -= 1
self.rotate = 1
elif self.rotate == 1:
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
allCubes[self.figureCoords[0][0] + 2][self.figureCoords[0][1]].give_color(self.figureType)
self.figureCoords[0][0] += 2
allCubes[self.figureCoords[1][0]][self.figureCoords[1][1]].take_color()
allCubes[self.figureCoords[1][0] + 1][self.figureCoords[1][1] - 1].give_color(self.figureType)
self.figureCoords[1][0] += 1
self.figureCoords[1][1] -= 1
allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
allCubes[self.figureCoords[3][0] - 1][self.figureCoords[3][1] + 1].give_color(self.figureType)
self.figureCoords[3][0] -= 1
self.figureCoords[3][1] += 1
self.rotate = 2
elif self.rotate == 2:
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1] - 2].give_color(self.figureType)
self.figureCoords[0][1] -= 2
allCubes[self.figureCoords[1][0]][self.figureCoords[1][1]].take_color()
allCubes[self.figureCoords[1][0] - 1][self.figureCoords[1][1] - 1].give_color(self.figureType)
self.figureCoords[1][0] -= 1
self.figureCoords[1][1] -= 1
allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
allCubes[self.figureCoords[3][0] + 1][self.figureCoords[3][1] + 1].give_color(self.figureType)
self.figureCoords[3][0] += 1
self.figureCoords[3][1] += 1
self.rotate = 3
elif self.rotate == 3:
allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
allCubes[self.figureCoords[0][0] - 2][self.figureCoords[0][1]].give_color(self.figureType)
self.figureCoords[0][0] -= 2
allCubes[self.figureCoords[1][0]][self.figureCoords[1][1]].take_color()
allCubes[self.figureCoords[1][0] - 1][self.figureCoords[1][1] + 1].give_color(self.figureType)
self.figureCoords[1][0] -= 1
self.figureCoords[1][1] += 1
allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
allCubes[self.figureCoords[3][0] + 1][self.figureCoords[3][1] - 1].give_color(self.figureType)
self.figureCoords[3][0] += 1
self.figureCoords[3][1] -= 1
self.rotate = 0
class Appearance():
def __init__(self):
self.level = Label(pointFrame, width = 5, text = 'Level', font = 'Arial 18', fg = '#f2e12e', bg = '#272822').pack(side ='left')
self.levelValue = Entry(pointFrame, width = 5, fg = '#f2e12e', bg = '#272822').pack(side = 'right')
self.score = Label(pointFrame, width = 5, text = 'Score', font = 'Arial 18', fg = '#f2e12e', bg = '#272822').pack(side = 'bottom')
allCubes = [[Moving_Cubes(x1, y1) for x1 in range(2, 200, 20)] \
for y1 in range(2, 500, 20)]
Scores = Appearance()
allCubes[0][1].create_figure()
#root.bind('<Down>', allCubes[0][1].move_down)
root.bind('<Left>', allCubes[0][1].move_left)
root.bind('<Right>', allCubes[0][1].move_right)
root.bind('<Up>', allCubes[0][1].rotate_figure)
mainloop()