Last active
March 24, 2019 20:29
-
-
Save tobiasvl/8f072bce8f67e410456eb8e3cee060d1 to your computer and use it in GitHub Desktop.
PICO-8 snippets
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
-- 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 |
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
--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 |
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
// 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) |
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
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" |
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
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