Files
pico8/trywater.p8
2025-07-25 11:46:41 +02:00

595 lines
25 KiB
Lua
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

pico-8 cartridge // http://www.pico-8.com
version 42
__lua__
function _init()
mymap={
w=36,
h=11,
}
coins_collected=false
chest_collected=false
initcoins()
initkey()
initchest()
initplayer()
initwater()
poke(0x5f5c,255) --disable btnp repeat
end
function _update()
updatewater()
updateplayer()
if btn() then
reload()
_init()
end
end
function _draw()
cls()
camera(player.x-20,-15)
map(0,0,0,0,mymap.w,mymap.h)
drawcoins()
drawkey()
drawchest()
drawwater()
drawplayer()
if coins_collected==true and chest_collected==true then
print("you win!\npress x to restart",player.x-5,9,3)
end
end
-->8
function initplayer()
player={
x=8,
y=mymap.h-8,
dir=false,
vv=0,
vh=2,
w=8,
h=8,
image=3,
timer=0,
walking=false,
jumping=false,
inwater=false
}
v0=-4.5
gravity=0.5
vf=-1
v1=-2
end
function updateplayer()
local dx=0
local dy=0
--walk
if btn() and player.x>=10 then
dx=-player.vh
player.dir=true
for i=1,player.vh do
local new_x,new_y,_,_=trymove(player.x,player.y,-1,0)
player.x=new_x
walking=true
end
elseif btn() and player.x<=mymap.w*8-player.w-10 then
dx=player.vh
player.dir=false
for i=1,player.vh do
local new_x,new_y,_,_=trymove(player.x,player.y,1,0)
player.x=new_x
walking=true
end
else walking=false
end--if
--jump & fall
player.inwater=isinwater(player.x+4,player.y+4)
if player.inwater==false then
if isonground() then
player.vv=0
if btnp() then
sfx(1)
player.vv=v0
walking=false
end
else
player.vv+=gravity
end
dy=player.vv --falling
if dy>0 then
dy=ceil(dy)
for i=1,dy do
local new_x,new_y,_,actual_dy=trymove(player.x,player.y,0,1)
player.y=new_y
if actual_dy==0 then
player.vv=0
break
end --if
end --for
elseif dy<0 then --jumping
dy=flr(dy)
for i=1,-dy do
local new_x,new_y,_,actual_dy=trymove(player.x,player.y,0,-1)
player.y=new_y
if actual_dy==0 then
player.vv=0
end --if
end --for
end
end
----in water
if player.inwater==true then
player.vv=vf
dy=player.vv
for i=1,-dy do
local new_x,new_y,_,actual_dy=trymove(player.x,player.y,0,-1)
player.y=new_y
if actual_dy==0 then
player.vv=0
end
end
end
if player.inwater==true or isinwater(player.x+4,player.y+12) then
if btnp() then --jump in water
sfx(1)
player.vv=v1
end
end
--image state
if isonground()==false then
walking=false
jumping=true
else jumping=false
end
--eat coins
if coins_collected==false then
eatcoin()
end
getkey()
if chest_collected==false then
openchest()
end
end
function drawplayer()
if player.inwater==true then
player.image=36
elseif walking==true then
player.timer+=1
if player.timer%6==0 then
sfx(2)
if player.image==3 then
player.image=19
else
player.image=3
end
end
elseif jumping==true then
player.image=35
else player.image=3
end
spr(player.image,player.x,player.y,1,1,player.dir)
end
------------
function issolid(x,y)
local tile=mget(flr(x/8),flr(y/8))
local f=fget(tile)
if f==1 then
return true
else return false
end--if
end
function trymove(x,y,dx,dy)
local new_x=x+dx
local new_y=y+dy
local collided_x=false
local collided_y=false
if dx>0 then --right
for i=0,player.h-1 do
if issolid(new_x+player.w-1,new_y+i) then
collided_x=true
break
end --if
end --for
end --if
if dx<0 then --left
for i=0,player.h-1 do
if issolid(new_x,new_y+i) then
collided_x=true
break
end --if
end --for
end --if
if dy>0 then --down
for i=0,player.w-1 do
if issolid(new_x+i,new_y+player.h-1) then
collided_y=true
break
end --if
end --for
end --if
if dy<0 then --up
for i=0,player.w-1 do
if issolid(new_x+i,new_y) then
collided_y=true
break
end --if
end --for
end --if
if new_y<0 then
dy=0
collided_y=true
end
if collided_x==true then
dx=0
end
if collided_y==true then
dy=0
end
return x+dx,y+dy,dx,dy
end
function isonground()
for i=0,player.w-1 do
if issolid(player.x+i,player.y+player.h) then
return true
end --if
end --for
return false
end
----------
function isinwater(x,y)
local x1=flr(x/8)
local y1=flr(y/8)
if water_state[x1][y1]==true then
return true
else return false
end--if
end
-->8
function initwater()
water_map={}
water_state={}
water_level=mymap.h-1
rising=true
water_timer=90
for x=0,mymap.w-1 do
water_map[x]={}
water_state[x]={}
for y=0,mymap.h-1 do
local tile=mget(x,y)
local f=fget(tile)
if f!=1 and f!=4 then
water_map[x][y]=true --not solid
else water_map[x][y]=false --ground
end--if
water_state[x][y]=false
end
end
end
function water_rise()
if water_level>0 then
water_level-=1
for i=0,mymap.w-1 do
if water_map[i][water_level]==true then
water_state[i][water_level]=true
end
end
end
end
function water_fall()
if water_level<mymap.h-1 then
for i=0,mymap.w-1 do
if water_map[i][water_level]==true then
water_state[i][water_level]=false
end
end
water_level+=1
end
end
function updatewater()
water_timer-=1
if water_timer==0 then
water_timer=90
if rising==true then
water_rise()
else water_fall()
end
end
---schedule
if water_level==3 and rising==true then
rising=false
elseif water_level==mymap.h-1 and rising==false then
rising=true
end
end
function drawwater()
for x=0,mymap.w-1 do
for y=0,mymap.h-1 do
if water_state[x][y]==true then
if y==water_level then
spr(20,x*8,y*8)
else
spr(4,x*8,y*8)
end
end
end
end
end
-->8
function initcoins()
score=0
totalscore=8
coins={}
for x=0,mymap.w do
for y=0,mymap.h do
local tile=mget(x,y)
if tile==5 then
add(coins,{
x=x*8,
y=y*8,
w=6,
collected=false,
timer=0
})
mset(x,y,2)
end--if
end
end
end
function eatcoin()
for c in all(coins) do
if c.collected==false and isoverlap(c.x,c.y,c.w)==true then
c.collected=true
sfx(0)
score+=1
c.timer=3
end--if
end--for
end
function isoverlap(x0,y0,w0)
local x=player.x
local y=player.y
if x+player.w<x0+(8-w0)/2 or x>x0+w0+(8-w0)/2 or y+player.h<y0+(8-w0)/2 or y>y0+w0+(8-w0)/2 then
return false
else return true
end
end
function drawcoins()
for c in all(coins) do
if c.collected==false then
spr(5,c.x,c.y)
elseif c.timer>0 then
spr(21,player.x+8,player.y)
spr(21,player.x-8,player.y,1,1,1)
c.timer-=1
end--if
end
print("score:"..score,player.x-5,1,4)
if score==totalscore then
coins_collected=true
end
end
------------
function initkey()
for x=0,mymap.w do
for y=0,mymap.h do
local tile=mget(x,y)
if tile==6 then
key={
x=x*8,
y=y*8,
w=6,
collected=false
}
mset(x,y,34)
break
end--if
end
end
end
function getkey()
if key.collected==false and isoverlap(key.x,key.y,key.w)==true then
key.collected=true
sfx(0)
end--if
end
function drawkey()
if key.collected==false then
spr(6,key.x,key.y)
elseif chest_collected==false then
print("key:z to use",player.x+30,1,4)
end
end
function initchest()
for x=0,mymap.w do
for y=0,mymap.h do
local tile=mget(x,y)
if tile==7 then
chest={
x=x*8,
y=y*8,
w=8,
collected=false,
timer=0
}
mset(x,y,2)
spr(7,key.x,key.y)
break
end--if
end
end
end
function openchest()
if btnp(🅾) and key.collected==true and chest.collected==false and isoverlap(chest.x,chest.y,chest.w)==true then
chest.collected=true
chest_collected=true
sfx(3)
chest.timer=15
end--if
end
function drawchest()
if chest.collected==false then
spr(7,chest.x,chest.y)
else
if chest.timer>0 then
chest.timer-=1
spr(8,chest.x,chest.y-12+chest.timer/3)
end
spr(23,chest.x,chest.y)
print("treasure found!",player.x+30,1,4)
end
end
__gfx__
0000000033b3b3b377777777002eeee0cccccccc00000000000000000c1111c00000000000000000000000000000000000000000000000000000000000000000
000000003b333b3377777f7722222eeec11cc11c009a9a0000000cc0c111111c0100001000000000000000000000000000000000000000000000000000000000
007007003333333377777777055797501cc11cc109a9a9a00000c880999889990100001000000000000000000000000000000000000000000000000000000000
00077000444444447f77777755919195cccccccc09aaaa90000c8800c118811c0000000000000000000000000000000000000000000000000000000000000000
00077000454445447777777705998990cccccccc09aaa9a00cc88000c118811c0eeeeee000000000000000000000000000000000000000000000000000000000
00700700444544447777777700e2e2e0c11cc11c099aaa900c080000c111111c00eeee0000000000000000000000000000000000000000000000000000000000
00000000444444547777f7770e02220e1cc11cc10099990008880000c111111c000ee00000000000000000000000000000000000000000000000000000000000
00000000454444447777777700020200cccccccc0000000000000000c111111c0000000000000000000000000000000000000000000000000000000000000000
000000004454444477777777002eeee000c100c10000000000000000c111111c0000000000000000000000000000000000000000000000000000000000000000
00000000544444547777777722222eeec11cc11c0009a000000000000c19a1c00000000000000000000000000000000000000000000000000000000000000000
000000004445444477777777055797501cc11cc100900000000000000c9e9ac00000000000000000000000000000000000000000000000000000000000000000
00000000444444447777777755919195cccccccc0000000000000000c9a9e9ac0000000000000000000000000000000000000000000000000000000000000000
0000000045444544b77777b705998990cccccccc00990a0000000000cccccccc0000000000000000000000000000000000000000000000000000000000000000
00000000444544447b777b7700e2e2e0c11cc11c0000000000000000c111111c0000000000000000000000000000000000000000000000000000000000000000
00000000444444547b77b777000e22e01cc11cc10090000000000000c111111c0000000000000000000000000000000000000000000000000000000000000000
000000004544444477b7b77700022000cccccccc0009a00000000000c111111c0000000000000000000000000000000000000000000000000000000000000000
000000000000000077777777002eeee0002eeee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000007777777722222eee22222eee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000007777c77705519150055191500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000007777777755979795559797950000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000007c7777770e99899e009989900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000007777777700e2e2e00ee2e2ee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000777777c700022200700222070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000007777777700200020007202700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__gff__
0001000002000000000000000000000000010000020000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
0202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020202020202020202020202050202020202020202020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020202020202020201010101010102020202020202020202020202020205020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020202020202020211222222221102020202020202020702020202020201010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020202020202010111062222221102020202020202010101020202020211111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020205120202020211111102020202020212020202020202020202020211111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020201010102020202020202020202020101010202020202020202020211111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020202020202021202020202020201011105020202020202020202020211111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0212120202020202010101020212010111110202020202021202121202020211111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0101010101010101111111010101111111111101010101010101010101010111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
000200001a0101d0201f0202002023020260202b02030020360103800037000360003400034000370003600036000340003400033000320003200022000220002200000000000000000000000000000000000000
0101000013050130501106013060180601a0601c0501d000287002870028700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000100000c0510c0062eb0630b0732b0736b0700000080000c0000e8061cb05303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000800001850015560175601856015560185601c5601f5601f560255002c5002a0002b0002a000290002800027000250002300021000210002100022000220000000000000000000000000000000000000000000
0010000000000000000000026050240502305022050200501d0501905015050120501105011050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0020000018a5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000