love:using_objects
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | love:using_objects [2021/10/05 02:58] (current) – created mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Using objects ====== | ||
+ | <file lua main.lua> | ||
+ | --[[------------------------------------- | ||
+ | - Objects in arrays. | ||
+ | - Mithat Konar | ||
+ | -------------------------------------]]-- | ||
+ | |||
+ | --------------------------------------------- | ||
+ | -- Functions to create circle data objects -- | ||
+ | --------------------------------------------- | ||
+ | --[[ Return a data object that has a circle' | ||
+ | function createCircle(x_, | ||
+ | local theCircle = { | ||
+ | x = x_, | ||
+ | y = y_, | ||
+ | radius = radius_, | ||
+ | hSpeed = hSpeed_ | ||
+ | } | ||
+ | return theCircle | ||
+ | end | ||
+ | |||
+ | ------------------------------------------------------- | ||
+ | -- Functions for using/ | ||
+ | ------------------------------------------------------- | ||
+ | --[[ Move a circle to the right, wrapping it around when necessary. ]]-- | ||
+ | function circleMoveHorizWrap(circle, | ||
+ | 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(" | ||
+ | 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, | ||
+ | table.insert(circlesArray, | ||
+ | table.insert(circlesArray, | ||
+ | table.insert(circlesArray, | ||
+ | end | ||
+ | |||
+ | --[[ Update values of game parameters. ]]-- | ||
+ | function love.update(dt) | ||
+ | -- move all the circles as needed | ||
+ | for i=1,# | ||
+ | circleMoveHorizWrap(circlesArray[i], | ||
+ | end | ||
+ | end | ||
+ | |||
+ | --[[ Render the game elements. ]]-- | ||
+ | function love.draw() | ||
+ | -- draw all the circles | ||
+ | for i=1,# | ||
+ | circleRender(circlesArray[i]) | ||
+ | end | ||
+ | end | ||
+ | |||
+ | </ |
love/using_objects.txt · Last modified: 2021/10/05 02:58 by mithat