-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain
More file actions
77 lines (70 loc) · 2.14 KB
/
Copy pathmain
File metadata and controls
77 lines (70 loc) · 2.14 KB
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
from logics import *
import pygame
import sys
def draw_interface():
pygame.draw.rect(screen, WHITE, TITLE_REC)
font = pygame.font.SysFont("stxingkai", 70)
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],
]
COLORS = {
0: (130, 130, 130),
2: (255, 255, 255),
4: (255, 255, 128),
8: (255, 255, 0),
16: (255, 255, 255),
32: (255, 255, 128),
64: (255, 255, 0),
}
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)
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()
pygame.display.update()
while is_zero_in_mas(mas):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
mas = move_left(mas)
elif event.key == pygame.K_RIGHT:
mas = move_right(mas)
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'We filled in the item under the number {random_num}')
draw_interface()
pygame.display.update()