Added sokoban WIP to demos
This commit is contained in:
121
demos/sokoban.lua
Normal file
121
demos/sokoban.lua
Normal file
@@ -0,0 +1,121 @@
|
||||
FLOOR = 0
|
||||
GOAL = 1
|
||||
BOX = 2
|
||||
PLAYER = 4
|
||||
WALL = 7
|
||||
|
||||
function init()
|
||||
mode(2)
|
||||
level=1
|
||||
player={}
|
||||
init_level()
|
||||
end
|
||||
|
||||
function init_level()
|
||||
update=update_level
|
||||
load_from_string("####|# .#|# ###|#*@ #|# $ #|# ###|####")
|
||||
end
|
||||
|
||||
function load_from_string(str)
|
||||
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
|
||||
|
||||
function update_level()
|
||||
border(0)
|
||||
color(1,0)
|
||||
cls()
|
||||
for y=1,#map do
|
||||
for x=1,#map[y] do
|
||||
if map[y][x] == WALL then color(4,15) print("\127",ox+x-1,oy+y-1)
|
||||
elseif map[y][x]&BOX == BOX then color(0,6) print("\016",ox+x-1,oy+y-1)
|
||||
elseif map[y][x] == GOAL then color(4,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)
|
||||
|
||||
if btnp(KEY_UP) then try_move(0,-1)
|
||||
elseif btnp(KEY_DOWN) then try_move(0,1)
|
||||
elseif btnp(KEY_LEFT) then try_move(-1,0)
|
||||
elseif btnp(KEY_RIGHT) then try_move(1,0)
|
||||
end
|
||||
end
|
||||
|
||||
function try_move(x,y)
|
||||
if is_empty(x,y) then
|
||||
move_player(x,y)
|
||||
elseif is_box(x,y) and is_empty(x*2,y*2) then
|
||||
move_box(x,y)
|
||||
end
|
||||
end
|
||||
|
||||
function is_empty(x,y)
|
||||
return map[player.y+y][player.x+x] < 2
|
||||
end
|
||||
|
||||
function is_box(x,y)
|
||||
return map[player.y+y][player.x+x] < 4
|
||||
end
|
||||
|
||||
function move_player(x,y)
|
||||
player.x=player.x+x
|
||||
player.y=player.y+y
|
||||
end
|
||||
|
||||
function move_box(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
|
||||
move_player(x,y)
|
||||
check_finished()
|
||||
end
|
||||
|
||||
function check_finished()
|
||||
for y=1,#map do
|
||||
for x=1,#map[y] do
|
||||
if map[y][x] == GOAL then return end
|
||||
end
|
||||
end
|
||||
init_goodjob()
|
||||
end
|
||||
|
||||
function init_goodjob()
|
||||
border(15)
|
||||
update=update_goodjob
|
||||
wait=120
|
||||
end
|
||||
|
||||
function update_goodjob()
|
||||
color(15,0)
|
||||
cls()
|
||||
print("LEVEL "..level,6,6)
|
||||
print("GOOD JOB!",4,8)
|
||||
wait=wait-1
|
||||
if wait==0 then
|
||||
level=level+1
|
||||
init_level()
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user