From 60d022c6632ee83f901ffd8422aa63df3331321f Mon Sep 17 00:00:00 2001 From: James Turk Date: Tue, 25 Apr 2023 02:24:54 -0500 Subject: [PATCH] block puzzle explosion --- games/boybomb.p8 | 112 +++++++++++++++++++++++++++++++++------------- games/sprites.lua | 7 ++- 2 files changed, 85 insertions(+), 34 deletions(-) diff --git a/games/boybomb.p8 b/games/boybomb.p8 index b8c514c..6ca7526 100644 --- a/games/boybomb.p8 +++ b/games/boybomb.p8 @@ -9,7 +9,6 @@ function _init() palt(11, true) palt(0, false) start_level(1) - last_gravity = t() end function _update() @@ -138,38 +137,41 @@ end function start_level(n) start_scene("level") + last_gravity = t() + exploding = false 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}, - {137, 137, 137, 137, 137, 137, 137, 137}, - {137, 137, 137, 137, 137, 137, 137, 137}, - {137, 137, 137, 137, 137, 137, 137, 137}, + {0, 0, 0, 0, 0, 0, 0, 137}, + {0, 0, 0, 0, 0, 0, 0, 137}, + {0, 0, 137, 0, 0, 137, 137, 137}, {137, 137, 137, 137, 137, 137, 137, 137}, } - bomb_x = 4 - bomb_y = 4 + bomb_x = 7 + bomb_y = 7 + bomb_spr = make_sprite(bomb_x*8, bomb_y*8, "bomb_s", 0) end function draw_level() - local xoff = 0 - local yoff = 0 cls() map(0, 8, 0, 0, 16, 16) for y=1,GRID_H do for x=1,GRID_W do n = puzzle_grid[y][x] if n > 0 then - spr(n, xoff+x*8, yoff+y*8) + spr(n, x*8, y*8) end end end 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 function can_move(dx, dy) @@ -181,39 +183,84 @@ function can_move(dx, dy) if puzzle_grid[ny][nx] ~= 0 then return false end - printh(ny, nx) return true end function update_level() - if btnp(⬅️) and can_move(-1, 0) then - bomb_x -= 1 + -- update grid + 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 - if btnp(⬇️) and can_move(0, 1) 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 + if t() - last_gravity > fall_speed then last_gravity = t() - for y=GRID_H-1,1,-1 do + for y=GRID_H,1,-1 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][x] = 0 end end end - if bomb_y < GRID_H and puzzle_grid[bomb_y+1][bomb_x] == 0 then - bomb_y += 1 + 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 + end +end + +function explode() + exploding = true + explode_time = t() + bomb_spr.speed = 0.1 end -- globals @@ -227,7 +274,8 @@ _scenes.level = {start=nil, draw=draw_level, update=update_level} GRID_W = 8 GRID_H = 9 -GRID_CRATE = 137 +GRID_BOX = 137 +GRID_EXPLODE = 76 animations = { boy_walk={frames={[0]=2, 0, 4, 0}, w=2, h=2}, diff --git a/games/sprites.lua b/games/sprites.lua index ad5a9df..cbf40e8 100644 --- a/games/sprites.lua +++ b/games/sprites.lua @@ -20,7 +20,7 @@ function update_sprite(s) if now - s.last_update > s.speed then s.frame += 1 s.last_update = now - if s.frame > #s.anim.frames then + if s.frame + 1 > #s.anim.frames then if s.loop then s.frame = 0 else @@ -28,10 +28,13 @@ function update_sprite(s) s.frame = #s.anim.frames end end + end end end function draw_sprite(s) - spr(s.anim.frames[s.frame], s.x, s.y, s.anim.w, s.anim.h, s.flip_x, s.flip_y) + 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) + end end \ No newline at end of file