-
Notifications
You must be signed in to change notification settings - Fork 20
Loading Levels From Files
While we are talking about splitting the code, I also want to move level definitions into separate files. This is not immediately necessary, but will become convenient later, when the amount of levels will grow.
The files with the levels descriptions will be kept at the levels
folder.
It is possible to write each level-file as a separate Lua module.
However, it is considered a good practice to avoid any code in level files, preferably making them data-only. Therefore, the only thing that remains from the Lua module structure is the return statement.
Here is an example of hey.lua
:
return {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 1, 0, 2, 2, 2, 0, 3, 0, 3 },
{ 1, 0, 1, 0, 2, 0, 0, 0, 3, 0, 3 },
{ 1, 1, 1, 0, 2, 2, 0, 0, 0, 3, 0 },
{ 1, 0, 1, 0, 2, 0, 0, 0, 0, 3, 0 },
{ 1, 0, 1, 0, 2, 2, 2, 0, 0, 3, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}
To maintain the order of the levels, the levels.sequence
also has to be move into separate file:
return {
"hey",
"bye"
}
The levels.sequence
has to be required
levels.sequence = require "levels/sequence"
.
Also the level files:
function levels.require_current_level()
local level_filename = "levels/" ..
levels.sequence[ levels.current_level ]
local level = require( level_filename )
return level
end
Corresponding chages in love.load
and levels.switch_to_next_level( bricks, ball )
.
function love.load()
level = levels.require_current_level()
bricks.construct_level( level )
walls.construct_walls()
end
There are couple of other things I want to introduce in this part. To facilitate the process of destroying bricks and moving to next level quickly:
function love.keyreleased( key, code )
if key == 'c' then
bricks.clear_current_level_bricks()
.....
end
function bricks.clear_current_level_bricks()
for i in pairs( bricks.current_level_bricks ) do
bricks.current_level_bricks[i] = nil
end
end
The second one is brick coloring depending on the brick`s type:
function bricks.draw_brick( single_brick )
love.graphics.rectangle( 'line',
single_brick.position.x,
single_brick.position.y,
single_brick.width,
single_brick.height )
local r, g, b, a = love.graphics.getColor( )
if single_brick.bricktype == 1 then
love.graphics.setColor( 255, 0, 0, 100 )
elseif single_brick.bricktype == 2 then
love.graphics.setColor( 0, 255, 0, 100 )
elseif single_brick.bricktype == 3 then
love.graphics.setColor( 0, 0, 255, 100 )
end
love.graphics.rectangle( 'fill',
single_brick.position.x,
single_brick.position.y,
single_brick.width,
single_brick.height )
love.graphics.setColor( r, g, b, a )
end
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: