User Tools

Site Tools


love:multiple_files

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
love:multiple_files [2021/10/05 03:02]
mithat created
love:multiple_files [2021/10/05 03:08]
mithat
Line 4: Line 4:
  
 <file lua main.lua> <file lua main.lua>
-</file>+--[[------------------------------------- 
 +- 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
 +</file>
  
 <file lua circle.lua> <file lua circle.lua>
 --[[ Functions and constants for creating and using circles ]]-- --[[ Functions and constants for creating and using circles ]]--
  
--- This may not be the best place to define these global constants, +-- It may make more sense to define these global constants in main.lua
--- but it shows that you can define constants in files as well.+-- but this shows that you can define constants in files as well.
 CIRCLE_DEFAULT_SPEED = 100  -- global constant CIRCLE_DEFAULT_SPEED = 100  -- global constant
 CIRCLE_DEFAULT_SIZE = 30    -- global constant CIRCLE_DEFAULT_SIZE = 30    -- global constant
Line 29: Line 65:
 end end
  
----------------------------------------- +-------------------------------------------------- 
--- Functions for using circle objects -- +-- Functions for using/modifying circle objects -- 
-----------------------------------------+--------------------------------------------------
 --[[ Move a circle to the right, wrapping it around when necessary. ]]-- --[[ Move a circle to the right, wrapping it around when necessary. ]]--
 function circleMoveHorizWrap(circle, dt) function circleMoveHorizWrap(circle, dt)
love/multiple_files.txt · Last modified: 2021/10/05 03:08 by mithat