Post by leon26leon26 on Oct 11, 2015 8:13:11 GMT
I made a simple game where you have to jump over spikes. It uses the draw library.
Now also for Windows and OSX! leon26leon26.itch.io/slime-bounce
Now also for Windows and OSX! leon26leon26.itch.io/slime-bounce
--[[-------------------------
Slime Bounce
Game made by leon26leon26
Version 1.3
Changelog:
If you make any changes to the code or have suggestions, please notify me of what you did, as I may incorporate it in the next update.
©leon26leon26
--]]-------------------------
--=========================--
-- Wrapping the draw lib --
--=========================--
--this converts coordinates based on your screen size
Draw={}
function Draw.rect(x1,y1,x2,y2,color)
x1=x1/320*xdim
y1=y1/568*ydim
x2=x2/320*xdim
y2=y2/568*ydim
draw.rect(x1,y1,x2,y2,color)
end
function Draw.fillrect(x1,y1,x2,y2,color)
x1=x1/320*xdim
y1=y1/568*ydim
x2=x2/320*xdim
y2=y2/568*ydim
draw.fillrect(x1,y1,x2,y2,color)
end
function Draw.roundedrect(x1,y1,x2,y2,round,color)
x1=x1/320*xdim
y1=y1/568*ydim
x2=x2/320*xdim
y2=y2/568*ydim
draw.rondedrect(x1,y1,x2,y2,round,color)
end
function Draw.fillroundedrect(x1,y1,x2,y2,round,color)
x1=x1/320*xdim
y1=y1/568*ydim
x2=x2/320*xdim
y2=y2/568*ydim
draw.fillroundedrect(x1,y1,x2,y2,round,color)
end
function Draw.circle(x1,y1,rad,color)
x1=x1/320*xdim
y1=y1/568*ydim
rad=rad/568*ydim
draw.circle(x1,y1,rad,color)
end
function Draw.fillcircle(x1,y1,rad,color)
x1=x1/320*xdim
y1=y1/568*ydim
rad=rad
draw.fillcircle(x1,y1,rad,color)
end
function Draw.triangle(x1,y1,x2,y2,x3,y3,color)
x1=x1/320*xdim
y1=y1/568*ydim
x2=x2/320*xdim
y2=y2/568*ydim
x3=x3/320*xdim
y3=y3/568*ydim
draw.triangle(x1,y1,x2,y2,x3,y3,color)
end
function Draw.filltriangle(x1,y1,x2,y2,x3,y3,color)
x1=x1/320*xdim
y1=y1/568*ydim
x2=x2/320*xdim
y2=y2/568*ydim
x3=x3/320*xdim
y3=y3/568*ydim
draw.filltriangle(x1,y1,x2,y2,x3,y3,color)
end
function Draw.string(string,x1,y1,color)
x1=x1/320*xdim
y1=y1/568*ydim
draw.string(string,x1,y1,color)
end
--=========================--
-- Functions --
--=========================--
function filecheck(name)
local f=io.open(name,"r")
if f~=nil then io.close(f)
return true
else
return false
end
end
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
--=========================--
-- Initializing --
--=========================--
function setvars()
--Variables below are safe to change.
speed=10
genmax=320
lvlup=20
sounds=true
debug=false
drawclouds=true
drawfps=true
levelup=true
--these are the paths for the soundfiles. change them to your liking, or don't.
sound={}
sound.jump="@resources/OwnSounds/Mario/jump.wav"
sound.click="@resources/OwnSounds/MGS/codec-beep.wav"
sound.loose="@resources/OwnSounds/Mario/die.wav"
sound.bg="@resources/"
end
function init()
math.randomseed(draw.gettime())
draw.setscreen(1)
draw.settitle("Slime Bounce!")
savedir="./sbsave.lua"
setvars()
--do not mess with these variables, they make the game run!
endframe=true
jump=false
floorcount=-1
version="V1.2.5"
gencount=0
color=1
frame=0
fpstime=sys.gettime()
fpscheck=0
active=true
spikesjumped=0
timestarted=sys.gettime()
cloudcount=0
jumpblock=0
jumpcount=1
level=1
ballpos=400
tilllvlup=lvlup
clouds={x={57,98,177,234,298,367,421,489,573,634,677,716,757,823,867,903,987,1057,1098,1177},y={}}
for i=1,17 do
table.insert(clouds.y,math.random(70,300))
end
for i=1,3 do
clouds.y[i+17]=clouds.y[i]
end
floor={{x1=360,x2=400,color={0,0.5,0,1}},{x1=320,x2=360,color={0,0.4,0,1}},{x1=280,x2=320,color={0,0.5,0,1}},{x1=240,x2=280,color={0,0.4,0,1}},{x1=200,x2=240,color={0,0.5,0,1}},{x1=160,x2=200,color={0,0.4,0,1}},{x1=120,x2=160,color={0,0.5,0,1}},{x1=80,x2=120,color={0,0.4,0,1}},{x1=40,x2=80,color={0,0.5,0,1}},{x1=0,x2=40,color={0,0.4,0,1}}}
spike={}
--coins={x={150},y={300}}
coins={x={},y={}}
balljump={401.5,399,396,393,389.5,384,374.5,358,345,335,327.5,321,313,306,297.5,287,278,269.5,262,256.5,251.5,247.5,243,239.5,235,231,228,225,223,221.5,220,219.5,219,218.5,218.5,218.5,218.5,219,221,223.5,227,231.5,237,242.5,248,254.5,261,268,275.5,283,291.5,298.5,306,315.5,324,332.5,340.5,347.5,355,361.5,367.5,372.5,377,381.5,385,389,392.5,395,397,400,401}
local function TB(x,y)
if screen==1 then
if jump==false and jumpblock==0 then
jump=true
if sounds then audio.playeffect(sound.jump) end
if debug then print(frame..": jumped")
end
end
elseif screen==0 then
if x>40/320*xdim and x<280/320*xdim and y>170/568*ydim and y<220/568*ydim then
init()
jumplock=10
screen=1
if sounds then audio.playeffect(sound.click) end
--elseif x>40/320*xdim and x<280/320*xdim and y>230/568*ydim and y<280/568*ydim then
--screen=4
end
elseif screen==4 then
screen=0
elseif screen==2 then
if x>40/320*xdim and x<280/320*xdim and y>415/568*ydim and y<455/568*ydim then
init()
screen=1
if sounds then audio.playeffect(sound.click) end
elseif x>40/320*xdim and x<280/320*xdim and y>460/568*ydim and y<500/568*ydim then
screen=0
if sounds then audio.playeffect(sound.click)
end
end
end
end
local function TM(x,y)
end
local function TE(x,y)
end
draw.tracktouches(TB,TM,TE)
end
--=========================--
-- Move everything --
--=========================--
function moveAll()
xdim,ydim=draw.getport()
for i=1,#floor do
f=floor[i]
floor[i].x1=f.x1-4
floor[i].x2=f.x2-4
end
for i=1,#spike do
local x=spike[i]
spike[i]=x-4
end
if jump==true then
ballpos=balljump[jumpcount]
jumpcount=jumpcount+1
if jumpcount==#balljump then
jump=false
jumpcount=1
end
end
if spike[1] and spike[1]<-50 then
table.remove(spike,1)
end
gencount=gencount+4
if gencount>genmax then
gencount=0
local x=math.random(4)
if x==1 then
table.insert(spike,600)
if debug and screen==1 then print(frame..": generated spike") end
elseif x==2 then
table.insert(spike,600)
table.insert(spike,640)
if debug and screen==1 then print(frame..": generated double spike") end
elseif x==3 then
table.insert(spike,600)
table.insert(spike,640)
table.insert(spike,680)
if debug and screen==1 then print(frame..": generated triple spike") end
elseif x==4 then
if debug and screen==1 then print(frame..": generated nothing") end
end
end
floorcount=floorcount+1
if floorcount==20 then
floorcount=0
for i=1,#floor do
floor[i].x1=floor[i].x1+80
floor[i].x2=floor[i].x2+80
end
end
if drawclouds then
cloudcount=cloudcount+0.1
if cloudcount==1000 then
for i=1,#clouds.x do
clouds.x[i]=clouds.x[i]+1000
end
end
for i=1,#clouds.x do
clouds.x[i]=clouds.x[i]-0.1
end
end
for i=1,#coins.x do
coins.x[i]=coins.x[i]-4
end
end
--=========================--
-- Draw everything --
--=========================--
function drawAll()
draw.beginframe()
Draw.fillrect(0,0,320,600,{0.54,0.824,1,1})
if drawclouds then
for i=1,#clouds.x do
local x=clouds.x[i]*3
if x<350 and x>-30 then
local y=clouds.y[i]
Draw.fillcircle(x,y,20,draw.white)
Draw.fillcircle(x-25,y-4,15,draw.white)
Draw.fillcircle(x+19,y-2,12,draw.white)
end
end
end
Draw.fillcircle(70,ballpos,40,draw.blue)
Draw.fillrect(0,0,320,62,draw.lightgray)
Draw.fillrect(0,17,320,19,draw.black)
Draw.fillrect(0,62,320,64,draw.black)
Draw.fillrect(0,460,320,570,draw.brown)
for i=1,#coins.x do
local x=coins.x[i]
local y=coins.y[i]
Draw.fillcircle(x,y,30,{0.81,0.7,0.32,1})
Draw.fillroundedrect(x-5,y-25,x+5,y+25,2,{0.61,0.50,0.12,1})
end
for i=1,#spike do
local x=spike[i]
Draw.filltriangle(x,420,x+40,420,x+20,360,draw.darkgray)
if debug then
Draw.rect(x+19,359,x+21,361,draw.yellow)
Draw.rect(x+9,389,x+11,391,draw.yellow)
end
end
Draw.fillrect(0,420,320,460,draw.white)
for i=1,#floor do
f=floor[i]
Draw.fillrect(f.x1,420,f.x2,460,f.color)
end
if debug then
Draw.rect(30,178,110,440,draw.green)
Draw.circle(70,ballpos,40,draw.yellow)
end
draw.setfont("Helvetica",10)
draw.string("©leon26leon26",1,ydim-11,draw.white)
draw.string(version,xdim-30,ydim-11,draw.white)
Draw.filltriangle(10,60,40,60,25,25,draw.darkgray)
draw.setfont("Helvetica",40)
draw.string(spikesjumped,50,20,draw.black)
draw.setfont("Helvetica",20)
draw.string("Level",139,20,draw.black)
local x,y=draw.stringsize(level)
Draw.string(level,48-(x/2)+139,41,draw.black)
if drawfps or debug then
draw.setfont("Helvetica",15)
Draw.string(fps,1,65,draw.yellow)
end
if endframe then draw.endframe() end
end
--=========================--
-- Check for collision --
--=========================--
function checkCollision()
if spike[1] then
for i=1,#spike do
if spike[i]<110 and spike[i]>30 then
local xd1=((70-spike[i]-20)*10)/10
local yd1=((ballpos-360)*10)/10
local xd2=((70-spike[i]-10)*10)/10
local yd2=((ballpos-390)*10)/10
if math.sqrt(xd1^2+yd1^2)<41 or math.sqrt(xd2^2+yd2^2)<41 then
if debug then
print(frame..": game over")
screen=3
else
screen=2
end
end
end
end
for i=1,#spike do
if spike[i]==40 then
spikesjumped=spikesjumped+1
tilllvlup=tilllvlup-1
end
if levelup then
if tilllvlup<1 then
speed=speed-1
if speed==0 then speed=1 end
genmax=genmax-4
level=level+1
tilllvlup=lvlup
end
end
end
end
end
--=========================--
-- AI --
--=========================--
function AI()
for i=1,#spike do
if spike[i]==140 then
jump=true
end
end
end
function game()
draw.clear(draw.white)
moveAll()
drawAll()
checkCollision(true)
if doAI then AI() end
draw.doevents()
end
function calcfps()
fpscheck=fpscheck+1
if os.difftime(os.time(), fpstime) > 1 then
fps = fpscheck
fpscheck = 0
fpstime = os.time()
end
end
function gameover()
timefinished=sys.gettime()
if sounds then audio.playeffect(sound.loose) end
while screen==2 do
frame=frame+1
calcfps()
endframe=false
draw.clear(draw.white)
drawAll()
Draw.fillrect(0,64,320,600,{0,0,0,0.4})
Draw.fillrect(30,70,290,510,draw.lightgray)
for i=1,2 do Draw.rect(30,70,290,510,draw.black) Draw.rect(29,69,291,511,draw.black) end
Draw.fillroundedrect(40,460,280,500,5,draw.darkgray)
Draw.fillroundedrect(40,415,280,455,5,draw.darkgray)
draw.setfont("Helvetica",34)
Draw.string("Menu",120,462,draw.black)
Draw.string("Try Again",90,413,draw.black)
draw.setfont("Helvetica",45)
Draw.string("Game Over",45,80,draw.red)
draw.setfont("Helvetica",20)
Draw.string("Spikes jumped over:\n "..spikesjumped,50,130,draw.black)
Draw.string("Time played:\n "..round(timefinished-timestarted,2).." seconds",50,170,draw.black)
draw.endframe()
draw.doevents()
end
endframe=true
jumpblock=10
end
--=========================--
-- Menu --
--=========================--
function menu()
init()
gencount=316
endframe=false
while screen==0 do
frame=frame+1
calcfps()
draw.beginframe()
draw.clear(draw.white)
moveAll()
drawAll()
draw.setfont("Helvetica",40)
Draw.string("Slime",70,70,draw.red)
Draw.string("Bounce",140,115,draw.red)
Draw.filltriangle(100,155,130,155,115,115,draw.darkgray)
Draw.fillcircle(70,135,20,draw.blue)
Draw.fillroundedrect(40,170,280,220,5,draw.gray)
Draw.string("Play",120,170,draw.black)
--Draw.fillroundedrect(40,230,280,280,5,draw.gray)
--Draw.string("Options",95,230,draw.black)
--Draw.fillroundedrect(40,290,280,340,5,draw.gray)
--Draw.string("Highscore",70,290,draw.black)
AI()
draw.endframe()
draw.doevents()
sleep(10)
end
endframe=true
end
--=========================--
-- Options --
--=========================--
function options()
while screen==4 do
draw.beginframe()
draw.clear(draw.white)
draw.doevents()
draw.endframe()
end
end
--=========================--
-- Highscore --
--=========================--
function highscore()
while screen==5 do
draw.beginframe()
draw.clear(draw.white)
draw.doevents()
draw.endframe()
end
end
--=========================--
-- Main Code --
--=========================--
-- This is the main code, everything is run from here
sys.clear()
init()
screen=0
if debug then
print(frame..": game started")
end
while active do
frame=frame+1
calcfps()
if jumpblock>0 then jumpblock=jumpblock-1 end
if screen==0 then
menu()
elseif screen==1 then
sleep(speed)
game()
elseif screen==2 then
gameover()
elseif screen==3 then
drawAll()
draw.waittouch()
screen=2
elseif screen==4 then
options()
elseif screen==5 then
highscore()
else
print("Error: Screen data invalid")
active=false
end
draw.endframe()
end