Code: Select all
require "bullet"
love.window.setMode(480, 576)
love.graphics.setDefaultFilter('nearest', 'nearest')
bullet = Bullet:new()
function love.load()
--player:load()
--player.x = 86
----player.y = 182
end
function love.update()
--player:update(dt)
end
function love.draw()
love.graphics.scale(3, 3)
bullet:draw()
--player:draw()
end
Code: Select all
require "class"
Bullet = class:new()
Bullet = {} -- Doesn't really do anything just there incase
Bullet.x = 0 -- Set bullet x
Bullet.y = 0 -- Set bullet y
Bullet.gfx = {} -- Set bullet gfx
Bullet.sfx = {} -- Set bullet sfx
Bullet.speed = 2 -- Set bullet speed
Bullet.width = 10 -- Set bullet width
Bullet.height = 10 -- Set bullet height
-- Helper functions
function toNumber(x)
if (x == false) then return 0
elseif (x == true ) then return 1
else return nil
end
end
-- Resource functions
function Bullet:loadGfx(file, xOffset, yOffset)
local sprite = {}
sprite.image = love.graphics.newImage(file)
sprite.xOffset = xOffset
sprite.yOffset = yOffset
table.insert(self.gfx, sprite)
end
function Bullet:loadSfx(file)
local sound = love.audio.newSource(file)
table.insert(self.sfx, sound)
end
function Bullet:update(dt)
if (self.y <= -self.width) then bullet = nil end -- Check if bullet is otuside screen and if so then delete it
self.y = self.y - speed -- Incerment y upwards
end
function Bullet:draw()
love.graphics.setColor(255, 255, 255) -- Set draw color
for _, g in pairs(self.gfx) do -- Loop though bullet graphics
love.graphics.draw(g.image, self.x + g.xOffset, self.y + g.yOffset) -- Draw them
end
end