Skip to content

The Ball, The Brick, The Platform

noooway edited this page Jul 31, 2018 · 11 revisions

A typical videogame consists of a number of interacting game objects such as a main character, enemies, obstacles and so on. Each object has certain properties, that control how it is displayed, responds to player actions and interacts with other game elements.

Things that are absolutely essential to any arkanoid-type game are a moving platform, a number of bricks that the player has to break, and a ball bouncing back and forth between them. Let's start from getting them on a screen, so there is something to play with.

As the LÖVE hamster ball demo tells us, we need to define love.load, love.update, and love.draw callbacks. love.load is executed only once right after the game is launched, love.update is continually executed to update states of the game objects, and love.draw redraws the screen according to the current state of the objects. These functions have to operate on some objects, so we also need to define them and their properties.

--(*1)

function love.load()
end
 
function love.update(dt)
end
 
function love.draw()
end

(*1): beginning of the file is a good place to define properties of several game objects

It's possible to represent objects as simple Lua tables with fields holding necessary properties.

The ball has position and speed:

local ball = {}
ball.position_x = 300
ball.position_y = 300
ball.speed_x = 300
ball.speed_y = 300

Same for the platform, but it doesn't need to have speed_y (since it moves only horizontally):

local platform = {}
platform.position_x = 500
platform.position_y = 500
platform.speed_x = 300

The brick doesn't need speed at all:

local brick = {}
brick.position_x = 100
brick.position_y = 100

This information is enough to define love.update() callback.

function love.update( dt )
   ball.position_x = ball.position_x + ball.speed_x * dt --(*1)
   ball.position_y = ball.position_y + ball.speed_y * dt
   
   if love.keyboard.isDown("right") then  --(*2)
      platform.position_x = platform.position_x + (platform.speed_x * dt)
   end
   if love.keyboard.isDown("left") then
      platform.position_x = platform.position_x - (platform.speed_x * dt)
   end
end

(*1): The ball's position is updated according to it's speed.
(*2): The platform reacts on the arrow keys, as suggested by the Hamster demo.

In order to define love.draw(), we need some additional properties for our objects: a radius for the ball, and a width and a height for the brick and the platform. Actual values are not important right now, so they can be assigned somewhat arbitrary.

   ..... --(*1)
   ball.radius = 10
   .....
   platform.width = 70
   platform.height = 20
   .....
   brick.width = 50
   brick.height = 30
   .....

(*1): I'm going to use 5 dots to denote code skips. Usually it's is clear from the context.

Of course, it is reasonable to place these properties near the position and the speed definitions.

Now it is possible to define love.draw() callback. There are a number of functions in love.graphics, which are capable to draw graphical primitives. We need love.graphics.circle for the ball and love.graphics.rectangle for the brick and the platform. With them, love.draw() can be defined in the following way:

function love.draw()
   local segments_in_circle = 16
   love.graphics.circle( 'line',
			 ball.position_x,
			 ball.position_y,
			 ball.radius,
			 segments_in_circle )
   love.graphics.rectangle( 'line',
			    platform.position_x,
			    platform.position_y,
			    platform.width,
			    platform.height )
   love.graphics.rectangle( 'line',
			    brick.position_x,
			    brick.position_y,
			    brick.width,
			    brick.height )
end

The last two remaining functions are love.quit() and love.load(). We do not need them right now, but it is nice to put a remainder about them:

function love.load()
end
.....
function love.quit()
  print("Thanks for playing! Come back soon!")
end



                                  ↑ To the top           Next section: 1.2 - Game Objects as Lua Tables→

    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