Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/python-igra-tetris/main_lesson_4.py
5925 views
1
from tkinter import *
2
from tkinter import messagebox
3
from random import choice, randrange
4
5
W, H = 10, 20
6
TILE = 45
7
GAME_RES = W * TILE, H * TILE
8
RES = 750, 940
9
FPS = 60
10
11
12
def on_closing():
13
if messagebox.askokcancel("Выход из приложения", "Хотите выйти из приложения?"):
14
tk.destroy()
15
16
17
tk = Tk()
18
tk.protocol("WM_DELETE_WINDOW", on_closing)
19
tk.title("Tetris")
20
tk.resizable(0, 0)
21
tk.wm_attributes("-topmost", 1)
22
#tk.iconbitmap("bomb-3175208_640.ico")
23
24
sc = Canvas(tk, width=RES[0], height=RES[1], bg="red", highlightthickness=0)
25
sc.pack()
26
27
game_sc = Canvas(tk, width=W*TILE+1, height=H*TILE+1, bg="yellow", highlightthickness=0)
28
game_sc.place(x=20, y=20, anchor=NW)
29
30
img_obj1 = PhotoImage(file="img/bg.png")
31
sc.create_image(0, 0, anchor=NW, image=img_obj1)
32
33
img_obj2 = PhotoImage(file="img/bg2.png")
34
game_sc.create_image(0, 0, anchor=NW, image=img_obj2)
35
36
grid = [game_sc.create_rectangle(x * TILE, y * TILE, x * TILE+TILE, y * TILE+TILE) for x in range(W) for y in range(H)]
37
# for item in grid:
38
# game_sc.move(item, 20, 20)
39
40
score = 0
41
record = "0"
42
43
sc.create_text(505, 30,text="TETRIS", font=("WiGuru 2", 50),fill="red", anchor=NW)
44
sc.create_text(535, 780,text="score:", font=("WiGuru 2", 35),fill="white", anchor=NW)
45
sc.create_text(550, 840,text=str(score), font=("WiGuru 2", 35),fill="white", anchor=NW)
46
sc.create_text(525, 650,text="record:", font=("WiGuru 2", 35),fill="white", anchor=NW)
47
sc.create_text(550, 710,text=record, font=("WiGuru 2", 35),fill="gold", anchor=NW)
48
49
get_color = lambda : (randrange(30, 256), randrange(30, 256), randrange(30, 256))
50
51
def rgb_to_hex(rgb):
52
return '#%02x%02x%02x' % rgb
53
54
print(rgb_to_hex(get_color()))
55
56
57
figures_pos = [[(-1, 0), (-2, 0), (0, 0), (1, 0)],
58
[(0, -1), (-1, -1), (-1, 0), (0, 0)],
59
[(-1, 0), (-1, 1), (0, 0), (0, -1)],
60
[(0, 0), (-1, 0), (0, 1), (-1, -1)],
61
[(0, 0), (0, -1), (0, 1), (-1, -1)],
62
[(0, 0), (0, -1), (0, 1), (1, -1)],
63
[(0, 0), (0, -1), (0, 1), (-1, 0)]]
64
65
figures = [[(x + W // 2, y + 1, 1, 1) for x, y in fig_pos] for fig_pos in figures_pos]
66
#figure_rect = pygame.Rect(0, 0, TILE - 2, TILE - 2)
67
field = [[0 for i in range(W)] for j in range(H)]
68
69
from copy import deepcopy
70
figure, next_figure = deepcopy(choice(figures)), deepcopy(choice(figures))
71
color, next_color = get_color(), get_color()
72
73
# draw figure
74
for i in range(4):
75
figure_rect_x = figure[i][0] * TILE
76
figure_rect_y = figure[i][1] * TILE
77
game_sc.create_rectangle(figure_rect_x, figure_rect_y, figure_rect_x + TILE, figure_rect_y + TILE, fill=rgb_to_hex(color))
78
79
# draw next figure
80
for i in range(4):
81
figure_rect_x = next_figure[i][0] * TILE + 380
82
figure_rect_y = next_figure[i][1] * TILE + 185
83
sc.create_rectangle(figure_rect_x, figure_rect_y, figure_rect_x + TILE, figure_rect_y + TILE,
84
fill=rgb_to_hex(next_color))
85
86
87
88
for item in grid:
89
game_sc.itemconfigure(item, fill=rgb_to_hex(get_color()))
90
91
for item in grid:
92
game_sc.itemconfigure(item, fill="")
93
94
95
tk.mainloop()
96
97