-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
84 lines (71 loc) · 1.91 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
local Map = require("lib.map").Map
local window_width = 800
local window_height = 600
local time_spent = 0
local moves = 0
local level = "assets/maps/test.map.lua"
local map = {}
local function draw_timer()
local minutes = math.floor(time_spent / 60)
local seconds = math.floor(time_spent % 60)
local milliseconds = math.floor((time_spent * 100) % 100)
local time_string = string.format("%02d:%02d:%02d", minutes, seconds, milliseconds)
love.graphics.setColor(255, 255, 255)
love.graphics.print(time_string, 450, 50)
end
local function draw_moves()
love.graphics.setColor(255, 255, 255)
love.graphics.print("Moves: " .. moves, 450, 100)
end
local function draw_win()
love.graphics.setColor(0, 255, 255)
love.graphics.print("YOU WON!", 450, 150)
love.graphics.print("R to restart", 450, 200)
end
local function reset_level()
map = Map.new(level)
time_spent = 0.0
moves = 0
end
function love.load()
math.randomseed(os.time())
love.window.setMode(window_width, window_height, {
resizable = false,
vsync = false
})
love.graphics.setNewFont(50)
map = Map.new(level)
end
function love.draw()
map:draw(window_width / 2, window_height / 2)
draw_timer()
draw_moves()
if map.game_won then
draw_win()
end
end
function love.update(dt)
if not map.game_won then
time_spent = time_spent + dt
end
map:update(dt)
end
function love.keypressed(k)
if map.game_won then
if k == "r" then
reset_level()
elseif k == "q" then
love.event.quit()
end
return
end
if k == "up" then
moves = moves + map:move_player(0, -1)
elseif k == "down" then
moves = moves + map:move_player(0, 1)
elseif k == "left" then
moves = moves + map:move_player(-1, 0)
elseif k == "right" then
moves = moves + map:move_player(1, 0)
end
end