Skip to content

Instantly share code, notes, and snippets.

@Synvox
Created October 19, 2012 20:34
Show Gist options
  • Save Synvox/3920572 to your computer and use it in GitHub Desktop.
Save Synvox/3920572 to your computer and use it in GitHub Desktop.
Lunar Lander
--# Main
-- Lunar Lander
function setup()
displayMode(FULLSCREEN)
instances = {}
ship = Ship(WIDTH/2,HEIGHT/2)
table.insert(instances,ship)
leftbutton = Button(100,200,"left")
table.insert(instances,leftbutton)
rightbutton = Button(300,200,"right")
table.insert(instances,rightbutton)
thrustbutton = Button(900,200,"thrust")
table.insert(instances,thrustbutton)
end
function dist(o1,o2)
return math.sqrt(math.pow((o1.x-o2.x),2)+math.pow((o1.y-o2.y),2))
end
function draw()
background(40, 40, 50)
strokeWidth(5)
for i,o in ipairs(instances) do
o:draw()
end
end
--# Ship
Ship = class()
function Ship:init(x, y)
self.x = x
self.y = y
self.direction = 0
self.speed = 1
end
function Ship:thrust()
end
function Ship:draw()
local direction = (90+self.direction)*(2*math.pi)/360
self.x = self.x + self.speed*math.cos(direction)
self.y = self.y + self.speed*math.sin(direction)
pushMatrix()
translate(self.x,self.y)
rotate(self.direction)
sprite("Tyrian Remastered:Ship C",0,0)
popMatrix()
end
--# Button
Button = class()
function Button:init(x,y,action)
self.x = x
self.y = y
self.action = action
self.size = 120
self.active = false
end
function Button:withinDistance()
if CurrentTouch.state == ENDED then return end
if dist(self,CurrentTouch)<self.size/2 then
self.active = true
return true
else
self.active = false
return false
end
end
function Button:draw()
if self.action == "left" then
fill(255, 0, 0, 255)
if self:withinDistance() then
ship.direction = ship.direction + 1
end
elseif self.action == "right" then
fill(229, 255, 0, 255)
if self:withinDistance() then
ship.direction = ship.direction - 1
end
elseif self.action == "thrust" then
fill(0, 255, 19, 255)
if self:withinDistance() then
ship:thrust()
end
end
ellipse(self.x,self.y,self.size)
end
function Button:touched(touch)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment