-
Notifications
You must be signed in to change notification settings - Fork 17
The Ball, The Brick, The Platform
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→
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: