Skip to content

Game Objects as Lua Tables

noooway edited this page Aug 9, 2017 · 26 revisions

A way to handle several objects of the same type is discussed.

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

    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