-
Notifications
You must be signed in to change notification settings - Fork 19
Splitting Code Into Several Files
In this part I'm finally going to split the main.lua
into several smaller files.
Module in Lua are typically return tables. The simplest way is to move previously defined table into corresponding files.
local levels = {}
levels.current_level = 1
levels.gamefinished = false
levels.sequence = {}
.....
return levels
Basically, the code doesn't change.
The only thing I want to introduce in this chapter is vectors
module from HUMP [link].
It allows to simplify some arithmetics
local vector = require "vector"
local ball = {}
ball.position = vector( 200, 500 )
ball.speed = vector( 700, 700 )
ball.radius = 10
function ball.update( dt )
ball.position = ball.position + ball.speed * dt
end
function ball.draw()
local segments_in_circle = 16
love.graphics.circle( 'line',
ball.position.x,
ball.position.y,
ball.radius,
segments_in_circle )
end
In the main.lua
it is necessary to require
the other files:
local ball = require "ball"
local platform = require "platform"
local bricks = require "bricks"
local walls = require "walls"
local collisions = require "collisions"
local levels = require "levels"
.....
The rest of the code in the main.lua
doesn't change.
Feedback is crucial to improve the tutorial!
Let me know if you have any questions, critique, suggestions or just any other ideas.
Chapter 1: Prototype
- The Ball, The Brick, The Platform
- Game Objects as Lua Tables
- Bricks and Walls
- Detecting Collisions
- Resolving Collisions
- Levels
Appendix A: Storing Levels as Strings
Appendix B: Optimized Collision Detection (draft)
Chapter 2: General Code Structure
- Splitting Code into Several Files
- Loading Levels from Files
- Straightforward Gamestates
- Advanced Gamestates
- Basic Tiles
- Different Brick Types
- Basic Sound
- Game Over
Appendix C: Stricter Modules (draft)
Appendix D-1: Intro to Classes (draft)
Appendix D-2: Chapter 2 Using Classes.
Chapter 3 (deprecated): Details
- Improved Ball Rebounds
- Ball Launch From Platform (Two Objects Moving Together)
- Mouse Controls
- Spawning Bonuses
- Bonus Effects
- Glue Bonus
- Add New Ball Bonus
- Life and Next Level Bonuses
- Random Bonuses
- Menu Buttons
- Wall Tiles
- Side Panel
- Score
- Fonts
- More Sounds
- Final Screen
- Packaging
Appendix D: GUI Layouts
Appendix E: Love-release and Love.js
Beyond Programming: