Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created December 27, 2015 03:39
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 dermotbalson/11d1f005d0715200d3f8 to your computer and use it in GitHub Desktop.
Save dermotbalson/11d1f005d0715200d3f8 to your computer and use it in GitHub Desktop.
--# Main
-- StarArcade
displayMode(FULLSCREEN)
function setup()
ReadPictures()
StartMenu()
end
function StartMenu()
draw=DrawMenu
touched=TouchMenu
if score then wait=ElapsedTime+2 else wait=0 end
end
function DrawMenu()
background(0) --(132, 190, 204, 255)
sprite(screen,WIDTH/2,HEIGHT/2)
fill(255)
font("Copperplate")
fontSize(48)
textMode(CENTER)
text("Star Wars Arcade",WIDTH/2,HEIGHT*3/4)
font("Copperplate-Light")
fontSize(24)
text("Touch the characters to score\n\nFaster moving characters score more\n\nDo not miss Vader or the Sith",WIDTH/2,HEIGHT*.55)
fill(255,255,0)
text("Touch to start",WIDTH/2,HEIGHT/4)
highScore=readLocalData("HighScore") or 0
if score then
if score>highScore then
saveLocalData("HighScore",math.floor(score))
highScore=score
end
fill(255,0,0)
text("You scored "..math.floor(score).."\n\nYour high score is "..math.floor(highScore),WIDTH/2,HEIGHT/3)
end
end
function StartPlay()
MakeRows()
score=0
time=60
draw=DrawPlay
touched=TouchPlay
textMode(CORNER)
end
function DrawPlay()
background(0)
sprite(screen,WIDTH/2,HEIGHT/2)
for i=1,3 do
if ElapsedTime>rows[i].onScreen then
local x=rows[i].x
for j=1,rows[i].count do
local p=rows[i].pix[j]
if p>0 then
rows[i].ang[j]=rows[i].ang[j]+picSin
local s=math.sin(rows[i].ang[j])*rows[i].wobble*(1+0.5*rows[i].loop)
sprite(pix[p].img,x,rows[i].y+s,picSize)
rows[i].pos[j]=vec2(x,rows[i].y+s)
else
text(-p,x,rows[i].y)
end
x=x+picGap
if p==vader1 and ((rows[i].currSpeed>0 and x>0.7*WIDTH) or
(rows[i].currSpeed<0 and x<0.3*WIDTH)) then rows[i].pix[j]=vader2
end
end
if (rows[i].currSpeed>0 and rows[i].x>WIDTH) or (rows[i].currSpeed<0 and x<0) then
--check if we hit all the siths and vaders
for j=1,rows[i].count do
if siths[rows[i].pix[j]] then
StartMenu() --missed a sith or vader
return
end
end
MakeRow(i)
else
rows[i].x=rows[i].x+rows[i].currSpeed
end
end
end
time=time-DeltaTime
if time<0 then StartMenu() end
fill(255)
fontSize(28)
text("Time: "..math.floor(time),25,50)
text("Score: "..score,150,50)
end
function ReadPictures()
local pic=readImage("Dropbox:StarWars")
pix={
{name="Luke",pos=vec4(2,0,150,182),prob={5,2,2},score=1000},
{name="Leia",pos=vec4(163,1,189,175),prob={5,1,1},score=3000},
{name="Solo",pos=vec4(368,2,126,156),prob={10,3,3},score=500},
{name="C3PIO",pos=vec4(527,7,109,151),prob={15,5,5},score=100},
{name="R2D2",pos=vec4(675,11,115,164),prob={15,5,5},score=100},
{name="Worm",pos=vec4(820,4,144,163),prob={15,5,3},score=100},
{name="Vader1",pos=vec4(8,195,195,209),prob={0,0,2},score=5000},
{name="Chewie",pos=vec4(216,198,161,221),prob={10,3,5},score=500},
{name="JarJar",pos=vec4(389,195,209,218),prob={35,15,0},score=20},
{name="Wookie",pos=vec4(624,194,154,222),prob={20,8,1},score=80},
{name="Hoodie",pos=vec4(824,201,127,225),prob={20,8,0},score=800},
{name="Yoda",pos=vec4(12,440,218,153),prob={0,1,2},score=2000},
{name="Sith",pos=vec4(243,432,125,164),prob={0,5,3},score=2000},
{name="Jabba",pos=vec4(408,441,196,148),prob={5,2,4},score=1000},
{name="Storm",pos=vec4(660,437,150,166),prob={50,25,0},score=30},
{name="Vader2",pos=vec4(832,433,124,163),prob={0,0,0},score=10000}
}
local h=pic.height
for i=1,#pix do
pix[i].pos.y=h-pix[i].pos.y-pix[i].pos.w
pix[i].img=pic:copy(pix[i].pos:unpack())
end
vader1,vader2=7,16
siths={}
siths[7],siths[13],siths[16]=1,1,1
screen=image(WIDTH,HEIGHT)
setContext(screen)
fill(255)
for i=1,1000 do
ellipse(math.random(0,WIDTH),math.random(0,HEIGHT),math.random(1,5))
end
setContext()
end
function MakeRows()
picSize=75
picGap=125
picSin=0.02
rows={}
rows[1]={speed=3,count=15,score=1,delay=2,y=150,wobble=5,loop=1}
rows[2]={speed=6,count=12,score=2,delay=6,y=400,wobble=10,loop=1}
rows[3]={speed=9,count=6,score=5,delay=15,y=650,wobble=20,loop=1}
--create probability tables for each layer
for i=1,3 do
rows[i].prob={}
for j=1,#pix do
for k=1,pix[j].prob[i] do
rows[i].prob[#rows[i].prob+1]=j
end
end
end
--create first set of rows
for i=1,3 do MakeRow(i) end
end
function MakeRow(t)
local p,a={},{}
for i=1,rows[t].count do
j=math.random(1,#rows[t].prob)
p[i]=rows[t].prob[j]
a[i]=math.random()*math.pi
end
rows[t].pix=p
rows[t].ang=a
rows[t].pos={}
rows[t].onScreen=ElapsedTime+rows[t].delay
if math.random()>0.5 then
rows[t].x=-picGap*rows[t].count
rows[t].currSpeed=rows[t].speed
else
rows[t].x=WIDTH+picGap
rows[t].currSpeed=-rows[t].speed
end
rows[t].loop=rows[t].loop+1
end
function TouchMenu(t)
if t.state==ENDED and ElapsedTime>wait then StartPlay() end
end
function TouchPlay(t)
if t.state==ENDED then
for i=1,3 do
if math.abs(t.y-rows[i].y)<picSize then
for j=1,rows[i].count do
if rows[i].pix[j]>0 then
if math.abs(t.x-rows[i].pos[j].x)<picSize/2
and math.abs(t.y-rows[i].pos[j].y)<picSize/2 then
local k=rows[i].pix[j]
local s=pix[k].score*rows[i].score
score=score+s
rows[i].pix[j]=-s
break
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment