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
|
extends Node2D
var matrix
var grid_cells
var history_matrixs
var won = false
var lost = false
func _ready():
randomize()
grid_cells = []
init_grid()
matrix = Logic.new_game(Constants.GRID_LEN)
history_matrixs = []
update_grid_cells()
const Block = preload("res://Block.tscn")
func init_grid():
for i in range(Constants.GRID_LEN):
var grid_row = []
for j in range(Constants.GRID_LEN):
var block = Block.instance()
add_child(block)
block.global_position += Vector2(
i * Constants.BLOCK_SIZE + 10, j * Constants.BLOCK_SIZE + 10
)
grid_row.append(block)
grid_cells.append(grid_row)
func update_grid_cells():
for i in range(Constants.GRID_LEN):
for j in range(Constants.GRID_LEN):
var new_number = matrix[i][j]
if new_number == 0:
grid_cells[i][j].configure("", Constants.BACKGROUND_COLOR_CELL_EMPTY)
else:
grid_cells[i][j].configure(
str(new_number),
Constants.BACKGROUND_COLOR_ARRAY[Constants.cells[new_number]],
Constants.CELL_COLOR_ARRAY[Constants.cells[new_number]]
)
func _input(event):
var input = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
if event.is_action("quit"):
get_tree().quit()
# elif event.is_action("undo") and history_matrixs.size() > 1: # dont want the undo feature anymore
# self.matrix = history_matrixs.pop_back()
# update_grid_cells()
# print('back on step total step:' + str(history_matrixs.size()))
elif input != Vector2.ZERO:
handle_input(input)
func handle_input(input):
if lost:
return
var tmp
match input:
Vector2.UP:
tmp = Logic.up(matrix)
Vector2.DOWN:
tmp = Logic.down(matrix)
Vector2.LEFT:
tmp = Logic.left(matrix)
Vector2.RIGHT:
tmp = Logic.right(matrix)
_:
return
matrix = tmp[0]
if tmp[1]:
matrix = Logic.add_two(matrix)
# history_matrixs.append(matrix)
update_grid_cells()
var state = Logic.game_state(matrix)
if state == "win" and not won:
won = true
grid_cells[0][0].configure(
"You", Constants.BACKGROUND_COLOR_CELL_EMPTY, Constants.LOSE_COLOR_TEXT
)
grid_cells[1][0].configure(
"won", Constants.BACKGROUND_COLOR_CELL_EMPTY, Constants.LOSE_COLOR_TEXT
)
elif state == "lose":
lost = true
grid_cells[0][0].configure(
"You", Constants.BACKGROUND_COLOR_CELL_EMPTY, Constants.LOSE_COLOR_TEXT
)
grid_cells[1][0].configure(
"lost", Constants.BACKGROUND_COLOR_CELL_EMPTY, Constants.LOSE_COLOR_TEXT
) # lmao L
func _on_SwipeHandler_swiped(swipe):
handle_input(swipe)
|