Skip to content

Instantly share code, notes, and snippets.

@tobiasvl
Last active March 24, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasvl/8f072bce8f67e410456eb8e3cee060d1 to your computer and use it in GitHub Desktop.
Save tobiasvl/8f072bce8f67e410456eb8e3cee060d1 to your computer and use it in GitHub Desktop.
PICO-8 snippets
-- four directions, 16 tokens
for i=0,1,0.25 do
local x,y=cos(i),sin(i)
end
-- eight directions, 24 tokens
for i=0.125,1,0.125 do
local x,y=flr(cos(i)+.5),flr(sin(i)+.5)
end
--random int between 0,h
function rand(h) --exclusive
return flr(rnd(h))
end
function randi(h) --inclusive
return flr(rnd(h+1))
end
--random int between l,h
function randb(l,h) --exclusive
return flr(rnd(h-l))+l
end
function randbi(l,h) --inclusive
return flr(rnd(h+1-l))+l
end
// before any srand() calls have happened
oldSeed=rnd(-1)
// begin deterministic stuff
srand(1)
for i=1,100 do
somethingWhichUsesRnd()
end
// reset seed to something unpredictable
srand(oldSeed)
t={"apple","orange","line"}
a="1"
t.__index=function(self,key)
local k=tonum(key)
if k>0 and k<=#self then -- this is needed since we don't have rawget()
return self[k]
else
return nil
end
setmetatable(t,t)
printh(t[a]) -- good, prints "apple"
function try(t,c,f)
local co=cocreate(t)
local s,m=true
while s and costatus(co)!="dead" do
s,m=coresume(co)
if not s then
c(m)
end
end
if f then
f()
end
end
cls()
try(function()
print("hi ")
--print("hi " .. nil)
end,
function(e)
print("an error occurred:\n"..e)
end,
function()
print("finally")
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment