-
Notifications
You must be signed in to change notification settings - Fork 20
Game Objects as Lua Tables
In the first part, several game objects have been introduced. They have been represented as Lua tables in the code, but some improvements are possible.
In complicated games a number of game elements can be wast and it is sometimes convenient to use object-oriented approach in the code to organize and structure them. In such a case, each game object is represented as an instance of a class. For simple games, such as this one, it is not necessary, and it is suffice to use basic Lua functionality and represent each game object as a simple Lua table. An example of object-oriented approach is provided in one of the appendices.
Regarding the initial code, a first thing to notice is that love.update
and love.draw
can be split into several functions, corresponding to update and draw of independent game objects:
function love.update( dt )
ball.update( dt )
platform.update( dt )
.....
end
function love.draw()
ball.draw()
platform.draw()
.....
end
The ball.update
and ball.draw
are defined as
function ball.update( dt )
ball.position_x = ball.position_x + ball.speed_x * dt
ball.position_y = ball.position_y + ball.speed_y * dt
end
function ball.draw()
local segments_in_circle = 16
love.graphics.circle( 'line',
ball.position_x,
ball.position_y,
ball.radius,
segments_in_circle )
end
The functions for the platform are similar.
With bricks, however, the situation is more complicated.
First of all, there are going to be several of them, each with it's own characteristics.
I replace the brick
table from the previous part with a different one - bricks
,
where I'm going to store all the relevant information.
local bricks = {}
.....
The bricks themselves are going to be stored in the bricks.current_level_bricks
table, which will be populated on level construction.
bricks.current_level_bricks = {}
Each single brick is represented as a table with position_x
, position_y
, width
and height
fields.
I define a special function bricks.new_brick
to construct such objects:
function bricks.new_brick( position_x, position_y, width, height )
return( { position_x = position_x,
position_y = position_y,
width = width or bricks.brick_width, --(*1)
height = height or bricks.brick_height } )
end
(*1): If width
is supplied as an argument to the bricks.new_brick
call, than that value is used; otherwise
the default one bricks.brick_width
is used.
Each single brick can be drawn with the following function:
function bricks.draw_brick( single_brick )
love.graphics.rectangle( 'line',
single_brick.position_x,
single_brick.position_y,
single_brick.width,
single_brick.height )
end
As before, there is nothing to update
in brick:
function bricks.update_brick( single_brick )
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: