-
Notifications
You must be signed in to change notification settings - Fork 20
Score
In this part, score counter is implemented.
Score is increased on each successful action of the player. This adds another element of interactivity into the game and contributes a bit to player's involvement. To make it more meaningful, lives are added on certain milestones.
From implementation standpoint, score display is similar to lives display, which can be used as a starting point for this object.
local vector = require "vector"
local score_display = {}
score_display.position = vector( 680, 32 )
score_display.score = 0
function score_display.update( dt )
end
function score_display.draw()
love.graphics.print( "Score: " .. tostring( score_display.score ),
score_display.position.x,
score_display.position.y )
end
function score_display.reset()
score_display.score = 0
end
Score is updated each time a brick breaks. 10 points are added for simple brick and 30 for armored.
function score_display.add_score_for_simple_brick()
score_display.score = score_display.score + 10
end
function score_display.add_score_for_cracked_brick()
score_display.score = score_display.score + 30
end
In the current code structure, bricks.brick_hit_by_ball
function determines whether or not a brick breaks after collision with a ball. So, the score-increasing functions also have to be called from there.
function bricks.brick_hit_by_ball( i, brick, shift_ball, bonuses, score_display )
if bricks.is_simple( brick ) then
.....
score_display.add_score_for_simple_brick()
table.remove( bricks.current_level_bricks, i )
simple_break_sound:play()
elseif .....
elseif bricks.is_cracked( brick ) then
.....
score_display.add_score_for_cracked_brick()
table.remove( bricks.current_level_bricks, i )
armored_break_sound:play()
elseif bricks.is_heavyarmored( brick ) then
ball_heavyarmored_sound:play()
end
end
Just like the lives_display
, the score_counter
is placed inside the side_panel
object.
local lives_display = require "lives_display"
local score_display = require "score_display"
side_panel.lives_display = lives_display
side_panel.score_display = score_display
function side_panel.update( dt )
side_panel.lives_display.update( dt )
side_panel.score_display.update( dt )
end
function side_panel.draw()
side_panel.draw_background()
side_panel.lives_display.draw()
side_panel.score_display.draw()
end
function side_panel.reset()
side_panel.lives_display.reset()
side_panel.score_display.reset()
end
function collisions.resolve_collisions( balls, platform,
walls, bricks,
bonuses, side_panel )
collisions.balls_platform_collision( balls, platform )
collisions.balls_walls_collision( balls, walls )
collisions.balls_bricks_collision( balls, bricks, bonuses,
side_panel.score_display )
collisions.platform_walls_collision( platform, walls )
collisions.platform_bonuses_collision( platform, bonuses,
balls, walls,
side_panel.lives_display )
end
function game.update( dt )
balls.update( dt, platform )
platform.update( dt )
bricks.update( dt )
bonuses.update( dt )
walls.update( dt )
side_panel.update( dt )
collisions.resolve_collisions( balls, platform,
walls, bricks,
bonuses, side_panel )
game.check_no_more_balls( balls, side_panel.lives_display )
game.switch_to_next_level( bricks, levels )
end
- Modifications to "game"
function game.enter( prev_state, ... )
local args = ...
if prev_state == "gamepaused" then
music:resume()
end
if prev_state == "gameover" or prev_state == "gamefinished" then
side_panel.reset()
music:rewind()
end
if args and args.current_level then
bricks.clear_current_level_bricks()
bonuses.clear_current_level_bonuses()
levels.current_level = args.current_level
local level = levels.require_current_level()
bricks.construct_level( level )
balls.reset()
platform.remove_bonuses_effects()
walls.remove_bonuses_effects()
end
end
- Add life if score milestone is reached.
function game.update( dt )
.....
side_panel.lives_display.add_life_if_score_reached(
side_panel.score_display.score )
game.check_no_more_balls( balls, side_panel.lives_display )
.....
end
local lives_display = {}
lives_display.position = vector( 680, 500 )
lives_display.lives = 5
lives_display.lives_added_from_score = 0
function lives_display.add_life_if_score_reached( score )
local score_milestone = (lives_display.lives_added_from_score + 1) * 3000
if score >= score_milestone then
lives_display.add_life()
lives_display.lives_added_from_score = lives_display.lives_added_from_score + 1
end
end
function lives_display.reset()
lives_display.lives = 5
lives_display.lives_added_from_score = 0
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: