block puzzle explosion

This commit is contained in:
James Turk 2023-04-25 02:24:54 -05:00
parent 5ff050892a
commit 60d022c663
2 changed files with 85 additions and 34 deletions

View File

@ -9,7 +9,6 @@ function _init()
palt(11, true) palt(11, true)
palt(0, false) palt(0, false)
start_level(1) start_level(1)
last_gravity = t()
end end
function _update() function _update()
@ -138,38 +137,41 @@ end
function start_level(n) function start_level(n)
start_scene("level") start_scene("level")
last_gravity = t()
exploding = false
puzzle_grid = { puzzle_grid = {
{137, 0, 0, 0, 0, 0, 0, 137}, {137, 0, 0, 0, 0, 0, 0, 0},
{137, 137, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 137},
{137, 137, 137, 137, 137, 137, 137, 137}, {0, 0, 0, 0, 0, 0, 0, 137},
{137, 137, 137, 137, 137, 137, 137, 137}, {0, 0, 137, 0, 0, 137, 137, 137},
{137, 137, 137, 137, 137, 137, 137, 137},
{137, 137, 137, 137, 137, 137, 137, 137}, {137, 137, 137, 137, 137, 137, 137, 137},
} }
bomb_x = 4 bomb_x = 7
bomb_y = 4 bomb_y = 7
bomb_spr = make_sprite(bomb_x*8, bomb_y*8, "bomb_s", 0)
end end
function draw_level() function draw_level()
local xoff = 0
local yoff = 0
cls() cls()
map(0, 8, 0, 0, 16, 16) map(0, 8, 0, 0, 16, 16)
for y=1,GRID_H do for y=1,GRID_H do
for x=1,GRID_W do for x=1,GRID_W do
n = puzzle_grid[y][x] n = puzzle_grid[y][x]
if n > 0 then if n > 0 then
spr(n, xoff+x*8, yoff+y*8) spr(n, x*8, y*8)
end end
end end
end end
print(" lobby", 90, 6) print(" lobby", 90, 6)
spr(64, xoff+bomb_x*8, yoff+bomb_y*8) bomb_spr.x = bomb_x * 8
bomb_spr.y = bomb_y * 8
draw_sprite(bomb_spr)
end end
function can_move(dx, dy) function can_move(dx, dy)
@ -181,39 +183,84 @@ function can_move(dx, dy)
if puzzle_grid[ny][nx] ~= 0 then if puzzle_grid[ny][nx] ~= 0 then
return false return false
end end
printh(ny, nx)
return true return true
end end
function update_level() function update_level()
if btnp(⬅️) and can_move(-1, 0) then -- update grid
bomb_x -= 1 fall_speed = 0.3
if exploding then
fall_speed /= 2
if bomb_spr.speed == 0 and bomb_spr.anim then
bomb_spr.anim = nil
puzzle_grid[bomb_y][bomb_x] = GRID_EXPLODE
end end
if btnp(➡️) and can_move(1, 0) then
bomb_x += 1
end end
if btnp(⬇️) and can_move(0, 1) then if t() - last_gravity > fall_speed then
bomb_y += 1
end
if btnp(⬆️) and can_move(0, -1) then
bomb_y -= 1
end
-- gravity
if t() - last_gravity > 0.5 then
last_gravity = t() last_gravity = t()
for y=GRID_H-1,1,-1 do for y=GRID_H,1,-1 do
for x=1,GRID_W do for x=1,GRID_W do
if puzzle_grid[y+1][x] == 0 then -- explode
if puzzle_grid[y][x] == GRID_EXPLODE then
if y > 1 and puzzle_grid[y-1][x] == GRID_BOX then
puzzle_grid[y-1][x] = GRID_EXPLODE
end
if y < GRID_H and puzzle_grid[y+1][x] == GRID_BOX then
puzzle_grid[y+1][x] = GRID_EXPLODE
end
if x > 1 and puzzle_grid[y][x-1] == GRID_BOX then
puzzle_grid[y][x-1] = GRID_EXPLODE
end
if x < GRID_W and puzzle_grid[y][x+1] == GRID_BOX then
puzzle_grid[y][x+1] = GRID_EXPLODE
end
puzzle_grid[y][x] = 0
end
-- fall down
if y < GRID_H and puzzle_grid[y+1][x] == 0 then
puzzle_grid[y+1][x] = puzzle_grid[y][x] puzzle_grid[y+1][x] = puzzle_grid[y][x]
puzzle_grid[y][x] = 0 puzzle_grid[y][x] = 0
end end
end end
end end
if bomb_y < GRID_H and puzzle_grid[bomb_y+1][bomb_x] == 0 then end
if exploding then
update_sprite(bomb_spr)
-- exploding means we're done
return
end
-- movement
if btnp(⬅️) then
if can_move(-1, 0) then
bomb_x -= 1
elseif can_move(-1, -1) then
bomb_x -= 1
bomb_y -= 1
end
end
if btnp(➡️) then
if can_move(1, 0) then
bomb_x += 1
elseif can_move(1, -1) then
bomb_x += 1
bomb_y -= 1
end
end
if btnp(❎) then
explode()
end
-- fall immediately
while bomb_y < GRID_H and puzzle_grid[bomb_y+1][bomb_x] == 0 do
bomb_y += 1 bomb_y += 1
end end
end end
function explode()
exploding = true
explode_time = t()
bomb_spr.speed = 0.1
end end
-- globals -- globals
@ -227,7 +274,8 @@ _scenes.level = {start=nil, draw=draw_level, update=update_level}
GRID_W = 8 GRID_W = 8
GRID_H = 9 GRID_H = 9
GRID_CRATE = 137 GRID_BOX = 137
GRID_EXPLODE = 76
animations = { animations = {
boy_walk={frames={[0]=2, 0, 4, 0}, w=2, h=2}, boy_walk={frames={[0]=2, 0, 4, 0}, w=2, h=2},

View File

@ -20,7 +20,7 @@ function update_sprite(s)
if now - s.last_update > s.speed then if now - s.last_update > s.speed then
s.frame += 1 s.frame += 1
s.last_update = now s.last_update = now
if s.frame > #s.anim.frames then if s.frame + 1 > #s.anim.frames then
if s.loop then if s.loop then
s.frame = 0 s.frame = 0
else else
@ -28,10 +28,13 @@ function update_sprite(s)
s.frame = #s.anim.frames s.frame = #s.anim.frames
end end
end end
end end
end end
end end
function draw_sprite(s) function draw_sprite(s)
if s.anim then
spr(s.anim.frames[s.frame], s.x, s.y, s.anim.w, s.anim.h, s.flip_x, s.flip_y) spr(s.anim.frames[s.frame], s.x, s.y, s.anim.w, s.anim.h, s.flip_x, s.flip_y)
end
end end