Formatting
This commit is contained in:
parent
b33983318c
commit
65fab4710d
380
main.lua
380
main.lua
@ -1,255 +1,255 @@
|
|||||||
Settings = {
|
Settings = {
|
||||||
player_speed = 8,
|
player_speed = 8,
|
||||||
ball_speed = 5,
|
ball_speed = 5,
|
||||||
|
|
||||||
block_rows = 3,
|
block_rows = 3,
|
||||||
block_cols = 8,
|
block_cols = 8,
|
||||||
|
|
||||||
block_height = 15,
|
block_height = 15,
|
||||||
block_offset_x = 30,
|
block_offset_x = 30,
|
||||||
block_offset_y = 80,
|
block_offset_y = 80,
|
||||||
block_gap = 5,
|
block_gap = 5,
|
||||||
|
|
||||||
collision_margin = 2 -- kinda tied to ball radius lol
|
collision_margin = 2, -- kinda tied to ball radius lol
|
||||||
}
|
}
|
||||||
|
|
||||||
-- represenets the player-controlled paddle
|
-- represenets the player-controlled paddle
|
||||||
Player = {
|
Player = {
|
||||||
x = 0,
|
x = 0,
|
||||||
y = 0,
|
y = 0,
|
||||||
width = 100,
|
width = 100,
|
||||||
height = 10,
|
height = 10,
|
||||||
moving_left = false,
|
moving_left = false,
|
||||||
moving_right = false,
|
moving_right = false,
|
||||||
-- Returns 2 values, x and y!
|
-- Returns 2 values, x and y!
|
||||||
getCenter = function()
|
getCenter = function()
|
||||||
return Player.x + Player.width / 2, Player.y + Player.height / 2
|
return Player.x + Player.width / 2, Player.y + Player.height / 2
|
||||||
end
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Game = {
|
Game = {
|
||||||
ball = {
|
ball = {
|
||||||
position = { x = 0, y = 0 },
|
position = { x = 0, y = 0 },
|
||||||
velocity = { dx = 0, dy = 0 },
|
velocity = { dx = 0, dy = 0 },
|
||||||
radius = 5
|
radius = 5,
|
||||||
},
|
},
|
||||||
score = 0,
|
score = 0,
|
||||||
-- Array of breakable blocks, generated on load!
|
-- Array of breakable blocks, generated on load!
|
||||||
blocks = {},
|
blocks = {},
|
||||||
is_playing = true,
|
is_playing = true,
|
||||||
}
|
}
|
||||||
|
|
||||||
Assets = {}
|
Assets = {}
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
Assets.jirka = love.graphics.newImage("jirka.png")
|
Assets.jirka = love.graphics.newImage("jirka.png")
|
||||||
Assets.impact = love.audio.newSource("impact.ogg", "static")
|
Assets.impact = love.audio.newSource("impact.ogg", "static")
|
||||||
Assets.impact_wall = love.audio.newSource("impact_wall.ogg", "static")
|
Assets.impact_wall = love.audio.newSource("impact_wall.ogg", "static")
|
||||||
Assets.impact_player = love.audio.newSource("impact_player.ogg", "static")
|
Assets.impact_player = love.audio.newSource("impact_player.ogg", "static")
|
||||||
|
|
||||||
setupGame()
|
love.window.setTitle("Breakout")
|
||||||
|
|
||||||
|
setupGame()
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update()
|
function love.update()
|
||||||
if Game.is_playing == false then
|
if Game.is_playing == false then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if Player.moving_left then
|
if Player.moving_left then
|
||||||
Player.x = Player.x - Settings.player_speed
|
Player.x = Player.x - Settings.player_speed
|
||||||
end
|
end
|
||||||
if Player.moving_right then
|
if Player.moving_right then
|
||||||
Player.x = Player.x + Settings.player_speed
|
Player.x = Player.x + Settings.player_speed
|
||||||
end
|
end
|
||||||
|
|
||||||
local b = Game.ball
|
local b = Game.ball
|
||||||
|
|
||||||
-- move ball
|
-- move ball
|
||||||
b.position.x = b.position.x + b.velocity.dx
|
b.position.x = b.position.x + b.velocity.dx
|
||||||
b.position.y = b.position.y + b.velocity.dy
|
b.position.y = b.position.y + b.velocity.dy
|
||||||
|
|
||||||
-- Check wall collisions
|
-- Check wall collisions
|
||||||
if b.position.x <= Settings.collision_margin then
|
if b.position.x <= Settings.collision_margin then
|
||||||
b.velocity.dx = -b.velocity.dx
|
b.velocity.dx = -b.velocity.dx
|
||||||
Assets.impact_wall:stop()
|
Assets.impact_wall:stop()
|
||||||
Assets.impact_wall:play()
|
Assets.impact_wall:play()
|
||||||
end
|
end
|
||||||
if b.position.x >= love.graphics.getWidth() - Settings.collision_margin then
|
if b.position.x >= love.graphics.getWidth() - Settings.collision_margin then
|
||||||
b.velocity.dx = -b.velocity.dx
|
b.velocity.dx = -b.velocity.dx
|
||||||
Assets.impact_wall:stop()
|
Assets.impact_wall:stop()
|
||||||
Assets.impact_wall:play()
|
Assets.impact_wall:play()
|
||||||
end
|
end
|
||||||
if b.position.y <= Settings.collision_margin then
|
if b.position.y <= Settings.collision_margin then
|
||||||
-- ceiling hit
|
-- ceiling hit
|
||||||
b.velocity.dy = -b.velocity.dy
|
b.velocity.dy = -b.velocity.dy
|
||||||
Assets.impact_wall:stop()
|
Assets.impact_wall:stop()
|
||||||
Assets.impact_wall:play()
|
Assets.impact_wall:play()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- player collision
|
-- player collision
|
||||||
if checkCollision(Player, Game.ball) then
|
if checkCollision(Player, Game.ball) then
|
||||||
Assets.impact_player:play()
|
Assets.impact_player:play()
|
||||||
b.velocity.dy = -b.velocity.dy
|
b.velocity.dy = -b.velocity.dy
|
||||||
local cx, cy = Player.getCenter()
|
local cx, cy = Player.getCenter()
|
||||||
local distance_from_center = b.position.x - cx
|
local distance_from_center = b.position.x - cx
|
||||||
Game.ball.velocity.dx = (distance_from_center / (Player.width / 2)) * Settings.ball_speed
|
Game.ball.velocity.dx = (distance_from_center / (Player.width / 2)) * Settings.ball_speed
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Check box collisions
|
||||||
|
for _, block in ipairs(Game.blocks) do
|
||||||
|
if block.active then
|
||||||
|
local dist_x = block.x - b.position.x
|
||||||
|
local dist_y = block.y - b.position.y
|
||||||
|
if checkCollision(block, Game.ball) then
|
||||||
|
block.active = false
|
||||||
|
Assets.impact:stop()
|
||||||
|
Assets.impact:play()
|
||||||
|
|
||||||
-- Check box collisions
|
Game.score = Game.score + 1
|
||||||
for _, block in ipairs(Game.blocks) do
|
|
||||||
if block.active then
|
|
||||||
local dist_x = block.x - b.position.x
|
|
||||||
local dist_y = block.y - b.position.y
|
|
||||||
if checkCollision(block, Game.ball) then
|
|
||||||
block.active = false
|
|
||||||
Assets.impact:stop()
|
|
||||||
Assets.impact:play()
|
|
||||||
|
|
||||||
Game.score = Game.score + 1
|
-- block hit horizontal
|
||||||
|
if b.position.x > (block.x + block.width) or b.position.x < block.x then
|
||||||
|
b.velocity.dx = -b.velocity.dx
|
||||||
|
else -- b.position.y > (block.y + block.height) or b.position.y < block.y then
|
||||||
|
b.velocity.dy = -b.velocity.dy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- block hit horizontal
|
-- GAME OVER
|
||||||
if b.position.x > (block.x + block.width) or b.position.x < block.x then
|
if b.position.y > love.graphics.getHeight() then
|
||||||
b.velocity.dx = -b.velocity.dx
|
love.event.quit(0)
|
||||||
else -- b.position.y > (block.y + block.height) or b.position.y < block.y then
|
end
|
||||||
b.velocity.dy = -b.velocity.dy
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- GAME OVER
|
-- WIN GAME??
|
||||||
if b.position.y > love.graphics.getHeight() then
|
if Game.score == #Game.blocks then
|
||||||
love.event.quit(0)
|
Game.is_playing = false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- WIN GAME??
|
|
||||||
if Game.score == #Game.blocks then
|
|
||||||
Game.is_playing = false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
if Game.is_playing == false then
|
if Game.is_playing == false then
|
||||||
love.graphics.clear(0.1, 0.9, 0.3, 1)
|
love.graphics.clear(0.1, 0.9, 0.3, 1)
|
||||||
love.graphics.setColor(0, 0, 0, 1)
|
love.graphics.setColor(0, 0, 0, 1)
|
||||||
love.graphics.print("YOU WON! GOOD JOB!", love.graphics.getWidth() / 2, love.graphics.getHeight() / 2)
|
love.graphics.print("YOU WON! GOOD JOB!", love.graphics.getWidth() / 2, love.graphics.getHeight() / 2)
|
||||||
love.graphics.print(
|
love.graphics.print(
|
||||||
"Press space to play again", love.graphics.getWidth() / 2, (love.graphics.getHeight() / 2) + 30
|
"Press space to play again",
|
||||||
)
|
love.graphics.getWidth() / 2,
|
||||||
love.graphics.setColor(255, 255, 255, 255) -- white?
|
(love.graphics.getHeight() / 2) + 30
|
||||||
love.graphics.draw(Assets.jirka,
|
)
|
||||||
(love.graphics.getWidth() / 2) - 150,
|
love.graphics.setColor(255, 255, 255, 255) -- white?
|
||||||
(love.graphics.getHeight() / 2) - 70)
|
love.graphics.draw(Assets.jirka, (love.graphics.getWidth() / 2) - 150, (love.graphics.getHeight() / 2) - 70)
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
love.graphics.setColor(1, 1, 1, 1) -- white
|
love.graphics.setColor(1, 1, 1, 1) -- white
|
||||||
love.graphics.clear()
|
love.graphics.clear()
|
||||||
|
|
||||||
love.graphics.print("Score: " .. tostring(Game.score), 10, 10)
|
love.graphics.print("Score: " .. tostring(Game.score), 10, 10)
|
||||||
|
|
||||||
love.graphics.rectangle("fill", Player.x, Player.y, Player.width, Player.height)
|
love.graphics.rectangle("fill", Player.x, Player.y, Player.width, Player.height)
|
||||||
|
|
||||||
-- draw the ball
|
-- draw the ball
|
||||||
love.graphics.circle("fill", Game.ball.position.x, Game.ball.position.y, Game.ball.radius)
|
love.graphics.circle("fill", Game.ball.position.x, Game.ball.position.y, Game.ball.radius)
|
||||||
|
|
||||||
-- draw the blocks
|
-- draw the blocks
|
||||||
for _, block in ipairs(Game.blocks) do
|
for _, block in ipairs(Game.blocks) do
|
||||||
if block.active then
|
if block.active then
|
||||||
love.graphics.setColor(block.color.r, block.color.g, block.color.b)
|
love.graphics.setColor(block.color.r, block.color.g, block.color.b)
|
||||||
love.graphics.rectangle("fill", block.x, block.y, block.width, block.height, 4, 4)
|
love.graphics.rectangle("fill", block.x, block.y, block.width, block.height, 4, 4)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- INPUT HANDLING
|
-- INPUT HANDLING
|
||||||
|
|
||||||
function love.keypressed(key)
|
function love.keypressed(key)
|
||||||
if key == "left" or key == "a" then
|
if key == "left" or key == "a" then
|
||||||
Player.moving_left = true
|
Player.moving_left = true
|
||||||
end
|
end
|
||||||
if key == "right" or key == "d" then
|
if key == "right" or key == "d" then
|
||||||
Player.moving_right = true
|
Player.moving_right = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.keyreleased(key)
|
function love.keyreleased(key)
|
||||||
if key == "left" or key == "a" then
|
if key == "left" or key == "a" then
|
||||||
Player.moving_left = false
|
Player.moving_left = false
|
||||||
end
|
end
|
||||||
if key == "right" or key == "d" then
|
if key == "right" or key == "d" then
|
||||||
Player.moving_right = false
|
Player.moving_right = false
|
||||||
end
|
end
|
||||||
|
|
||||||
if Game.is_playing == false and key == "space" then
|
if Game.is_playing == false and key == "space" then
|
||||||
resetGame()
|
resetGame()
|
||||||
Game.is_playing = true
|
Game.is_playing = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- UTILS
|
-- UTILS
|
||||||
|
|
||||||
function checkCollision(box, ball)
|
function checkCollision(box, ball)
|
||||||
local closestX = math.max(box.x, math.min(ball.position.x, box.x + box.width))
|
local closestX = math.max(box.x, math.min(ball.position.x, box.x + box.width))
|
||||||
local closestY = math.max(box.y, math.min(ball.position.y, box.y + box.height))
|
local closestY = math.max(box.y, math.min(ball.position.y, box.y + box.height))
|
||||||
|
|
||||||
local distanceX = ball.position.x - closestX
|
local distanceX = ball.position.x - closestX
|
||||||
local distanceY = ball.position.y - closestY
|
local distanceY = ball.position.y - closestY
|
||||||
|
|
||||||
local distanceSquared = (distanceX * distanceX) + (distanceY * distanceY)
|
local distanceSquared = (distanceX * distanceX) + (distanceY * distanceY)
|
||||||
|
|
||||||
return distanceSquared < (ball.radius * ball.radius)
|
return distanceSquared < (ball.radius * ball.radius)
|
||||||
end
|
end
|
||||||
|
|
||||||
function setupGame()
|
function setupGame()
|
||||||
-- place the player
|
-- place the player
|
||||||
Player.x = love.graphics.getWidth() / 2
|
Player.x = love.graphics.getWidth() / 2
|
||||||
Player.y = love.graphics.getHeight() - 30
|
Player.y = love.graphics.getHeight() - 30
|
||||||
|
|
||||||
-- place the ball
|
-- place the ball
|
||||||
local px, py = Player.getCenter()
|
local px, py = Player.getCenter()
|
||||||
Game.ball.position.x = px
|
Game.ball.position.x = px
|
||||||
Game.ball.position.y = py - Player.height
|
Game.ball.position.y = py - Player.height
|
||||||
Game.ball.velocity.dx = (love.math.random() - 0.5) * Settings.ball_speed
|
Game.ball.velocity.dx = (love.math.random() - 0.5) * Settings.ball_speed
|
||||||
Game.ball.velocity.dy = -Settings.ball_speed
|
Game.ball.velocity.dy = -Settings.ball_speed
|
||||||
|
|
||||||
-- Setup block variables
|
-- Setup block variables
|
||||||
local cur_x = Settings.block_offset_x
|
local cur_x = Settings.block_offset_x
|
||||||
local cur_y = Settings.block_offset_y
|
local cur_y = Settings.block_offset_y
|
||||||
local gap = Settings.block_gap
|
local gap = Settings.block_gap
|
||||||
local block_width = (love.graphics.getWidth() - cur_x * 2 - gap * (Settings.block_cols - 1)) / Settings.block_cols
|
local block_width = (love.graphics.getWidth() - cur_x * 2 - gap * (Settings.block_cols - 1)) / Settings.block_cols
|
||||||
-- Generate the actual blocks
|
-- Generate the actual blocks
|
||||||
for row = 1, Settings.block_rows do
|
for row = 1, Settings.block_rows do
|
||||||
for col = 1, Settings.block_cols do
|
for col = 1, Settings.block_cols do
|
||||||
table.insert(Game.blocks, {
|
table.insert(Game.blocks, {
|
||||||
x = cur_x,
|
x = cur_x,
|
||||||
y = cur_y,
|
y = cur_y,
|
||||||
width = block_width,
|
width = block_width,
|
||||||
height = Settings.block_height,
|
height = Settings.block_height,
|
||||||
color = {
|
color = {
|
||||||
r = math.max(love.math.random(), 0.2),
|
r = math.max(love.math.random(), 0.2),
|
||||||
g = math.max(love.math.random(), 0.2),
|
g = math.max(love.math.random(), 0.2),
|
||||||
b = math.max(love.math.random(), 0.2)
|
b = math.max(love.math.random(), 0.2),
|
||||||
},
|
},
|
||||||
active = true -- This is turned off when a box is hit
|
active = true, -- This is turned off when a box is hit
|
||||||
})
|
})
|
||||||
cur_x = cur_x + block_width + gap
|
cur_x = cur_x + block_width + gap
|
||||||
end
|
end
|
||||||
cur_y = cur_y + Settings.block_height + gap
|
cur_y = cur_y + Settings.block_height + gap
|
||||||
cur_x = Settings.block_offset_x
|
cur_x = Settings.block_offset_x
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function resetGame()
|
function resetGame()
|
||||||
-- Remove all blocks
|
-- Remove all blocks
|
||||||
for k in pairs(Game.blocks) do
|
for k in pairs(Game.blocks) do
|
||||||
Game.blocks[k] = nil
|
Game.blocks[k] = nil
|
||||||
end
|
end
|
||||||
-- reset score
|
-- reset score
|
||||||
Game.score = 0
|
Game.score = 0
|
||||||
|
|
||||||
-- Reload
|
-- Reload
|
||||||
setupGame()
|
setupGame()
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user