HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Pong-like game built in Love2D

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
builtponglove2dlikegame

Problem

I just started learning programming in the past two or three months. I started with Python, and now I'm learning lua. I started game programming this week and this is a game I made with the Love2D engine for lua. If you don't know how it works, there are these three main functions:

love.load()   -- Runs at game startup and doesn't run again unless explicitly called

love.update() -- Updates values in the game and runs every tick. It uses delta time as an argument for consistent speed on every machine

love.draw()   -- Draws the updates to the screen, however you choose for them to be displayed.


love.update() and love.draw() loop back and forth in-between each other.
love.update() always runs first and then love.draw().

This is my first game, a Pong like game where you destroy blocks with a ball bounced off of a paddle like player. I would like less criticism on the game itself, but more on the structure of the code. Please let me know if you see anywhere I could make a function, or achieve something with less code.

```
function love.load()
-- WINDOW SETUP
love.window.setTitle("Block Buster")
height = love.graphics.getHeight()
width = love.graphics.getWidth()

-- SOUND SOURCES
hit = love.audio.newSource("hit.mp3")
bounce = love.audio.newSource("bounce.mp3")
loss = love.audio.newSource("loss.mp3")

-- PLAYER SETUP
player = {}
function player.load()
player.width = 70
player.height = 20
player.x = width/2 - player.width/2
player.y = height - player.height
player.speed = 400
player.lives = 5
player.points = 0
end
player.load()

-- BLOCKS
blocks = {}
blocks.draw = {}

-- LOAD BLOCKS
function blocks.load()
column = 0; row = 1
while 5 >= row do
block = {}
block.width = width/10 - 5
block.height = 20
block.x = column * (block.width + 5)
block.y = row * (block.height + 5)
table.insert(blocks.draw, block)
column = column + 1
if column == 1

Solution

1) Instead of this:

if ball.direction == "uur" then ball.direction = "uul"
elseif ball.direction == "ur" then ball.direction = "ul"
elseif ball.direction == "urr" then ball.direction = "ull"
elseif ball.direction == "drr" then ball.direction = "dll"
elseif ball.direction == "dr" then ball.direction = "dl"
elseif ball.direction == "ddr" then ball.direction = "ddl"
elseif ball.direction == "ddl" then ball.direction = "ddr"
elseif ball.direction == "dl" then ball.direction = "dr"
elseif ball.direction == "dll" then ball.direction = "drr"
elseif ball.direction == "ull" then ball.direction = "urr"
elseif ball.direction == "ul" then ball.direction = "ur"
elseif ball.direction == "uul" then ball.direction = "uur"


You may want to use a table with all the values.

local WALL_BOUNCE = {
    uur = "uul",
    ur = "ul"
-- etc...
}


And in your function just use:

ball.direction = WALL_BOUNCE[ball.direction]


2) Another point:

if ball.direction == "u" then
  ball.y = ball.y - 2 * (dt * ball.speed)
elseif ball.direction == "uur" then
  ball.y = ball.y - 2 * (dt * ball.speed)
  ball.x = ball.x + 1 * (dt * ball.speed)
elseif ball.direction == "ur" then
  ball.y = ball.y - 2 * (dt * ball.speed)
  ball.x = ball.x + 2 * (dt * ball.speed)
elseif ball.direction == "urr" then
  ball.y = ball.y - 1 * (dt * ball.speed)
  ball.x = ball.x + 2 * (dt * ball.speed)
elseif ball.direction == "drr" then
  ball.y = ball.y + 1 * (dt * ball.speed)
  ball.x = ball.x + 2 * (dt * ball.speed)
elseif ball.direction == "dr" then
  ball.y = ball.y + 2 * (dt * ball.speed)
  ball.x = ball.x + 2 * (dt * ball.speed)
elseif ball.direction == "ddr" then
  ball.y = ball.y + 2 * (dt * ball.speed)
  ball.x = ball.x + 1 * (dt * ball.speed)
elseif ball.direction == "d" then
  ball.y = ball.y + 2 * (dt * ball.speed)
elseif ball.direction == "ddl" then
  ball.y = ball.y + 2 * (dt * ball.speed)
  ball.x = ball.x - 1 * (dt * ball.speed)
elseif ball.direction == "dl" then
  ball.y = ball.y + 2 * (dt * ball.speed)
  ball.x = ball.x - 2 * (dt * ball.speed)
elseif ball.direction == "dll" then
  ball.y = ball.y + 1 * (dt * ball.speed)
  ball.x = ball.x - 2 * (dt * ball.speed)
elseif ball.direction == "ull" then
  ball.y = ball.y - 1 * (dt * ball.speed)
  ball.x = ball.x - 2 * (dt * ball.speed)
elseif ball.direction == "ul" then
  ball.y = ball.y - 2 * (dt * ball.speed)
  ball.x = ball.x - 2 * (dt * ball.speed)
elseif ball.direction == "uul" then
  ball.y = ball.y - 2 * (dt * ball.speed)
  ball.x = ball.x - 1 * (dt * ball.speed)
end


You can use a table here too, it will have a format

local VELOCITY = {
    u = {0, -2},
    uur = {1, -2}
-- etc... 
-- direction = {velocity_x, velocity_y}
}

local velocity = VELOCITY[ball.direction]
ball.x = ball.x + velocity[1] * dt * ball.speed
ball.y = ball.y + velocity[2] * dt * ball.speed


3) Use local variables wherever possible. However for a simple project like this one this is not obligatory

Code Snippets

if ball.direction == "uur" then ball.direction = "uul"
elseif ball.direction == "ur" then ball.direction = "ul"
elseif ball.direction == "urr" then ball.direction = "ull"
elseif ball.direction == "drr" then ball.direction = "dll"
elseif ball.direction == "dr" then ball.direction = "dl"
elseif ball.direction == "ddr" then ball.direction = "ddl"
elseif ball.direction == "ddl" then ball.direction = "ddr"
elseif ball.direction == "dl" then ball.direction = "dr"
elseif ball.direction == "dll" then ball.direction = "drr"
elseif ball.direction == "ull" then ball.direction = "urr"
elseif ball.direction == "ul" then ball.direction = "ur"
elseif ball.direction == "uul" then ball.direction = "uur"
local WALL_BOUNCE = {
    uur = "uul",
    ur = "ul"
-- etc...
}
ball.direction = WALL_BOUNCE[ball.direction]
if ball.direction == "u" then
  ball.y = ball.y - 2 * (dt * ball.speed)
elseif ball.direction == "uur" then
  ball.y = ball.y - 2 * (dt * ball.speed)
  ball.x = ball.x + 1 * (dt * ball.speed)
elseif ball.direction == "ur" then
  ball.y = ball.y - 2 * (dt * ball.speed)
  ball.x = ball.x + 2 * (dt * ball.speed)
elseif ball.direction == "urr" then
  ball.y = ball.y - 1 * (dt * ball.speed)
  ball.x = ball.x + 2 * (dt * ball.speed)
elseif ball.direction == "drr" then
  ball.y = ball.y + 1 * (dt * ball.speed)
  ball.x = ball.x + 2 * (dt * ball.speed)
elseif ball.direction == "dr" then
  ball.y = ball.y + 2 * (dt * ball.speed)
  ball.x = ball.x + 2 * (dt * ball.speed)
elseif ball.direction == "ddr" then
  ball.y = ball.y + 2 * (dt * ball.speed)
  ball.x = ball.x + 1 * (dt * ball.speed)
elseif ball.direction == "d" then
  ball.y = ball.y + 2 * (dt * ball.speed)
elseif ball.direction == "ddl" then
  ball.y = ball.y + 2 * (dt * ball.speed)
  ball.x = ball.x - 1 * (dt * ball.speed)
elseif ball.direction == "dl" then
  ball.y = ball.y + 2 * (dt * ball.speed)
  ball.x = ball.x - 2 * (dt * ball.speed)
elseif ball.direction == "dll" then
  ball.y = ball.y + 1 * (dt * ball.speed)
  ball.x = ball.x - 2 * (dt * ball.speed)
elseif ball.direction == "ull" then
  ball.y = ball.y - 1 * (dt * ball.speed)
  ball.x = ball.x - 2 * (dt * ball.speed)
elseif ball.direction == "ul" then
  ball.y = ball.y - 2 * (dt * ball.speed)
  ball.x = ball.x - 2 * (dt * ball.speed)
elseif ball.direction == "uul" then
  ball.y = ball.y - 2 * (dt * ball.speed)
  ball.x = ball.x - 1 * (dt * ball.speed)
end
local VELOCITY = {
    u = {0, -2},
    uur = {1, -2}
-- etc... 
-- direction = {velocity_x, velocity_y}
}

local velocity = VELOCITY[ball.direction]
ball.x = ball.x + velocity[1] * dt * ball.speed
ball.y = ball.y + velocity[2] * dt * ball.speed

Context

StackExchange Code Review Q#99149, answer score: 4

Revisions (0)

No revisions yet.