initial commit
This commit is contained in:
310
myfirstgame.p8
Normal file
310
myfirstgame.p8
Normal file
@@ -0,0 +1,310 @@
|
||||
pico-8 cartridge // http://www.pico-8.com
|
||||
version 42
|
||||
__lua__
|
||||
function _init()
|
||||
mymap={
|
||||
w=32,
|
||||
h=9
|
||||
}
|
||||
initcoins()
|
||||
initplayer()
|
||||
end
|
||||
|
||||
function _update()
|
||||
updateplayer()
|
||||
if btn(4) then
|
||||
reload()
|
||||
_init()
|
||||
end
|
||||
end
|
||||
|
||||
function _draw()
|
||||
cls()
|
||||
camera(player.x-20,-20)
|
||||
map(0,0,0,0,mymap.w,mymap.h)
|
||||
drawcoins()
|
||||
drawplayer()
|
||||
end
|
||||
-->8
|
||||
function initplayer()
|
||||
player={
|
||||
x=8,
|
||||
y=56,
|
||||
dir=false,
|
||||
vv=0,
|
||||
vh=2,
|
||||
w=8,
|
||||
h=8,
|
||||
image=3,
|
||||
timer=0,
|
||||
walking=false,
|
||||
jumping=false
|
||||
}
|
||||
v0=-4.5
|
||||
gravity=0.5
|
||||
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
|
||||
if isonground() then
|
||||
sfx(2)
|
||||
end
|
||||
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
|
||||
if isonground() then
|
||||
sfx(2)
|
||||
end
|
||||
walking=true
|
||||
end
|
||||
|
||||
else walking=false
|
||||
end--if
|
||||
|
||||
--jump & fall
|
||||
if isonground() then
|
||||
player.vv=0
|
||||
if btn(⬆️) then
|
||||
sfx(1)
|
||||
player.vv=v0
|
||||
walking=false
|
||||
end
|
||||
else
|
||||
player.vv+=gravity
|
||||
end
|
||||
|
||||
dy=player.vv
|
||||
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
|
||||
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
|
||||
|
||||
--image state
|
||||
if isonground()==false then
|
||||
walking=false
|
||||
jumping=true
|
||||
else jumping=false
|
||||
end
|
||||
|
||||
--eat coins
|
||||
eatcoin(player.x+3,player.y+4)
|
||||
|
||||
end
|
||||
|
||||
function drawplayer()
|
||||
if walking==true then
|
||||
player.timer+=1
|
||||
if player.timer%6==0 then
|
||||
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
|
||||
-->8
|
||||
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
|
||||
-->8
|
||||
function initcoins()
|
||||
score=0
|
||||
coins={}
|
||||
for x=0,mymap.w do
|
||||
for y=0,mymap.h do
|
||||
local tile=mget(x,y)
|
||||
if tile==4 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)
|
||||
end
|
||||
|
||||
__gfx__
|
||||
0000000033b3b3b377777777002eeee0777777770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
000000003b333b33777777c722222eee779a9a77009a9a0000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0070070033333333777777770557975079a9a9a709a9a9a000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00077000444444447c7777775591919579aaaa9709aaaa9000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0007700045444544777777770599899079aaa9a709aaa9a000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00700700444544447777777700e2e2e0799aaa97099aaa9000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
000000004444445477777c770e02220e779999770099990000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000454444447777777700020200777777770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
000000004454444477777777002eeee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000054444454777777c722222eee000000000009a00000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000444544447777777705579750000000000090000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000444444447c77777755919195000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000045444544b77777b7059989900000000000990a0000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000444544447b777b7700e2e2e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000444444547b77b777000e22e0000000000090000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
000000004544444477b7b77700022000000000000009a00000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
000000000000000000000000002eeee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000022222eee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000005519150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000055979795000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000e99899e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000e2e2e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000022200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000200020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
__gff__
|
||||
0001000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
__map__
|
||||
0202020202020202020204020202020202020202020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0202020202020202020202020202020202020202020202020204020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0202020402020202020202020202020202020202020402021202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0202020202020202010101020202020202020202020202010101020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0202020202120402020202020202020402020212020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0202020201010102020202020202020202020101010202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0202020202020202021202020202020201011104020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0212120202020202010101020212020111111102020202020202121202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
0101010101010101111111010101011111111101010101010101010101010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
__sfx__
|
||||
000300001a0101d0201f0302004023050260402b03030020360103800037000360003400034000370003600036000340003400033000320003200022000220002200000000000000000000000000000000000000
|
||||
000100000f0601106011070110701107013070140701506016060190001a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00010000210401b000130001300000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
Reference in New Issue
Block a user