Skip to content

Instantly share code, notes, and snippets.

@Synvox
Created October 5, 2012 18:19
Show Gist options
  • Save Synvox/3841486 to your computer and use it in GitHub Desktop.
Save Synvox/3841486 to your computer and use it in GitHub Desktop.
Evade
--# Main
function setup()
displayMode(FULLSCREEN)
instances={}
for i=0,20 do
spawn(Star())
end
ship=Ship(WIDTH/2,HEIGHT/2)
spawn(ship)
for i=0,20 do
spawn(Asteroid(math.random(0,WIDTH),HEIGHT+100+math.random(0,HEIGHT+200)))
end
end
function draw()
background(0, 0, 0, 255)
for i,obj in ipairs(instances) do
index=i
obj:update()
for j,other in ipairs(instances) do
other:collides(obj)
end
i=index
end
for i,obj in ipairs(instances) do
obj:draw()
end
if math.ceil(math.random(150))==1 then
spawn(Ammo())
end
fill(255, 255, 255, 255)
fontSize(20)
font("Futura-CondensedMedium")
--textAlign(RIGHT)
text("Score: "..ship.score,50,50)
text("Ammo: "..ship.ammo,50,75)
end
function dist(o1,o2)
return math.sqrt(math.pow((o1.x-o2.x),2)+math.pow((o1.y-o2.y),2))
end
function spawn(inst)
table.insert(instances,inst)
end
function destroy()
table.remove(instances,index)
index = index - 1
end
function touched(t)
if t.state==BEGAN then
ship:shoot()
end
end
--# Star
Star = class()
function Star:init()
self.x = math.random(0,WIDTH)
self.y = math.random(0,HEIGHT)
self.size=math.random()
self.speed=self.size*8+8
self.rsize = self.size * 6
self.is="star"
end
function Star:update()
self.y = self.y - self.speed
if self.y<0 then
self.y = self.y + HEIGHT
end
end
function Star:draw()
fill(255, 255, 255, 255*self.size)
ellipse(self.x,self.y,self.rsize,self.rsize)
end
function Star:collides(obj) end
--# Ship
Ship = class()
function Ship:init(x,y)
self.x = x
self.y = y
self.is="ship"
self.ammo=20
self.score=0
end
function Ship:update()
self.x = self.x + Gravity.x*15
self.y = self.y + Gravity.y*8 + 5
self.score = self.score + 1
end
function Ship:draw()
if math.abs(Gravity.x)<.05 then
sprite("Tyrian Remastered:Ship B",self.x,self.y)
elseif math.abs(Gravity.x)<.1 then
if Gravity.x<0 then
sprite("Tyrian Remastered:Ship B L1",self.x,self.y)
else
sprite("Tyrian Remastered:Ship B R1",self.x,self.y)
end
elseif Gravity.x<0 then
sprite("Tyrian Remastered:Ship B L2",self.x,self.y)
else
sprite("Tyrian Remastered:Ship B R2",self.x,self.y)
end
end
function Ship:shoot()
if self.ammo==0 then
return
sound(SOUND_HIT, 20942)
end
spawn(Bullet(self.x,self.y))
self.ammo = self.ammo - 1
sound(SOUND_HIT, 6975)
end
function Ship:collides(obj)
if obj.is=="asteroid" and dist(self,obj)<obj.size*10 then
close()
end
end
--# Bullet
Bullet = class()
function Bullet:init(x,y)
self.x = x
self.y = y
self.speed=16
self.is="bullet"
self.destroy=false
end
function Bullet:update()
self.y = self.y + self.speed
if self.y>HEIGHT or self.destroy then
destroy()
end
end
function Bullet:draw()
sprite("Tyrian Remastered:Bullet Fire D",self.x,self.y)
end
function Bullet:collides(obj) end
--# Asteroid
Asteroid = class()
function Asteroid:init(x,y)
self.x = x
self.y = y
self.angle=math.random(0,360)
self.rspeed=math.random(-8,8)
self.speed=math.random(4,8)
self.size=math.ceil(math.random(2,4))
self.vx=0
self.is="asteroid"
end
function Asteroid:update()
self.y = self.y - self.speed
self.x = self.x + self.vx
self.angle = self.angle + self.rspeed
if self.angle<0 then
self.angle = self.angle + 360
elseif self.angle>360 then
self.angle = self.angle - 360
end
if self.y<-100 then
self.y = self.y + HEIGHT +100+ math.random(100)
self.x = math.random(0,WIDTH)
self.angle=math.random(0,360)
self.rspeed=math.random(-8,8)
self.vx=0
if self.size==1 then
if math.ceil(math.random(4))==1 then
self.size=4
else
destroy()
end
end
end
if self.size==0 then
destroy()
end
end
function Asteroid:draw()
pushMatrix()
translate(self.x,self.y)
rotate(self.angle)
if self.size==4 then
sprite("Tyrian Remastered:Rock 5",0,0)
elseif self.size==3 then
sprite("Tyrian Remastered:Rock 4",0,0)
elseif self.size==2 then
sprite("Tyrian Remastered:Rock 2",0,0)
elseif self.size==1 then
sprite("Tyrian Remastered:Rock 6",0,0)
end
popMatrix()
end
function Asteroid:collides(obj)
if obj.is=="bullet" and dist(self,obj)<self.size*10 then
sound(DATA, "ZgNADwAnPDpCR1o9As+9vRRtiT5yddg+aAA+fz5HQzgyP3lU")
self.size = self.size - 1
self.vx=math.random(-8,8)
local ast=Asteroid(self.x,self.y)
ast.vx=-self.vx
ast.size=self.size
spawn(ast)
obj.destroy=true
spawn(Explosion(self.x,self.y,self.speed))
ship.score = ship.score + 100
end
end
--# Explosion
Explosion = class()
function Explosion:init(x,y,speed)
self.x = x
self.y = y
self.life=10
self.speed=speed
end
function Explosion:update()
self.life = self.life - 1
if self.life<0 then
destroy()
end
self.y = self.y - self.speed
end
function Explosion:draw()
tint(255, 255, 255, (self.life/10)*255)
sprite("Tyrian Remastered:Explosion Ball",self.x,self.y)
tint(255, 255, 255, 255)
end
function Explosion:collides(obj) end
--# Ammo
Ammo = class()
function Ammo:init(x,y,speed)
self.x = math.random(WIDTH)
self.y = HEIGHT+100
self.speed=5
end
function Ammo:update()
self.y = self.y - self.speed
if self.y<-100 then
destroy()
end
if self.destroy then
destroy()
end
end
function Ammo:draw()
sprite("Tyrian Remastered:Powerup 7",self.x,self.y)
end
function Ammo:collides(obj)
if obj.is=="ship" and dist(self,obj)<30 then
sound(SOUND_PICKUP, 20181)
obj.ammo = obj.ammo + 20
self.destroy=true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment