====== Multiple files ======
The following assumes ''main.lua'' and ''circle.lua'' are in the same directory.
--[[-------------------------------------
- Using multiple files.
- Mithat Konar
-------------------------------------]]--
require('circle') -- get the contents of file circle.lua.
--------------------
-- LÖVE functions --
--------------------
--[[ Create game globals and initial conditions. ]]--
function love.load()
-- create array of drawable circles
circlesArray = {}
-- populate the array with as many circles as you want!
table.insert(circlesArray, createCircle(0, 100, CIRCLE_DEFAULT_SIZE, CIRCLE_DEFAULT_SPEED))
table.insert(circlesArray, createCircle(0, 200, CIRCLE_DEFAULT_SIZE / 1.5, CIRCLE_DEFAULT_SPEED * 1.5))
table.insert(circlesArray, createCircle(0, 300, CIRCLE_DEFAULT_SIZE * 1.5, CIRCLE_DEFAULT_SPEED / 1.5))
table.insert(circlesArray, createCircle(0, 400, CIRCLE_DEFAULT_SIZE / 2, CIRCLE_DEFAULT_SPEED * 2))
end
--[[ Update values of game parameters. ]]--
function love.update(dt)
-- move all the circles as needed
for i=1,#circlesArray do
circleMoveHorizWrap(circlesArray[i], dt)
end
end
--[[ Render the game elements. ]]--
function love.draw()
-- draw all the circles
for i=1,#circlesArray do
circleRender(circlesArray[i])
end
end
--[[ Functions and constants for creating and using circles ]]--
-- It may make more sense to define these global constants in main.lua,
-- but this shows that you can define constants in files as well.
CIRCLE_DEFAULT_SPEED = 100 -- global constant
CIRCLE_DEFAULT_SIZE = 30 -- global constant
----------------------------------------
-- Functions to create circle objects --
----------------------------------------
--[[ Return a data object that has a circle's properties. ]]--
function createCircle(x_, y_, radius_, hSpeed_)
local theCircle = {
x = x_,
y = y_,
radius = radius_,
hSpeed = hSpeed_
}
return theCircle
end
--------------------------------------------------
-- Functions for using/modifying circle objects --
--------------------------------------------------
--[[ Move a circle to the right, wrapping it around when necessary. ]]--
function circleMoveHorizWrap(circle, dt)
circle.x = circle.x + circle.hSpeed * dt
if circle.x > (love.graphics.getWidth() + circle.radius) then
circle.x = -circle.radius
end
end
--[[ Render a circle on the screen. ]]--
function circleRender(circle)
love.graphics.circle("fill", circle.x, circle.y, circle.radius)
end