Created
March 4, 2019 15:43
-
-
Save morningtoast/3c01e7801af301e96536093a932e0b99 to your computer and use it in GitHub Desktop.
Snake taile for PICO-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pico-8 cartridge // http://www.pico-8.com | |
version 16 | |
__lua__ | |
-- scratch | |
function newall() | |
local obj={ | |
x=120,y=120,tail={} | |
} | |
obj.aim=get_angle(120,120, mouse_x,mouse_y) | |
obj.dx,obj.dy=dir_calc(obj.aim,2) | |
add(all,obj) | |
end | |
function _init() | |
mouse.init() | |
all={} | |
--tail={} | |
--x,y=120,120 | |
--aim=.375 | |
--dx,dy=dir_calc(aim,1.5) | |
end | |
hold=0 | |
function _update60() | |
mouse_x,mouse_y = mouse.pos() | |
mouse_btn = mouse.button() | |
for _,o in pairs(all) do | |
o.x+=o.dx | |
o.y+=o.dy | |
if #o.tail>=3 then | |
del(o.tail,o.tail[1]) | |
end | |
add(o.tail,{x=o.x,y=o.y}) | |
end | |
if mouse_btn==1 then | |
if hold==0 then | |
newall() | |
end | |
hold+=1 | |
if hold>5 then hold=0 end | |
end | |
if mouse_btn<1 then hold=0 end | |
--hold=0 | |
end | |
function _draw() | |
cls() | |
rectfill(0,0,127,127,1) | |
circfill(120,120, 4,6) | |
for _,v in pairs(all) do | |
for _,t in pairs(v.tail) do | |
--circfill(t.x,t.y, 1,10) | |
spr(1,t.x,t.y) | |
end | |
--circfill(v.x,v.y, 1,9) | |
end | |
pset(mouse_x,mouse_y,7) | |
end | |
function dir_calc(angle,speed) | |
local dx=cos(angle)*speed | |
local dy=sin(angle)*speed | |
return dx,dy | |
end | |
function get_angle(fromx,fromy, tox,toy) | |
return atan2(tox-fromx, toy-fromy) -- to-from | |
end | |
mouse = { | |
init = function() | |
poke(0x5f2d, 1) | |
end, | |
-- return int:x, int:y, onscreen:bool | |
pos = function() | |
local x,y = stat(32)-1,stat(33)-1 | |
return stat(32)-1,stat(33)-1 | |
end, | |
-- return int:button [0..4] | |
-- 0 .. no button | |
-- 1 .. left | |
-- 2 .. right | |
-- 4 .. middle | |
button = function() | |
return stat(34) | |
end, | |
} | |
__gfx__ | |
0000000007aa00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
0000000077aaa0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
007007007aa990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
000770000aa900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment