Skip to content

Instantly share code, notes, and snippets.

@jonbro
Created December 30, 2015 01:09
Show Gist options
  • Select an option

  • Save jonbro/83ea7d08723fc0f09546 to your computer and use it in GitHub Desktop.

Select an option

Save jonbro/83ea7d08723fc0f09546 to your computer and use it in GitHub Desktop.
mgl lua edition
local Star = class('Star', Actor)
function Star:init()
self.position.x = random():range()
self.position.y = random():range()
local c = random():range()
self.speed = c*0.005
self.drawing:setColor(c*1.5,c*1.5,c*1.5):addRect(0,0, 0.01*c, 0.01*c)
end
function Star:update()
self.position.y = self.position.y+self.speed
end
local Shot = class('Shot', Actor)
function Shot:initialize(...)
Actor.initialize(self, ...) -- invoking the superclass' initializer
end
function Shot:init(position)
self.drawing:setColor(1, 0,0)
:addRect(-0.02, 0.02, 0.02, 0.04)
:addRect(0.02, 0.02, 0.02, 0.04)
self.position:set(position)
self.velocity = Vector:new(0,-0.03)
end
function Shot:update()
if not self.position:onScreen() then
self:remove()
end
end
local Ship = class('Ship', Actor)
function Ship:init()
self.position.x = 0.5
self.position.y = 0.8
self.drawing:setColor(1, 0,0)
:addRect(-0.02, 0.02, 0.02, 0.04)
:addRect(0.02, 0.02, 0.02, 0.04)
:setColor(1, 1, 1)
:addRect(0,0, 0.02, 0.05)
self:newFiber()
:doOnce(function()
Shot:new(self.position)
self:addMuzzleFlash(-0.02)
self:addMuzzleFlash(0.02)
end)
:wait(3)
end
function Ship:addMuzzleFlash(offsetX)
self:newParticle()
:setPosition(Vector:new(self.position.x+offsetX, self.position.y))
:setNumber(3)
:setDirection(0, 30)
:setSpeed(0.02)
:setColor(1, 0, 0)
end
function Ship:update()
-- self.position.y = self.position.y + 0.01
self.position.x = Mouse.position.x
self.position.y = Mouse.position.y
if self:onCollision('Enemy') or self:onCollision('Bullet') then
Game.static.endGame();
end
end
local Bullet = class('Bullet', Actor)
function Bullet:init(position)
-- get the ship
self.ship = Actor.getGroup('Ship')[1]
self.position:set(position)
local angle = self.position:directionTo(self.ship.position)
self.velocity:addDirection(angle, 0.015)
self.drawing:setColor(1,1,1)
:addRects(0.02, 0.04)
:addRects(0.04, 0.02)
end
function Bullet:update()
self.rotation = self.rotation + 3
if not self.position:onScreen() then self:remove() end
end
local Enemy = class('Enemy', Actor)
function Enemy:init()
self.ship = Actor.getGroup('Ship')[1]
self.position.x = random():range()
self.position.y = 0
self.drawing:setColor(1, 1,0)
:addRect(-0.02, -0.02, 0.02, 0.04)
:addRect(0.02, -0.02, 0.02, 0.04)
:addRect(0,0, 0.02, 0.05)
--[[]]--
self:newFiber()
:doOnce(function()
if self.ship then
Bullet:new(self.position)
end
end)
:wait(30)
end
function Enemy:update()
self.position.y = self.position.y + 0.01
if self.position.y > 1 then
self:remove()
--[[]]--
self:newParticle()
:setColor(1,1,0)
:setNumber(5)
end
--[[]]--
if self:onCollision('Shot') then
local ta = TextActor:new("+1")
ta.color = Color:new(1,1,1)
ta.duration = 30
ta.size = 0.005
ta.position:set(self.position)
ta.velocity:set(Vector:new(0,-0.01))
self:remove()
self:newParticle()
:setColor(1,1,0)
:setNumber(5)
end
end
-- setup the update for the game
Game.setInit(function(self)
for i=0,30 do
local s = Star:new()
end
-- if the game has started, then run this
if self.isPlaying and self.ticks == 0 then
Ship:new()
Fiber:new()
:wait(30)
:doOnce(function()
Enemy:new()
end)
end
end)
Game.setUpdate(function()
Actor.scroll('Star', 0,0, 0,0, 1,1)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment