Skip to content

Loading Levels From Files

noooway edited this page Jan 17, 2017 · 11 revisions

While we are splitting the code, let's also move levels into separate files. This is not immediately necessary, but will become convenient later, when the amount of levels grow.

The idea is to move each level definition in it's own file. They are kept in the levels folder.

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

    Home
    Acknowledgements
    Todo

Chapter 1: Prototype

  1. The Ball, The Brick, The Platform
  2. Game Objects as Lua Tables
  3. Bricks and Walls
  4. Detecting Collisions
  5. Resolving Collisions
  6. Levels

    Appendix A: Storing Levels as Strings
    Appendix B: Optimized Collision Detection (draft)

Chapter 2: General Code Structure

  1. Splitting Code into Several Files
  2. Loading Levels from Files
  3. Straightforward Gamestates
  4. Advanced Gamestates
  5. Basic Tiles
  6. Different Brick Types
  7. Basic Sound
  8. 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

  1. Improved Ball Rebounds
  2. Ball Launch From Platform (Two Objects Moving Together)
  3. Mouse Controls
  4. Spawning Bonuses
  5. Bonus Effects
  6. Glue Bonus
  7. Add New Ball Bonus
  8. Life and Next Level Bonuses
  9. Random Bonuses
  10. Menu Buttons
  11. Wall Tiles
  12. Side Panel
  13. Score
  14. Fonts
  15. More Sounds
  16. Final Screen
  17. Packaging

    Appendix D: GUI Layouts
    Appendix E: Love-release and Love.js

Beyond Programming:

  1. Game Design
  2. Minimal Marketing (draft)
  3. Finding a Team (draft)

Archive

Clone this wiki locally