185 lines
5.6 KiB
Lua
185 lines
5.6 KiB
Lua
FLOOR = 0
|
|
GOAL = 1
|
|
BOX = 2
|
|
PLAYER = 4
|
|
WALL = 7
|
|
|
|
function init()
|
|
mode(2)
|
|
setchar(127,0xee,0xee,0xee,0x00,0xbb,0xbb,0xbb,0x00)
|
|
setchar(16,0xff,0xc3,0xa5,0x99,0x99,0xa5,0xc3,0xff)
|
|
menu.init()
|
|
end
|
|
|
|
-- ====================================================================================================
|
|
-- MENU (game state)
|
|
-- ====================================================================================================
|
|
menu={
|
|
init = function()
|
|
rst()
|
|
update=menu.update
|
|
end,
|
|
|
|
update = function()
|
|
color(15,0) cls()
|
|
ink(1) print("\x20\x20\x20\x87\x81\x87\x85\x85\x85\x87\x85\x87\x85\x87\x85\x87\x84\x20\x20\x20",0,2)
|
|
ink(9) print("\x20\x20\x20\x83\x85\x85\x85\x8D\x81\x85\x85\x87\x84\x8D\x85\x85\x85\x20\x20\x20",0,3)
|
|
ink(11) print("\x20\x20\x20\x83\x81\x83\x81\x81\x81\x83\x81\x83\x81\x81\x81\x81\x81\x20\x20\x20",0,4)
|
|
ink(15*flr((cnt()%60)/30)) print("PRESS SPACE TO PLAY",1,9)
|
|
|
|
if btnp(KEY_SPACE) then
|
|
level.start()
|
|
game.init()
|
|
end
|
|
end
|
|
}
|
|
|
|
-- ====================================================================================================
|
|
-- GAME (game state)
|
|
-- ====================================================================================================
|
|
game={
|
|
init = function()
|
|
update=game.update
|
|
level.load()
|
|
end,
|
|
|
|
update = function()
|
|
border(0)
|
|
color(1,0)
|
|
cls()
|
|
level.draw()
|
|
|
|
if btnp(KEY_UP) then level.try_move(0,-1)
|
|
elseif btnp(KEY_DOWN) then level.try_move(0,1)
|
|
elseif btnp(KEY_LEFT) then level.try_move(-1,0)
|
|
elseif btnp(KEY_RIGHT) then level.try_move(1,0)
|
|
end
|
|
end
|
|
}
|
|
|
|
-- ====================================================================================================
|
|
-- LOADING (game state)
|
|
-- ====================================================================================================
|
|
loading = {
|
|
init = function()
|
|
border(15)
|
|
update=loading.update
|
|
rst()
|
|
end,
|
|
|
|
update = function()
|
|
color(15,0)
|
|
cls()
|
|
print("LEVEL "..num_level,6,5)
|
|
print("GOOD JOB!",5,8)
|
|
if (cnt()%60)<15 then print("\248",9,10)
|
|
elseif (cnt()%60)<30 then print("\250",9,10)
|
|
elseif (cnt()%60)<45 then print("\248",9,10)
|
|
else print("\251",9,10) end
|
|
if cnt()==120 then
|
|
level.next()
|
|
game.init()
|
|
end
|
|
end
|
|
}
|
|
|
|
|
|
-- ====================================================================================================
|
|
-- LEVEL
|
|
-- ====================================================================================================
|
|
levels= {
|
|
"####|# .#|# ###|#*@ #|# $ #|# ###|####",
|
|
"######|# #|# #@ #|# $* #|# .* #|# #|######",
|
|
" ####|### ####|# $ #|# # #$ #|# . .#@ #|#########",
|
|
"########|# #|# .**$@#|# #|##### #| ####",
|
|
" #######| # #| # .$. #|## $@$ #|# .$. #|# #|########",
|
|
"###### #####|# ### #|# $$ #@#|# $ #... #|# ########|#####",
|
|
"#######|# .$. #|# $.$ #|# .$. #|# $.$ #|# @ #|#######",
|
|
"#####|#. ##|#@$$ #|## #| ## #| ##.#| ###"
|
|
}
|
|
|
|
level = {
|
|
start = function() num_level=1 end,
|
|
next = function() num_level=num_level+1 end,
|
|
|
|
load = function()
|
|
local str = levels[num_level]
|
|
player={}
|
|
map={}
|
|
local pos=0
|
|
local y,x=1,1
|
|
local maxx=0
|
|
map[y] = {}
|
|
while pos<#str do
|
|
local chr = ascii(str,pos)
|
|
if chr == 35 then map[y][x] = WALL end
|
|
if chr == 64 then player.x,player.y=x,y map[y][x] = FLOOR end
|
|
if chr == 43 then player.x,player.y=x,y map[y][x] = GOAL end
|
|
if chr == 36 then map[y][x] = BOX end
|
|
if chr == 42 then map[y][x] = GOAL+BOX end
|
|
if chr == 46 then map[y][x] = GOAL end
|
|
if chr == 32 then map[y][x] = FLOOR end
|
|
if chr == 124 then
|
|
y=y+1
|
|
map[y] = {}
|
|
if maxx < x then maxx=x end
|
|
x=0
|
|
end
|
|
pos=pos+1
|
|
x=x+1
|
|
end
|
|
ox = flr((20-maxx)/2)
|
|
oy = flr((15-y)/2)
|
|
end,
|
|
|
|
draw = function()
|
|
for y=1,#map do
|
|
for x=1,#map[y] do
|
|
if map[y][x] == WALL then color(4,0) print("\127",ox+x-1,oy+y-1)
|
|
elseif map[y][x] == BOX then color(8,7) print("\016",ox+x-1,oy+y-1)
|
|
elseif map[y][x] == BOX+GOAL then color(3,11) print("\016",ox+x-1,oy+y-1)
|
|
elseif map[y][x] == GOAL then color(3,0) print("\144",ox+x-1,oy+y-1)
|
|
end
|
|
end
|
|
end
|
|
color(15,0) print("\248",ox+player.x-1,oy+player.y-1)
|
|
end,
|
|
|
|
try_move = function(x,y)
|
|
if level.is_empty(x,y) then
|
|
level.move_player(x,y)
|
|
elseif level.is_box(x,y) and level.is_empty(x*2,y*2) then
|
|
level.move_box(x,y)
|
|
end
|
|
end,
|
|
|
|
is_empty = function(x,y)
|
|
return map[player.y+y][player.x+x] < 2
|
|
end,
|
|
|
|
is_box = function(x,y)
|
|
return map[player.y+y][player.x+x] < 4
|
|
end,
|
|
|
|
move_player = function(x,y)
|
|
player.x=player.x+x
|
|
player.y=player.y+y
|
|
end,
|
|
|
|
move_box = function(x,y)
|
|
map[player.y+y*2][player.x+x*2] = map[player.y+y*2][player.x+x*2] + BOX
|
|
map[player.y+y][player.x+x] = map[player.y+y][player.x+x] - BOX
|
|
level.move_player(x,y)
|
|
level.check_finished()
|
|
end,
|
|
|
|
check_finished = function()
|
|
for y=1,#map do
|
|
for x=1,#map[y] do
|
|
if map[y][x] == GOAL then return end
|
|
end
|
|
end
|
|
loading.init()
|
|
end
|
|
}
|