-
Notifications
You must be signed in to change notification settings - Fork 17
Levels
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
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: