-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
247 lines (230 loc) · 7.45 KB
/
main.lua
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
local function swap(a, b)
return b, a
end
local function DoTableMove(a, b, c, d)
local testTable = {a, b, c, d}
for h = 1, 3 do
--是否需要搬运数字
local isCarry = false
if testTable[h] == 0 then
isCarry = true
end
for g = h + 1, 4 do
if testTable[g] ~= 0 and isCarry then
testTable[h], testTable[g] = swap(testTable[h], testTable[g])
isCarry = false
elseif isCarry == false and testTable[g] ~= 0 then
if testTable[g] == testTable[h] then
testTable[h] = testTable[g] + testTable[h]
testTable[g] = 0
GameScore = GameScore + testTable[h]
end
break
end
end
end
print(table.concat(testTable, ","))
return testTable[1], testTable[2], testTable[3], testTable[4]
end
local function DetectionKeyClick()
--按键检测
if(love.keyboard.isDown("up")) then
for i = 1, 4 do
GameArray[i][1], GameArray[i][2], GameArray[i][3], GameArray[i][4] = DoTableMove(GameArray[i][1], GameArray[i][2], GameArray[i][3], GameArray[i][4])
end
end
if(love.keyboard.isDown("down")) then
for i = 1, 4 do
GameArray[i][4], GameArray[i][3], GameArray[i][2], GameArray[i][1] = DoTableMove(GameArray[i][4], GameArray[i][3], GameArray[i][2], GameArray[i][1])
end
end
if(love.keyboard.isDown("left")) then
for j = 1, 4 do
GameArray[1][j], GameArray[2][j], GameArray[3][j], GameArray[4][j] = DoTableMove(GameArray[1][j], GameArray[2][j], GameArray[3][j], GameArray[4][j])
end
end
if(love.keyboard.isDown("right")) then
for j = 1, 4 do
GameArray[4][j], GameArray[3][j], GameArray[2][j], GameArray[1][j] = DoTableMove(GameArray[4][j], GameArray[3][j], GameArray[2][j], GameArray[1][j])
end
end
end
isLose = false
GameScore = 0
GameHighScore = 0
GameArray = {}
TableCache = {}
function love.load()
love.window.setMode(600, 750)
music = love.audio.newSource("BG.mp3", "stream")
clickSound = love.audio.newSource("btnClick.mp3", "static")
love.audio.play(music)
BgImage = love.graphics.newImage("bg.jpg")
BgTransform = love.math.newTransform(0, 0, 0, 0.8, 0.8, 580, 80)
LoseImage = love.graphics.newImage("lose.png")
LoseTransform = love.math.newTransform(0, 0, 0, 0.8, 0.8, 580, 80)
SquareImage = love.graphics.newImage("kpbg.png")
InitGame()
end
function love.update(dt)
local musicNum = love.audio.getActiveSourceCount()
if musicNum == 0 then
love.audio.play(music)
end
--print(musicNum)
end
function love.draw()
love.graphics.draw(BgImage, BgTransform)
--打印当前积分
love.graphics.print(GameScore, 190, 50, 0, 2, 2)
--打印最高分数
love.graphics.print(GameHighScore, 402, 50, 0, 2, 2)
UpdateGameScreen()
if isLose then
love.graphics.draw(LoseImage, LoseTransform)
end
end
function love.keyreleased(key)
if isLose then
return
end
for i = 1, 4 do
for j = 1, 4 do
TableCache[i][j] = GameArray[i][j]
end
end
if(key =="up") then
for i = 1, 4 do
GameArray[i][1], GameArray[i][2], GameArray[i][3], GameArray[i][4] = DoTableMove(GameArray[i][1], GameArray[i][2], GameArray[i][3], GameArray[i][4])
end
end
if(key =="down") then
for i = 1, 4 do
GameArray[i][4], GameArray[i][3], GameArray[i][2], GameArray[i][1] = DoTableMove(GameArray[i][4], GameArray[i][3], GameArray[i][2], GameArray[i][1])
end
end
if(key =="left") then
for j = 1, 4 do
GameArray[1][j], GameArray[2][j], GameArray[3][j], GameArray[4][j] = DoTableMove(GameArray[1][j], GameArray[2][j], GameArray[3][j], GameArray[4][j])
end
end
if(key =="right") then
for j = 1, 4 do
GameArray[4][j], GameArray[3][j], GameArray[2][j], GameArray[1][j] = DoTableMove(GameArray[4][j], GameArray[3][j], GameArray[2][j], GameArray[1][j])
end
end
for i = 1, 4 do
for j = 1, 4 do
if TableCache[i][j] ~= GameArray[i][j] then
RandomSetSquare2()
love.audio.play(clickSound)
isLose = detectIsLose()
return
end
end
end
end
function love.mousereleased(x, y, button, istouch)
if button == 1 then
fireSlingshot(x,y) -- this totally awesome custom function is defined elsewhere
end
end
--点击位置
function fireSlingshot(x,y)
if x > 430 and x < 530 and y > 630 and y < 670 then
love.audio.play(clickSound)
InitGame()
end
end
--检测玩家是否输了
function detectIsLose()
for x = 1, 4 do
for y = 1, 4 do
local fx = x - 1
local fy = y
if fx >= 1 and fx <= 4 and fy >= 1 and fy <= 4 then
if GameArray[fx][fy] == 0 or GameArray[x][y] == GameArray[fx][fy] then
return false
end
end
fx = x + 1
fy = y
if fx >= 1 and fx <= 4 and fy >= 1 and fy <= 4 then
if GameArray[fx][fy] == 0 or GameArray[x][y] == GameArray[fx][fy] then
return false
end
end
fx = x
fy = y - 1
if fx >= 1 and fx <= 4 and fy >= 1 and fy <= 4 then
if GameArray[fx][fy] == 0 or GameArray[x][y] == GameArray[fx][fy] then
return false
end
end
fx = x
fy = y + 1
if fx >= 1 and fx <= 4 and fy >= 1 and fy <= 4 then
if GameArray[fx][fy] == 0 or GameArray[x][y] == GameArray[fx][fy] then
return false
end
end
end
end
return true
end
--创建数字方块
function CreaterSquare(arrayX, arrayY, num)
local fromX = 75
local formY = 130
local x = fromX + (arrayX - 1) * 120
local y = formY + (arrayY - 1) * 120
local SquareTransform = love.math.newTransform(x, y, 0, 0.8, 0.8, 0, 0)
love.graphics.draw(SquareImage, SquareTransform)
assert(type(num) == "number", "num 不是一个数字")
local numberStr = tostring(num)
local numberlen = string.len(numberStr)
love.graphics.print(numberStr, x + 45 - ((numberlen - 1) * 10), y + 35, 0, 2.4, 2.4)
end
--初始化游戏
function InitGame()
isLose = false
GameScore = 0
math.randomseed(os.time())
--print(os.time())
for i = 1, 4 do
local son = {}
GameArray[i] = son
for j = 1, 4 do
son[j] = 0
end
end
for i = 1, 4 do
local son = {}
TableCache[i] = son
for j = 1, 4 do
son[j] = 0
end
end
RandomSetSquare2()
RandomSetSquare2()
end
--随机生成1个2号方块
function RandomSetSquare2()
local numRandX
local numRandY
repeat
numRandX = math.random(4)
numRandY = math.random(4)
until GameArray[numRandX][numRandY] == 0
GameArray[numRandX][numRandY] = 2
end
--在视图里显示数组
function UpdateGameScreen()
for i = 1, #GameArray do
for j = 1, #GameArray[i] do
if GameArray[i][j] ~= 0 then
CreaterSquare(i, j, GameArray[i][j])
end
end
end
end