-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048.py
More file actions
162 lines (142 loc) · 5.61 KB
/
2048.py
File metadata and controls
162 lines (142 loc) · 5.61 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import tkinter
from random import randint
from itertools import product
CELL_SIDE = int(input("cell side in pixels: "))
FIELD_WIDTH = int(input("field width in tiles: "))
FIELD_HEIGHT = int(input("field height in tiles: "))
MASTER = tkinter.Tk()
MASTER.title("2048 tkinter ripoff")
CANVAS = tkinter.Canvas(MASTER, bg="white", width=FIELD_WIDTH * CELL_SIDE + 1, height=FIELD_HEIGHT * CELL_SIDE + 1,
bd=0, highlightthickness=0)
def rgb(parts) -> str:
color = "#"
for part in parts:
part = hex(part)[2:]
if len(part) == 1:
color += '0'
color += part
return color
COLORS = list(product(range(63, 256, 64), range(63, 256, 64), range(63, 256, 64)))
COLORS.sort(key=lambda x: -sum(x))
COLORS = (None,) + tuple(map(rgb, COLORS))
tiles = [[0 for y in range(FIELD_HEIGHT)] for x in range(FIELD_WIDTH)]
ids = [[(None, None) for y in range(FIELD_HEIGHT)] for x in range(FIELD_WIDTH)]
free_tiles = FIELD_WIDTH * FIELD_HEIGHT
def delete_tile(x: int, y: int) -> None:
tiles[x][y] = 0
CANVAS.delete(*ids[x][y])
ids[x][y] = (None, None)
def set_tile(x: int, y: int, tile_type: int) -> None:
delete_tile(x, y)
tiles[x][y] = tile_type
cx = x * CELL_SIDE
cy = y * CELL_SIDE
ids[x][y] = (CANVAS.create_rectangle(cx, cy, cx + CELL_SIDE, cy + CELL_SIDE, fill=COLORS[tile_type]),
CANVAS.create_text(cx + CELL_SIDE // 2, cy + CELL_SIDE // 2, text=str(2 ** tile_type), font="courier"))
def create_random() -> None:
global free_tiles
place_delay = randint(1, free_tiles)
for x in range(FIELD_WIDTH):
for y in range(FIELD_HEIGHT):
if not tiles[x][y]:
place_delay -= 1
if not place_delay:
set_tile(x, y, 1 if randint(0, 10) else 2)
free_tiles -= 1
return
def step(event: tkinter.Event) -> None:
global free_tiles
if event.keysym == "Left":
for y in range(FIELD_HEIGHT):
last = 0
while last < FIELD_WIDTH and not tiles[last][y]:
last += 1
if last != FIELD_WIDTH:
set_tile(0, y, tiles[last][y])
if last:
delete_tile(last, y)
last = 0
for x in range(1, FIELD_WIDTH):
if tiles[x][y]:
if tiles[last][y] == tiles[x][y]:
set_tile(last, y, tiles[last][y] + 1)
delete_tile(x, y)
free_tiles += 1
else:
last += 1
set_tile(last, y, tiles[x][y])
if last != x:
delete_tile(x, y)
elif event.keysym == "Right":
for y in range(FIELD_HEIGHT):
last = FIELD_WIDTH - 1
while last > -1 and not tiles[last][y]:
last -= 1
if last != -1:
set_tile(FIELD_WIDTH - 1, y, tiles[last][y])
if last != FIELD_WIDTH - 1:
delete_tile(last, y)
last = FIELD_WIDTH - 1
for x in range(FIELD_WIDTH - 2, -1, -1):
if tiles[x][y]:
if tiles[last][y] == tiles[x][y]:
set_tile(last, y, tiles[last][y] + 1)
delete_tile(x, y)
free_tiles += 1
else:
last -= 1
set_tile(last, y, tiles[x][y])
if last != x:
delete_tile(x, y)
elif event.keysym == "Up":
for x in range(FIELD_WIDTH):
last = 0
while last < FIELD_HEIGHT and not tiles[x][last]:
last += 1
if last != FIELD_HEIGHT:
set_tile(x, 0, tiles[x][last])
if last:
delete_tile(x, last)
last = 0
for y in range(1, FIELD_HEIGHT):
if tiles[x][y]:
if tiles[x][last] == tiles[x][y]:
set_tile(x, last, tiles[x][last] + 1)
delete_tile(x, y)
free_tiles += 1
else:
last += 1
set_tile(x, last, tiles[x][y])
if last != y:
delete_tile(x, y)
elif event.keysym == "Down":
for x in range(FIELD_WIDTH):
last = FIELD_HEIGHT - 1
while last > -1 and not tiles[x][last]:
last -= 1
if last != -1:
set_tile(x, FIELD_HEIGHT - 1, tiles[x][last])
if last != FIELD_HEIGHT - 1:
delete_tile(x, last)
last = FIELD_HEIGHT - 1
for y in range(FIELD_HEIGHT - 2, -1, -1):
if tiles[x][y]:
if tiles[x][last] == tiles[x][y]:
set_tile(x, last, tiles[x][last] + 1)
delete_tile(x, y)
free_tiles += 1
else:
last -= 1
set_tile(x, last, tiles[x][y])
if last != y:
delete_tile(x, y)
else:
return
if (free_tiles):
create_random()
def main() -> None:
MASTER.bind("<KeyPress>", step)
CANVAS.pack()
MASTER.mainloop()
if __name__ == "__main__":
main()