MAKE A ONE-BUTTON GAME USING PICO8 #3 – JUMPING(2)
We continue working from the previous post by adding new logic to jump between blocks. It will be a simple logic. When the player’s character jumps outside the current block. It will start looking for another block to set as the current block.
PREPARE VARIABLES TO SUPPORT MULTIPLE BLOCKS
We add a new variable to keep all created blocks. block_list = {}
ADD A NEW FUNCTION TO LOOKING FOR THE NEW CURRENT BLOCK
This function will start looking for the new current block when the player's character doesn't stand on the ground and stay outside the current block. function player_find_new_current_block() if player.ground == false and is_inside_block(player.x, player.y, current_block) == false then for b in all(block_list) do if is_inside_block(player.x, player.y, b) == true then current_block = b break end end end end
And add this function in update()
function _update60()
player_running()
player_jumping()
player_apply_gravity()
player_find_new_current_block()
end
ADD CODE TO DRAW ALL BLOCKS
We will modify the current draw() to support drawing all the blocks in the “block_list” variable.
function _draw()
cls(0)
print(“웃”, player.x-3, player.y – 5, 7)
for b in all(block_list) do
draw_block(b, 7)
draw_block(b, 7)
end
end
ADD MORE BLOCKS
Now we have prepared variables and functions enough for making the player's character jump between the blocks. The only thing we need to do is creating new block data.
function _init()
add(block_list, create_block(16,64,96,8))
add(block_list, create_block(16,56,96,8))
add(block_list, create_block(16,48,96,8))
current_block = block_list[1]
end
RESULT
SOURCE CODE
player = {x=64,y=64,di=1,jump_counter=0,ground=true} current_block = nil block_list = {} function create_block(x,y,w,h) return {x=x,y=y,w=w,h=h} end function draw_block(b,c) if (b == nil) return rect(b.x,b.y,b.x+b.w,b.y-b.h,c) end function is_inside_block(x,y,b) if (b == nil) return if x >= b.x and x < b.x + b.w then if y <= b.y and y > b.y - b.h then return true end end return false end function player_running() local next_px = player.x + player.di if is_inside_block(player.x, player.y, current_block) == true and is_inside_block(next_px, player.y, current_block) == false then player.di = player.di * -1 next_px = player.x + player.di end player.x = next_px end function player_jumping() if btnp(4) then if player.jump_counter == 0 and player.ground == true then player.jump_counter = 20 player.ground = false end end local next_py = player.y if player.jump_counter > 0 then player.jump_counter -= 1 if player.jump_counter > 10 then next_py = player.y - 1 else next_py = player.y end end player.y = next_py end function player_apply_gravity() local next_py = player.y if player.ground == false then if player.jump_counter <= 0 then next_py += 1 if is_inside_block(player.x, player.y, current_block) == true and is_inside_block(player.x, next_py, current_block) == false then next_py = player.y player.ground = true end end end player.y = next_py end function player_find_new_current_block() if player.ground == false and is_inside_block(player.x, player.y, current_block) == false then for b in all(block_list) do if is_inside_block(player.x, player.y, b) == true then current_block = b break end end end end function _init() add(block_list, create_block(16,64,96,8)) add(block_list, create_block(16,56,96,8)) add(block_list, create_block(16,48,96,8)) current_block = block_list[1] end function _update60() player_running() player_jumping() player_apply_gravity() player_find_new_current_block() end function _draw() cls(0) print("웃", player.x-3, player.y - 5, 7) for b in all(block_list) do draw_block(b, 7) end end