Skip to content

Instantly share code, notes, and snippets.

@monkeyman32123
Last active August 29, 2015 13:57
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 monkeyman32123/6f52c4cfbe555397d252 to your computer and use it in GitHub Desktop.
Save monkeyman32123/6f52c4cfbe555397d252 to your computer and use it in GitHub Desktop.
Codea gyroship file
function setup()
youdead = false
elapsed = 0
stars = true
MENU = 1
GAME = 2
GAME_OVER = 3
screen = MENU
stroke(255, 0, 0, 139)
smooth()
lineCapMode(ROUND)
displayMode(FULLSCREEN_NO_BUTTONS)
supportedOrientations(LANDSCAPE_RIGHT)
starangle = math.random()*359
lasso, originid = nil, nil
lassocol = color(0, 255, 255, 255)
simpveloc=4
shipVelocity = vec2(simpveloc,0)
shiploc = vec2(768/2, HEIGHT/2)
anglerad = math.random(1,4)*(math.pi/2)
dots = {}
numdots = math.deg(math.pi*.5)
shipsize = 30
dotang=0
lowlim = 30
upperlim = 120
b=0
dotfx = true
--768 width
--1024 height
--256 difference
drawguides = false
guidevec= vec2(0,0)
strokecolour = color(0, 255, 0, 255)
colour = color(0, 0, 0, 255)
buttons = {}
buttons[0] = Button(false,256,768/5,768,0,"Turn Around",strokecolour,colour, strokecolour, 3, true, 2, false, false, false, false,35)
buttons[1] = Button(false,256,768/5,768,768/5-1,"Break Lasso",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,35)
buttons[2] = Button(true,256/5,768/5,768,(768/5)*2-3,"2",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[3] = Button(true,256/5,768/5,768+((256/5)*1),(768/5)*2-3,"3",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[4] = Button(true,256/5,768/5,768+((256/5)*2),(768/5)*2-3,"4",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[5] = Button(true,256/5,768/5,768+((256/5)*3),(768/5)*2-3,"5",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[6] = Button(true,256/5,768/5,768+((256/5)*4),(768/5)*2-3,"6",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[7] =Button(false,256,256/5,768,(768/5)*2.5-30,"Velocity",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, true,35)
--Button:init(circ, width, height, xloc, yloc, namestring, namecolour, colour, strokecolour, strokewidth, timedbool, timelen, togglebool, defaultinvis, defaultoff, istag)
menubuttons = {}
menubuttons[0] = Button(false,512,768/5,(WIDTH/2)-512/2,HEIGHT/2,"BEGIN",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,150)
buttons[4].on = false
lasers = {}
lastelapsed=0
menfont= 0
end
function draw()
background(0,0,0,255)
if screen == MENU then
fill(0,255,0,255)
textMode(CENTER)
fontSize(180)
font("Futura-Medium")
text("GYROSHIP", WIDTH/2-math.cos(ElapsedTime*5), HEIGHT-90+math.sin(ElapsedTime*5))
menubuttons[0]:draw()
--fill(0,0,255,255)
--rect(WIDTH/2, HEIGHT-200, 100,100)
elseif screen == GAME then
if stars then
pushMatrix()
translate(768/2,768/2)
spriteMode(CENTER)
rotate(starangle)
sprite("Documents:stars4", 0,0, 1300)
--sprite("Documents:earth", 0,0, 768)
rectMode(CENTER)
fill(0,0,0,90)
rect(768,768,0,0)
popMatrix()
end
--font("Copperplate-Bold")
font("Copperplate-Bold")
rectMode(CORNER)
fontSize(20)
ellipseMode(CENTER)
calcshiploc()
processLasers()
fxdraw()
drawship()
for i=0, #buttons do
buttons[i]:draw()
end
end
end
function drawship()
pushMatrix()
translate(shiploc.x,shiploc.y)
rotate(math.deg(anglerad)-90)
sprite("Space Art:Red Ship", 0,0, shipsize)
popMatrix()
strokeWidth(3)
stroke(0,255,0,255)
fill(0, 0, 0, 255)
rect(768,0-1,256,HEIGHT+1.5)
end
function fxdraw()
if drawguides then
dist = guidevec:dist(shiploc)
if dist >lowlim and dist <upperlim then
strokeWidth(1)
stroke(0, 255, 0, 255)
line(guidevec.x-40, guidevec.y, guidevec.x+40, guidevec.y)
line(guidevec.x, guidevec.y-40, guidevec.x, guidevec.y+40)
fill(0,0,0,0)
ellipse(guidevec.x, guidevec.y, 60)
ellipse(guidevec.x, guidevec.y, dist*2)
end
end
if lasso ~= nil then
stroke(lassocol)
strokeWidth(3)
line(lasso.x,lasso.y, shiploc.x,shiploc.y)
if b<lassolength then
calcdots()
end
strokeWidth(10)
pushMatrix()
translate(lasso.x, lasso.y)
for i=1, #dots do
if i%2 == 0 then
rotate(dotang)
ellipse(dots[i].x,dots[i].y, 5)
rotate(-dotang)
else
rotate(-dotang)
ellipse(dots[i].x,dots[i].y, 5)
rotate(dotang)
end
end
dotang=dotang+.05
popMatrix()
end
if drawguides==true or lasso == nil then
strokeWidth(1)
stroke(0, 255, 0, 255)
fill(0, 0, 0, 0)
ellipse(shiploc.x,shiploc.y,lowlim*2)
ellipse(shiploc.x,shiploc.y,upperlim*2)
end
fill(255,255,255,255)
text(math.floor(1/DeltaTime), WIDTH/2-15, HEIGHT-30)
text(math.floor(ElapsedTime-elapsed), WIDTH/2-15, HEIGHT-50)
end
function touched(touch)
if screen == MENU then
if menubuttons[0]:ispressed(touch) then
menubuttons[0]:callafterpressed()
screen = GAME
elapsed = ElapsedTime
lastelapsed = 0
end
end
if screen == GAME then
if touch.state == BEGAN and originid == nil and touch.x < 768 then
drawguides = true
originid = touch.id
elseif touch.state == ENDED and touch.id == originid and (vec2(touch.x,touch.y):dist(shiploc) < upperlim and vec2(touch.x,touch.y):dist(shiploc) >lowlim) then
b=0
local t = (vec2(touch.x,touch.y))
dots = nil
dots = {}
drawguides = false
initlasso(t)
originid = nil
elseif touch.state == ENDED and touch.id == originid then
drawguides = false
originid = nil
end
for i=0,#buttons do
if buttons[i]:ispressed(touch) then
print(i)
buttons[i]:callafterpressed()
processbuttonpress(i)
end
end
if touch ~= nil and touch.id == originid then
guidevec= vec2(touch.x,touch.y)
end
end
end
function initlasso(t)
lassolength = vec2(t.x,t.y):dist(shiploc)
if lassolength < lowlim or lassolength > upperlim then
lasso = nil
originid = nil
return
end
lasso = vec2(t.x,t.y)
local circumf = 2*math.pi*lassolength
angleaddrad = 2*math.pi*simpveloc/circumf
local offset = lasso - shiploc
local anglebet = math.atan2(offset.y,offset.x)
local tangent = anglebet - (math.pi/2)
if (anglerad - anglebet)%(2*math.pi) > math.pi then
anglerad = tangent
else
anglerad = tangent + math.pi
angleaddrad = -angleaddrad
end
end
function calcshiploc()
if lasso ~= nil then
anglerad = anglerad+ angleaddrad
end
if lasso ~= nil then
shiploc = lasso + (shiploc - lasso):rotate(angleaddrad)
else
shiploc = shipVelocity:rotate(anglerad) + shiploc
end
if shiploc.x < 0+10 or shiploc.x > 768-10 or shiploc.y < 0+10 or shiploc.y > 768-10 then
turnaround()
end
end
function calcdots()
if dotfx == true then
b=b+lassolength/math.sqrt(lassolength*4)
if b>lassolength then
b= lassolength
end
else
b=lassolength
end
for i=0, numdots do
if i%2 == 0 then
b=b+3
dots[i] = vec2((b*math.cos(math.rad(i*(360/numdots)))),(b*math.sin(math.rad(i*(360/numdots)))))
b=b-3
else
dots[i] = vec2((b*math.cos(math.rad(i*(360/numdots)))),(b*math.sin(math.rad(i*(360/numdots)))))
end
end
end
function turnaround()
anglerad = anglerad + math.pi
if anglerad > 2*math.pi then
anglerad = anglerad-(2*math.pi)
end
if lasso~=nil then
angleaddrad = -angleaddrad
end
end
function breaklasso()
lasso = nil
originid = nil
dots = nil
dots = {}
end
function changeveloc(newVeloc)
simpveloc = newVeloc
shipVelocity = vec2(simpveloc, 0)
if lasso ~= nil then
initlasso(lasso)
end
end
function altlasso(indivoffsets,fromlasso,xoffset,yoffset,dist,angle)
if indivoffsets then
if fromlasso then
t = vec2(lasso.x+xoffset,lasso.y+yoffset)
else
t = vec2(shiploc.x+xoffset, shiploc.y+yoffset)
end
b=0
dots = nil
dots = {}
initlasso(t)
else
t= vec2(0,0)
if fromlasso then
t.x = lasso.x + (math.cos(math.rad(angle))*dist)
t.y = lasso.y + (math.sin(math.rad(angle))*dist)
else
t.x = shiploc.x + (math.cos(math.rad(angle))*dist)
t.y = shiploc.y + (math.sin(math.rad(angle))*dist)
end
b=0
dots = nil
dots = {}
initlasso(t)
end
end
function processbuttonpress(i)
if i == 0 then
turnaround()
elseif i==1 then
breaklasso()
end
if i>=2 and i <= 6 then
changeveloc(tonumber(buttons[i].name))
for b = 2, 6 do
if b ~= i then
buttons[b].on = true
elseif b == i then
buttons[b].on = false
end
end
end
end
function processLasers()
local gorp = (ElapsedTime-elapsed)/500
if gorp > .5 then
gorp= .5
end
if ElapsedTime-elapsed > lastelapsed+1.25-gorp then
--if math.random(0,100) > 99-ElapsedTime/20 then
local z = 3-(gorp*3)
local g = .4+gorp
if math.random(0,1) == 1 then
if lasers[0] == nil then
lasers[0] = Laser(0,0,0,0, true, true, false,z,g, ElapsedTime-elapsed)
else
lasers[#lasers+1] = Laser(0,0,0,0, true, true, false, z,g, ElapsedTime-elapsed)
end
--Laser:init(x1,y1,x2,y2, rand, vert, horiz, t, t2, e)
else
if lasers[0] == nil then
lasers[0] = Laser(0,0,0,0, true, false, true, z,g, ElapsedTime-elapsed)
else
lasers[#lasers+1] = Laser(0,0,0,0, true, false, true,z,g, ElapsedTime-elapsed)
end
end
lastelapsed = ElapsedTime-elapsed
end
if lasers[0] ~= nil then
for i=0, #lasers do
lasers[i]:draw()
end
end
if lasers[0] ~= nil then
for i=0, #lasers do
if lasers[i].alive == true and lasers[i]:getDistTo(shiploc) <15 then
--screen = GAME_OVER
youdead = true
end
end
if youdead then
resetdastuff()
end
end
end
function resetdastuff()
youdead = false
elapsed = 0
stars = true
MENU = 1
GAME = 2
GAME_OVER = 3
screen = MENU
stroke(255, 0, 0, 139)
smooth()
lineCapMode(ROUND)
displayMode(FULLSCREEN_NO_BUTTONS)
supportedOrientations(LANDSCAPE_RIGHT)
starangle = math.random()*359
lasso, originid = nil, nil
lassocol = color(0, 255, 255, 255)
simpveloc=4
shipVelocity = vec2(simpveloc,0)
shiploc = vec2(768/2, HEIGHT/2)
anglerad = math.random(1,4)*(math.pi/2)
dots = {}
numdots = math.deg(math.pi*.5)
shipsize = 30
dotang=0
lowlim = 30
upperlim = 120
b=0
dotfx = true
--768 width
--1024 height
--256 difference
drawguides = false
guidevec= vec2(0,0)
strokecolour = color(0, 255, 0, 255)
colour = color(0, 0, 0, 255)
buttons = nil
buttons = {}
buttons[0] = Button(false,256,768/5,768,0,"Turn Around",strokecolour,colour, strokecolour, 3, true, 2, false, false, false, false,35)
buttons[1] = Button(false,256,768/5,768,768/5-1,"Break Lasso",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,35)
buttons[2] = Button(true,256/5,768/5,768,(768/5)*2-3,".1",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[3] = Button(true,256/5,768/5,768+((256/5)*1),(768/5)*2-3,"3",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[4] = Button(true,256/5,768/5,768+((256/5)*2),(768/5)*2-3,"4",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[5] = Button(true,256/5,768/5,768+((256/5)*3),(768/5)*2-3,"5",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[6] = Button(true,256/5,768/5,768+((256/5)*4),(768/5)*2-3,"6",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[7] =Button(false,256,256/5,768,(768/5)*2.5-30,"Velocity",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, true,35)
--Button:init(circ, width, height, xloc, yloc, namestring, namecolour, colour, strokecolour, strokewidth, timedbool, timelen, togglebool, defaultinvis, defaultoff, istag)
menubuttons = {}
menubuttons[0] = Button(false,512,768/5,(WIDTH/2)-512/2,HEIGHT/2,"BEGIN",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,150)
buttons[4].on = false
lasers = nil
lasers = {}
menfont= 0
end
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
Button = class()
function Button:init(circ, width, height, xloc, yloc, namestring, namecolour, colour, strokecolour, strokewidth, timedbool, timelen, togglebool, defaultinvis, defaultoff, istag, fontsize)
self.circle = circ
if circ == true then
self.dia = width
self.rad = width/2
else
self.wid = width
self.hei = height
end
self.x = xloc
self.y = yloc
self.loc = vec2(self.x,self.y)
self.name = namestring
self.namecol = namecolour
self.col = colour
self.strokewid = strokewidth
self.strokecol = strokecolour
self.timed = timedbool
if self.timed then
self.time = timelen
end
self.toggle = togglebool
if defaultoff then
self.on = false
else
self.on = true
end
if defaultinvis then
self.visible = false
else
self.visible = true
end
self.timer = false
self.elapsed = 0
self.pressed = false
self.originid = nil
self.halfpressed = false
self.pressable = true
self.tag = istag
self.fsize = fontsize
end
function Button:draw()
fontSize(self.fsize)
textMode(CENTER)
ellipseMode(CORNER)
if self.visible then
fill(self.col)
strokeWidth(self.strokewid)
stroke(self.strokecol)
if self.circle then
ellipse(self.x,self.y,self.dia)
fill(self.namecol)
text(self.name, self.x+self.rad,self.y+self.rad)
else
rect(self.x,self.y,self.wid,self.hei)
fill(self.namecol)
text(self.name, self.x+(self.wid/2), self.y+(self.hei/2))
end
end
if self.halfpressed and self.on and self.visible then
fill(0, 0, 0, 120)
strokeWidth(0)
if self.circle then
ellipse(self.x,self.y,self.dia)
else
rect(self.x,self.y,self.wid,self.hei)
end
end
if not self.on or not self.pressable and self.visible then
fill(0, 0, 0, 120)
strokeWidth(0)
if self.circle then
ellipse(self.x,self.y,self.dia)
else
rect(self.x,self.y,self.wid,self.hei)
end
end
if self.timer then
self:processTimer()
lineCapMode(PROJECT)
local vlorp = WIDTH-(((ElapsedTime-self.elapsed)/self.time)*self.wid)
stroke(255,0,0,255)
strokeWidth(5)
if vlorp > self.x+self.wid-5 then
vlorp= self.x+self.wid-5
elseif vlorp < self.x+5 then
vlorp = self.x+5
end
if self.timer then
line(self.x+5, self.y+5,vlorp,self.y+5)
end
lineCapMode(ROUND)
end
end
function Button:ispressed(touch)
if not self.tag then
if self.visible and self.pressable then
t = vec2(touch.x, touch.y)
if touch.state == BEGAN and self.originid == nil then
if self.circle then
if (self.loc + vec2(self.rad,self.rad)):dist(t) < self.rad then
self.originid = touch.id
self.halfpressed = true
end
else
if t.x > self.x and t.x < self.x+self.wid and t.y > self.y and t.y < self.y+self.hei then
self.originid = touch.id
self.halfpressed = true
end
end
elseif touch.state == ENDED and touch.id == self.originid then
if self.circle then
if (self.loc + vec2(self.rad,self.rad)):dist(t) < self.rad then
self.pressed = true
self.halfpressed = false
end
else
if t.x > self.x and t.x < self.x+self.wid and t.y > self.y and t.y < self.y+self.hei then
self.pressed = true
self.halfpressed = false
end
end
self.halfpressed = false
self.originid = nil
else return end
if self.pressed then
self:processBehaviour()
end
end
return self.pressed
end
end
function Button:processTimer()
if ElapsedTime - self.elapsed > self.time then
self.timer = false
if not self.toggle then
self.on = true
elseif self.toggle then
self.pressable = true
end
end
end
function Button:togglevisible()
end
function Button:processBehaviour()
if self.toggle and self.pressable then
if not self.on then
self.on = true
elseif self.on then
self.on = false
end
end
if self.timed and self.toggle and self.pressable then
self.elapsed = ElapsedTime
self.pressable = false
self.timer = true
end
if self.timed and not self.toggle and self.on then
self.elapsed = ElapsedTime
self.on = false
self.timer = true
elseif not self.toggle and not self.on then
self.pressed = false
end
end
function Button:callafterpressed()
self.pressed = false
end
Laser = class()
function Laser:init(x1,y1,x2,y2, rand, vert, horiz, t, t2, e)
self.alive = true
self.ON = 1
self.OFF = 2
self.time = t
self.lifespan = t2+t
self.timer = self.ON
self.elapsed = e
self.horiz = horiz
self.vert = vert
if not rand and not vert and not horiz then
self.x1=x1
self.x2=x2
self.y1=y1
self.y2=y2
elseif rand and not vert and not horiz then
elseif rand and vert and not horiz then
self.x1= math.random(shiploc.x-250,shiploc.x+250)
--self.x1= math.random(0,768)
self.x2= self.x1
self.y1= -60
self.y2= 768+60
elseif rand and not vert and horiz then
self.x1= -60
self.x2= 768+60
self.y1=math.random(shiploc.y-250,shiploc.y+250)
--self.y1=math.random(0,768)
self.y2=self.y1
elseif not rand and vert and not horiz then
self.x1=x1
self.x2=x1
self.y1=-60
self.y2=769+60
elseif not rand and not vert and horiz then
self.x1=-60
self.x2=768+60
self.y1=y1
self.y2=y1
elseif not rand and vert and horiz then
self.x1=x1
self.x2=x2
self.y1=y1
self.y2=y2
elseif rand and vert and horiz then
self.x1=x1
self.x2=x2
self.y1=y1
self.y2=y2
end
end
function Laser:draw()
strokeWidth(3* (ElapsedTime-elapsed-self.elapsed))
stroke(255, 0, 0, 136)
if ElapsedTime-elapsed-self.elapsed < self.lifespan then
if self.timer == self.ON then
local mult = (1/(ElapsedTime-elapsed-self.elapsed))-.1
if mult >1 then
mult = 1
end
if self.vert then
local h = 50*math.sin(math.rad(60))*mult-10
line(self.x1-25,768-5,self.x1+25, 768-5)
line(self.x1-25,768-5,self.x1, 768-h-5)
line(self.x1+25,768-5,self.x1, 768-h-5)
elseif self.horiz then
local h = 50*math.sin(math.rad(60))*mult-10
line(0+5, self.y1+25, 0+5, self.y1-25)
line(0+5, self.y1+25, 0+5+h, self.y1)
line(0+5, self.y1-25, 0+5+h, self.y1)
end
self:processtimer()
elseif self.timer == self.OFF then
strokeWidth(3)
stroke(255,0,0,255)
line(self.x1,self.y1,self.x2,self.y2)
end
self.alive= true
else
self.alive= false
end
end
function Laser:getDistTo(coords)
if self.timer == self.OFF then
local m= (self.y2-self.y1)/(self.x2-self.x1)
local b= (self.y2-(self.x2*m))
local dist = (math.abs(((-m)*coords.x)+coords.y+(-b)))/(math.sqrt((m^2)+1))
local len = math.sqrt(((self.x2-self.x1)^2)+((self.y2-self.y1)^2))
local p1dist = vec2(self.x1,self.y1):dist(coords)
local p2dist = vec2(self.x2,self.y2):dist(coords)
if self.vert then dist = math.abs(coords.x-self.x1) end
if p1dist > len or p2dist > len then
if p1dist < p2dist then
return p1dist
else
return p2dist
end
else
return dist
end
else
return 9999999999
end
end
function Laser:processtimer()
if ElapsedTime-elapsed-self.elapsed > self.time then
self.timer = self.OFF
end
end
function Laser:touched(touch)
-- Codea does not automatically call this method
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment