plan_war/main2.lua
2025-05-13 17:52:48 +08:00

97 lines
3.3 KiB
Lua

local core = require("core")
local block_pic = {}
local bk
local over_flag = false
local new_block = {flag = false}
local wH --window height
local wW --window weight
local bW --block width
local startpos = {}
local delay = 0
function love.load()
love.window.setFullscreen()
wH = love.window.getHeight()
wW = love.window.getWidth()
bW = 0.8 * wH / 4
bk = love.graphics.newImage("src/bk.jpg")
for i = 1, 11 do
block_pic[tostring(math.pow(2,i))] = love.graphics.newImage("src/"..tostring(math.pow(2,i))..".PNG")
end
love.graphics.setBackgroundColor(255, 255, 255)
love.graphics.setNewFont(24)
love.graphics.setColor(255, 255, 255)
core.initial()
end
local function draw_block(index, value)
local line = math.modf((index - 1)/4)
local row = (index - 1) % 4
local pic_index = tostring(value)
love.graphics.draw(block_pic[pic_index], 0.1 * wH + row * bW, 0.1 * wH + line * bW, 0, bW/block_pic[pic_index]:getWidth(), bW/block_pic[pic_index]:getHeight())
end
function love.draw()
local scorestr = "SCORE:\n"..core.score.."\nBEST:\n"..core.best
love.graphics.draw(bk, 0, 0, 0, wW/bk:getWidth(), wH/bk:getHeight())
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("line", 0.1 * wH, 0.1 * wH, 0.8 * wH, 0.8 * wH)
for i = 1, 16 do
if core.block[i] then
draw_block(i, core.block[i])
end
end
if new_block.flag then
if delay < 10 then delay = delay + 1
else
draw_block(new_block.index, new_block.value)
core.block[new_block.index] = new_block.value
new_block.flag = false
delay = 0
end
end
love.graphics.print(scorestr, wH, wH * 0.1)
if over_flag then
love.graphics.setColor(0, 0, 255)
love.graphics.rectangle("fill", 0.25 * wW, 0.25 * wH, 0.5 * wW, 0.5 * wH)
love.graphics.setColor(255,255,255)
love.graphics.print(scorestr, 0.45 * wW, 0.45 * wH)
end
end
function love.mousepressed(x, y, button)
if button == 'l' then
startpos.x = x
startpos.y = y
end
end
function love.mousereleased(x, y, button)
if button == 'l' then
if over_flag then
over_flag = false
core.initial()
return
end
local x_dis = x - startpos.x
local y_dis = y - startpos.y
local ret
if y_dis < 0 and math.abs(y_dis) > math.abs(x_dis) then
ret = core.up_move()
elseif y_dis > 0 and math.abs(y_dis) > math.abs(x_dis) then
ret = core.down_move()
elseif x_dis < 0 and math.abs(x_dis) > math.abs(y_dis) then
ret = core.left_move()
elseif x_dis > 0 and math.abs(x_dis) > math.abs(y_dis) then
ret = core.right_move()
end
if not ret then return end
new_block = core.new_block()
if not new_block then return end
new_block.flag = true
local testtbl = core.tblclone(core.block, 16)
testtbl[new_block.index] = new_block.value
if core.isfull(testtbl) then
if core.up_move(testtbl) or core.down_move(testtbl) or core.left_move(testtbl) or core.right_move(testtbl) then
return
end
core.set_best()
over_flag = true
end
end
end