User Tools

Site Tools


love:using_objects

Using objects

main.lua
--[[-------------------------------------
- Objects in arrays.
- Mithat Konar
-------------------------------------]]--
 
---------------------------------------------
-- Functions to create circle data 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 data 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
 
--------------------
-- LÖVE functions --
--------------------
--[[ Create game globals and initial conditions. ]]--
function love.load()
    -- circle constants
    CIRCLE_DEFAULT_SPEED = 100
    CIRCLE_DEFAULT_SIZE = 30
 
    -- 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
love/using_objects.txt · Last modified: 2021/10/05 02:58 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki