Skip to content
noooway edited this page Jan 13, 2017 · 18 revisions

Next thing is to implement different levels.

All levels-related information will be held at the levels table. The actual sequence of levels will be stored in the levels.sequence array.

local levels = {}
.....
levels.sequence = {}

I represent each level as a 2d-table. Inner tables correspond to the rows of the bricks.

levels.sequence[1] = {
   { 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, 1, 1, 1, 0, 1, 0, 1 },
   { 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1 },
   { 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0 },
   { 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 },
   { 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0 },
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
}

levels.sequence[2] = {
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
   { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 },
   { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
   { 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0 },
   { 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0 },
   { 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1 },
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
}

Since there are several levels, it is necessary to maintain the current level number

levels.current_level = 1

Changes are necessary in bricks.construct_level function, to make it read the table with blocks arrangement. The brick is created if it's type is nonzero.

function bricks.construct_level( level_bricks_arrangement )
   bricks.no_more_bricks = false
   for row_index, row in pairs( level_bricks_arrangement ) do
      for col_index, bricktype in pairs( row ) do
         if bricktype ~= 0 then
            local new_brick_position_x = bricks.top_left_position_x +
               ( col_index - 1 ) *
               ( bricks.brick_width + bricks.horizontal_distance )
            local new_brick_position_y = bricks.top_left_position_y +
               ( row_index - 1 ) *
               ( bricks.brick_height + bricks.vertical_distance )
            local new_brick = bricks.new_brick( new_brick_position_x,
                                                new_brick_position_y )
            table.insert( bricks.current_level_bricks, new_brick )
         end
      end
   end
end

The switch to the next level happens when there are no more bricks left. The amount of remaining bricks is monitored by bricks.update function. If there are none, the flag is raised.

function bricks.update( dt )
   if #bricks.current_level_bricks == 0 then
      bricks.no_more_bricks = true
   else
      for _, brick in pairs( bricks.current_level_bricks ) do
         bricks.update_brick( brick )
      end      
   end
end

This flag is checked in each update cycle.

function love.update( dt )
   .....
   levels.switch_to_next_level( bricks )
end

If it is on, then the current_level is updated, the new set of bricks is constructed and the ball is repositioned.

function levels.switch_to_next_level( bricks )
   if bricks.no_more_bricks then
      .....
         levels.current_level = levels.current_level + 1
         bricks.construct_level( levels.sequence[levels.current_level] )
         ball.reposition()
      .....
      end
   end
end

function ball.reposition()
   ball.position_x = 200
   ball.position_y = 500   
end

We also need to make something when there are no more levels, i.e. when the game is finished. First, we need the levels.max_level counter.

levels.max_level = 2

I want to display a congratulations message. To signal, that there are no more levels left, switch_to_next_level function raises the appropriate flag.

function levels.switch_to_next_level( bricks )
   if bricks.no_more_bricks then
      if levels.current_level < levels.max_level then
	 levels.current_level = levels.current_level + 1
	 ball.reposition()
	 bricks.construct_level( levels )	 
      elseif levels.current_level >= levels.max_level then
	 levels.gamefinished = true
      end
   end
end

When it is on, the message is displayed

function love.draw()
   .....
   walls.draw()
   if levels.gamefinished then
      love.graphics.printf( "Congratulations!\n" ..
			       "You have finished the game!",
			    300, 250, 200, "center" )
   end
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