-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
98 lines (86 loc) · 2.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from logics import *
import pygame
import sys
def draw_interface(score):
pygame.draw.rect(screen, WHITE, TITLE_REC)
font = pygame.font.SysFont('stixingkai', 70)
font_score = pygame.font.SysFont('simsun', 48)
text_score = font_score.render('Score: ', True, COLOR_TEXT)
text_score_value = font_score.render(f'{score}', True, COLOR_TEXT)
screen.blit(text_score, (20, 35))
screen.blit(text_score_value, (175, 35))
pretty_print(mas)
for row in range(BLOCKS):
for column in range(BLOCKS):
value = mas[row][column]
text = font.render(f'{value}', True, BLACK)
w = column * SIZE_BLOCK + (column + 1) * MARGIN
h = row * SIZE_BLOCK + (row + 1) * MARGIN + SIZE_BLOCK
pygame.draw.rect(screen, COLORS[value], (w, h, SIZE_BLOCK, SIZE_BLOCK))
if value != 0:
font_w, font_h = text.get_size()
text_x = w + (SIZE_BLOCK - font_w) / 2
text_y = h + (SIZE_BLOCK - font_h) / 2
screen.blit(text, (text_x, text_y))
mas = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
]
COLOR_TEXT = (255, 127, 0)
COLORS = {
0: (130, 130, 130),
2: (255, 255, 255),
4: (255, 255, 128),
8: (255, 255, 0),
16: (255, 255, 235),
32: (255, 235, 128),
64: (255, 235, 0),
128: (255, 225, 255),
256: (244, 233, 222),
512: (0, 255, 244)
}
WHITE = (255, 255, 255)
GRAY = (130, 130, 130)
BLACK = (0, 0, 0)
BLOCKS = 4
SIZE_BLOCK = 110
MARGIN = 10
WIDTH = BLOCKS * SIZE_BLOCK + (BLOCKS + 1) * MARGIN
HEIGTH = WIDTH + 110
TITLE_REC = pygame.Rect(0, 0, WIDTH, 110)
score = 0
mas[1][2] = 2
mas[3][0] = 4
print(get_empty_list(mas))
pretty_print(mas)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGTH))
pygame.display.set_caption('2048')
draw_interface(score)
pygame.display.update()
while is_zero_in_mas(mas) or can_move(mas):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
elif event.type == pygame.KEYDOWN:
delta = 0
if event.key == pygame.K_LEFT:
mas, delta = move_left(mas)
elif event.key == pygame.K_RIGHT:
mas, delta = move_right(mas)
elif event.key == pygame.K_UP:
mas, delta = move_up(mas)
elif event.key == pygame.K_DOWN:
mas, delta = move_down(mas)
score += delta
empty = get_empty_list(mas)
random.shuffle(empty)
random_num = empty.pop()
x, y = get_index_from_number(random_num)
mas = insert_2_or_4(mas, x, y)
print(f'Элемент под номером {random_num} заполнен')
draw_interface(score)
pygame.display.update()