fork download
  1. from tkinter import *
  2. import random
  3.  
  4. root = Tk()
  5. root.title("Tetris")
  6. root.geometry(("%dx%d")%(400, 510))
  7. mainFrame = Frame(root, width = 200, height = 510)
  8. pointFrame = Frame(root, width = 200, height = 500, bg = '#272822')
  9. can = Canvas(mainFrame, bg = "#272822",width = 200, height = 500)
  10. mainFrame.grid(row = 0, column = 0)
  11. pointFrame.grid(row = 0, column = 1)
  12. can.pack()
  13.  
  14. class Cube():
  15. def __init__(self, xBeginCoords, yBeginCoords):
  16. self.singleCube = can.create_rectangle(xBeginCoords, yBeginCoords, \
  17. xBeginCoords + 20, \
  18. yBeginCoords + 20 ,\
  19. outline = '#272822')
  20. self.isActive = False
  21. self.maxHeight = 25
  22. #self.create_figure()
  23. def stop_figure(self):
  24. for x in self.figureCoords:
  25. allCubes[x[0]][x[1]].give_true()
  26. self.get_max()
  27. self.check_line()
  28. self.create_figure()
  29.  
  30. def get_max(self):
  31. self.possibleHeight = min(self.figureCoords[x][0] for x in range(4))
  32. if self.maxHeight > self.possibleHeight:
  33. self.maxHeight = self.possibleHeight
  34.  
  35. def give_true(self):
  36. self.isActive = True
  37.  
  38. def give_end_color(self):
  39. can.itemconfig(self.singleCube, fill = "#0ec0e2", outline = 'white')
  40. self.give_true()
  41.  
  42. def take_color(self):
  43. can.itemconfig(self.singleCube, fill = '#272822', outline = '#272822')
  44. self.isActive = False
  45.  
  46. def give_color(self, ftype):
  47. if ftype == 0:
  48. can.itemconfig(self.singleCube, fill = "#fd7521", outline = 'white') # it came to me accidentally
  49.  
  50. if ftype == 1:
  51. can.itemconfig(self.singleCube, fill = "#ae739a", outline = 'white')
  52.  
  53. if ftype == 2:
  54. can.itemconfig(self.singleCube, fill = "#f92672", outline = 'white')
  55.  
  56. if ftype == 3:
  57. can.itemconfig(self.singleCube, fill = "#a6e22a", outline = 'white')
  58.  
  59. if ftype == 4:
  60. can.itemconfig(self.singleCube, fill = "#66d9ef", outline = 'white')
  61.  
  62. def create_figure(self):
  63. self.figureType = random.randint(0, 4)
  64. self.rotate = 0
  65. # self.figureType = 0 #temporary
  66. if self.figureType == 0:
  67. self.figureCoords = [[0, 3], [0, 4], [0, 5], [1, 4]]
  68. for x in self.figureCoords:
  69. allCubes[x[0]][x[1]].give_color(self.figureType)
  70. if self.figureType == 1:
  71. self.figureCoords = [[0, 3], [0, 4], [0, 5], [0, 6]]
  72. for x in self.figureCoords:
  73. allCubes[x[0]][x[1]].give_color(self.figureType)
  74. if self.figureType == 2:
  75. self.figureCoords = [[0, 3], [0, 4], [1, 3], [1, 4]]
  76. for x in self.figureCoords:
  77. allCubes[x[0]][x[1]].give_color(self.figureType)
  78. if self.figureType == 3:
  79. self.figureCoords = [[0, 3], [0, 4], [1, 4], [1, 5]]
  80. for x in self.figureCoords:
  81. allCubes[x[0]][x[1]].give_color(self.figureType)
  82. if self.figureType == 4:
  83. self.figureCoords = [[0, 3], [0, 4], [1, 4], [2, 4]]
  84.  
  85. self.move_down()
  86.  
  87.  
  88. class Moving_Cubes(Cube):
  89. def move_down(self):
  90. if self.maxHeight == 0:
  91. exit()
  92. try:
  93. for x in self.figureCoords:
  94. allCubes[x[0]+1][x[1]]
  95. except IndexError:
  96.  
  97. self.stop_figure()
  98.  
  99. return
  100. else:
  101. for x in self.figureCoords:
  102. if allCubes[x[0]+1][x[1]].isActive == True:
  103. #
  104. self.stop_figure()
  105.  
  106. return
  107. for x in self.figureCoords:
  108. allCubes[x[0]][x[1]].take_color()
  109. for x in self.figureCoords:
  110. allCubes[x[0]+1][x[1]].give_color(self.figureType)
  111. x[0] += 1
  112.  
  113. can.after(100, self.move_down)
  114.  
  115. def move_left(self, event):
  116. for x in self.figureCoords:
  117. if x[1] == 0 or allCubes[x[0]][x[1]-1].isActive == True:
  118. return
  119. else:
  120. for x in self.figureCoords:
  121. allCubes[x[0]][x[1]].take_color()
  122. for x in self.figureCoords:
  123. allCubes[x[0]][x[1]-1].give_color(self.figureType)
  124. x[1] -= 1
  125.  
  126. def move_right(self, event):
  127. for x in self.figureCoords:
  128. if x[1] == 9 or allCubes[x[0]][x[1]+1].isActive == True:
  129. return
  130. else:
  131. for x in self.figureCoords:
  132. allCubes[x[0]][x[1]].take_color()
  133. for x in self.figureCoords:
  134. allCubes[x[0]][x[1]+1].give_color(self.figureType)
  135. x[1] += 1
  136.  
  137. def check_line(self):
  138. self.y = min(self.figureCoords)
  139. while self.y[0] < 25:
  140. x = 0
  141. while x < 10:
  142. if allCubes[self.y[0]][x].isActive == False:
  143. break
  144. elif x == 9:
  145. for z in range(0, 10):
  146. allCubes[self.y[0]][z].take_color()
  147. allCubes[self.y[0]][z].isActive = False
  148. self.move_the_line_down()
  149. x += 1
  150. self.y[0] += 1
  151.  
  152. def move_the_line_down(self):
  153. self.begin_moving = self.y[0]-1
  154. while self.begin_moving >= self.maxHeight:
  155. for x in range(10):
  156. if allCubes[self.begin_moving][x].isActive == True:
  157. allCubes[self.begin_moving][x].take_color()
  158. allCubes[self.begin_moving + 1][x].give_end_color()
  159. self.begin_moving -= 1
  160.  
  161. def rotate_figure(self, event):
  162. if self.figureType == 0: #harder than i thought
  163. if self.rotate == 0:
  164. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
  165. allCubes[self.figureCoords[0][0] - 1][self.figureCoords[0][1] + 1].give_color(self.figureType)
  166. self.figureCoords[0][0] -= 1
  167. self.figureCoords[0][1] += 1
  168. self.rotate += 1
  169.  
  170. elif self.rotate == 1:
  171. allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
  172. allCubes[self.figureCoords[3][0] - 1][self.figureCoords[3][1] - 1].give_color(self.figureType)
  173. self.figureCoords[3][0] -= 1
  174. self.figureCoords[3][1] -= 1
  175. self.rotate += 1
  176.  
  177. elif self.rotate == 2:
  178. allCubes[self.figureCoords[2][0]][self.figureCoords[2][1]].take_color()
  179. allCubes[self.figureCoords[2][0] + 1][self.figureCoords[2][1] - 1].give_color(self.figureType)
  180. self.figureCoords[2][0] += 1
  181. self.figureCoords[2][1] -= 1
  182. self.rotate += 1
  183.  
  184. elif self.rotate == 3: #oh fuck. what a shame
  185. allCubes[self.figureCoords[2][0]][self.figureCoords[2][1]].take_color()
  186. allCubes[self.figureCoords[2][0] - 1][self.figureCoords[2][1] + 1].give_color(self.figureType)
  187. self.figureCoords[2][0] -= 1
  188. self.figureCoords[2][1] += 1
  189.  
  190. allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
  191. allCubes[self.figureCoords[3][0] + 1][self.figureCoords[3][1] + 1].give_color(self.figureType)
  192. self.figureCoords[3][0] += 1
  193. self.figureCoords[3][1] += 1
  194.  
  195. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
  196. allCubes[self.figureCoords[0][0] + 1][self.figureCoords[0][1] - 1].give_color(self.figureType)
  197. self.figureCoords[0][0] += 1
  198. self.figureCoords[0][1] -= 1
  199.  
  200. self.rotate = 0
  201.  
  202. if self.figureType == 1:
  203. if self.rotate == 0:
  204. for x in range(1, 4):
  205. allCubes[self.figureCoords[x][0]][self.figureCoords[x][1]].take_color()
  206. allCubes[self.figureCoords[x][0] - x][self.figureCoords[x][1] - x].give_color(self.figureType)
  207. self.figureCoords[x][0] -= x
  208. self.figureCoords[x][1] -= x
  209. self.rotate = 1
  210. else:
  211. for x in range(1, 4):
  212. allCubes[self.figureCoords[x][0]][self.figureCoords[x][1]].take_color()
  213. allCubes[self.figureCoords[x][0] + x][self.figureCoords[x][1] + x].give_color(self.figureType)
  214. self.figureCoords[x][0] += x
  215. self.figureCoords[x][1] += x
  216. self.rotate = 0
  217.  
  218. if self.figureType == 2:
  219. return
  220.  
  221. if self.figureType == 3:
  222. if self.rotate == 0:
  223. allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
  224. allCubes[self.figureCoords[3][0] - 2][self.figureCoords[3][1]].give_color(self.figureType)
  225. self.figureCoords[3][0] -= 2
  226.  
  227. allCubes[self.figureCoords[2][0]][self.figureCoords[2][1]].take_color()
  228. allCubes[self.figureCoords[2][0] - 1][self.figureCoords[2][1] + 1].give_color(self.figureType)
  229. self.figureCoords[2][0] -= 1
  230. self.figureCoords[2][1] += 1
  231.  
  232. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
  233. allCubes[self.figureCoords[0][0] + 1][self.figureCoords[0][1] + 1].give_color(self.figureType)
  234. self.figureCoords[0][0] += 1
  235. self.figureCoords[0][1] += 1
  236. self.rotate = 1
  237.  
  238. else:
  239. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
  240. allCubes[self.figureCoords[0][0] - 1][self.figureCoords[0][1] - 1].give_color(self.figureType)
  241. self.figureCoords[0][0] -= 1
  242. self.figureCoords[0][1] -= 1
  243.  
  244. allCubes[self.figureCoords[2][0]][self.figureCoords[2][1]].take_color()
  245. allCubes[self.figureCoords[2][0] + 1][self.figureCoords[2][1] - 1].give_color(self.figureType)
  246. self.figureCoords[2][0] += 1
  247. self.figureCoords[2][1] -= 1
  248.  
  249. allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
  250. allCubes[self.figureCoords[3][0] + 2][self.figureCoords[3][1]].give_color(self.figureType)
  251. self.figureCoords[3][0] += 2
  252. self.rotate = 0
  253.  
  254. if self.figureType == 4: #please kill me
  255. if self.rotate == 0:
  256. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
  257. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1] + 2].give_color(self.figureType)
  258. self.figureCoords[0][1] += 2
  259.  
  260. allCubes[self.figureCoords[1][0]][self.figureCoords[1][1]].take_color()
  261. allCubes[self.figureCoords[1][0] + 1][self.figureCoords[1][1] + 1].give_color(self.figureType)
  262. self.figureCoords[1][0] += 1
  263. self.figureCoords[1][1] += 1
  264.  
  265. allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
  266. allCubes[self.figureCoords[3][0] - 1][self.figureCoords[3][1] - 1].give_color(self.figureType)
  267. self.figureCoords[3][0] -= 1
  268. self.figureCoords[3][1] -= 1
  269. self.rotate = 1
  270.  
  271. elif self.rotate == 1:
  272. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
  273. allCubes[self.figureCoords[0][0] + 2][self.figureCoords[0][1]].give_color(self.figureType)
  274. self.figureCoords[0][0] += 2
  275.  
  276. allCubes[self.figureCoords[1][0]][self.figureCoords[1][1]].take_color()
  277. allCubes[self.figureCoords[1][0] + 1][self.figureCoords[1][1] - 1].give_color(self.figureType)
  278. self.figureCoords[1][0] += 1
  279. self.figureCoords[1][1] -= 1
  280.  
  281. allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
  282. allCubes[self.figureCoords[3][0] - 1][self.figureCoords[3][1] + 1].give_color(self.figureType)
  283. self.figureCoords[3][0] -= 1
  284. self.figureCoords[3][1] += 1
  285. self.rotate = 2
  286.  
  287. elif self.rotate == 2:
  288. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
  289. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1] - 2].give_color(self.figureType)
  290. self.figureCoords[0][1] -= 2
  291.  
  292. allCubes[self.figureCoords[1][0]][self.figureCoords[1][1]].take_color()
  293. allCubes[self.figureCoords[1][0] - 1][self.figureCoords[1][1] - 1].give_color(self.figureType)
  294. self.figureCoords[1][0] -= 1
  295. self.figureCoords[1][1] -= 1
  296.  
  297. allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
  298. allCubes[self.figureCoords[3][0] + 1][self.figureCoords[3][1] + 1].give_color(self.figureType)
  299. self.figureCoords[3][0] += 1
  300. self.figureCoords[3][1] += 1
  301. self.rotate = 3
  302.  
  303. elif self.rotate == 3:
  304. allCubes[self.figureCoords[0][0]][self.figureCoords[0][1]].take_color()
  305. allCubes[self.figureCoords[0][0] - 2][self.figureCoords[0][1]].give_color(self.figureType)
  306. self.figureCoords[0][0] -= 2
  307.  
  308. allCubes[self.figureCoords[1][0]][self.figureCoords[1][1]].take_color()
  309. allCubes[self.figureCoords[1][0] - 1][self.figureCoords[1][1] + 1].give_color(self.figureType)
  310. self.figureCoords[1][0] -= 1
  311. self.figureCoords[1][1] += 1
  312.  
  313. allCubes[self.figureCoords[3][0]][self.figureCoords[3][1]].take_color()
  314. allCubes[self.figureCoords[3][0] + 1][self.figureCoords[3][1] - 1].give_color(self.figureType)
  315. self.figureCoords[3][0] += 1
  316. self.figureCoords[3][1] -= 1
  317. self.rotate = 0
  318.  
  319.  
  320. class Appearance():
  321. def __init__(self):
  322. self.level = Label(pointFrame, width = 5, text = 'Level', font = 'Arial 18', fg = '#f2e12e', bg = '#272822').pack(side ='left')
  323. self.levelValue = Entry(pointFrame, width = 5, fg = '#f2e12e', bg = '#272822').pack(side = 'right')
  324. self.score = Label(pointFrame, width = 5, text = 'Score', font = 'Arial 18', fg = '#f2e12e', bg = '#272822').pack(side = 'bottom')
  325.  
  326. allCubes = [[Moving_Cubes(x1, y1) for x1 in range(2, 200, 20)] \
  327. for y1 in range(2, 500, 20)]
  328.  
  329. Scores = Appearance()
  330. allCubes[0][1].create_figure()
  331.  
  332. #root.bind('<Down>', allCubes[0][1].move_down)
  333. root.bind('<Left>', allCubes[0][1].move_left)
  334. root.bind('<Right>', allCubes[0][1].move_right)
  335. root.bind('<Up>', allCubes[0][1].rotate_figure)
  336. mainloop()
Runtime error #stdin #stdout #stderr 0.06s 28272KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 4, in <module>
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1854, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable