Path: blob/master/python-igra-tetris/main_lesson_4.py
5925 views
from tkinter import *1from tkinter import messagebox2from random import choice, randrange34W, H = 10, 205TILE = 456GAME_RES = W * TILE, H * TILE7RES = 750, 9408FPS = 6091011def on_closing():12if messagebox.askokcancel("Выход из приложения", "Хотите выйти из приложения?"):13tk.destroy()141516tk = Tk()17tk.protocol("WM_DELETE_WINDOW", on_closing)18tk.title("Tetris")19tk.resizable(0, 0)20tk.wm_attributes("-topmost", 1)21#tk.iconbitmap("bomb-3175208_640.ico")2223sc = Canvas(tk, width=RES[0], height=RES[1], bg="red", highlightthickness=0)24sc.pack()2526game_sc = Canvas(tk, width=W*TILE+1, height=H*TILE+1, bg="yellow", highlightthickness=0)27game_sc.place(x=20, y=20, anchor=NW)2829img_obj1 = PhotoImage(file="img/bg.png")30sc.create_image(0, 0, anchor=NW, image=img_obj1)3132img_obj2 = PhotoImage(file="img/bg2.png")33game_sc.create_image(0, 0, anchor=NW, image=img_obj2)3435grid = [game_sc.create_rectangle(x * TILE, y * TILE, x * TILE+TILE, y * TILE+TILE) for x in range(W) for y in range(H)]36# for item in grid:37# game_sc.move(item, 20, 20)3839score = 040record = "0"4142sc.create_text(505, 30,text="TETRIS", font=("WiGuru 2", 50),fill="red", anchor=NW)43sc.create_text(535, 780,text="score:", font=("WiGuru 2", 35),fill="white", anchor=NW)44sc.create_text(550, 840,text=str(score), font=("WiGuru 2", 35),fill="white", anchor=NW)45sc.create_text(525, 650,text="record:", font=("WiGuru 2", 35),fill="white", anchor=NW)46sc.create_text(550, 710,text=record, font=("WiGuru 2", 35),fill="gold", anchor=NW)4748get_color = lambda : (randrange(30, 256), randrange(30, 256), randrange(30, 256))4950def rgb_to_hex(rgb):51return '#%02x%02x%02x' % rgb5253print(rgb_to_hex(get_color()))545556figures_pos = [[(-1, 0), (-2, 0), (0, 0), (1, 0)],57[(0, -1), (-1, -1), (-1, 0), (0, 0)],58[(-1, 0), (-1, 1), (0, 0), (0, -1)],59[(0, 0), (-1, 0), (0, 1), (-1, -1)],60[(0, 0), (0, -1), (0, 1), (-1, -1)],61[(0, 0), (0, -1), (0, 1), (1, -1)],62[(0, 0), (0, -1), (0, 1), (-1, 0)]]6364figures = [[(x + W // 2, y + 1, 1, 1) for x, y in fig_pos] for fig_pos in figures_pos]65#figure_rect = pygame.Rect(0, 0, TILE - 2, TILE - 2)66field = [[0 for i in range(W)] for j in range(H)]6768from copy import deepcopy69figure, next_figure = deepcopy(choice(figures)), deepcopy(choice(figures))70color, next_color = get_color(), get_color()7172# draw figure73for i in range(4):74figure_rect_x = figure[i][0] * TILE75figure_rect_y = figure[i][1] * TILE76game_sc.create_rectangle(figure_rect_x, figure_rect_y, figure_rect_x + TILE, figure_rect_y + TILE, fill=rgb_to_hex(color))7778# draw next figure79for i in range(4):80figure_rect_x = next_figure[i][0] * TILE + 38081figure_rect_y = next_figure[i][1] * TILE + 18582sc.create_rectangle(figure_rect_x, figure_rect_y, figure_rect_x + TILE, figure_rect_y + TILE,83fill=rgb_to_hex(next_color))84858687for item in grid:88game_sc.itemconfigure(item, fill=rgb_to_hex(get_color()))8990for item in grid:91game_sc.itemconfigure(item, fill="")929394tk.mainloop()959697