Love:Mike: cleaned up formatting

This commit is contained in:
Logen Kain 2017-08-10 10:59:40 -07:00
parent ea0bf0a56c
commit 23bbad4f2b
4 changed files with 12 additions and 31 deletions

View File

@ -6,7 +6,6 @@ function GameState.save(tbl)
return saved_table return saved_table
end end
function GameState.load(SAVE_FILE) function GameState.load(SAVE_FILE)
-- placeholder is the name of our placeholder variable -- placeholder is the name of our placeholder variable
-- so we can do things like "actors = GameState.load" -- so we can do things like "actors = GameState.load"
@ -49,7 +48,7 @@ function print_table(tbl, table_name, only_once)
return saved_table return saved_table
end end
--Counts contents of tables of tables as well -- Counts contents of tables of tables as well
function tablelength(tbl, count) function tablelength(tbl, count)
local count = count or 0 local count = count or 0

View File

@ -3,18 +3,13 @@ function screen_col_detect(object, display)
object.x <= (display.width - object.width)) then object.x <= (display.width - object.width)) then
return false return false
end end
if not (object.y >= -1 and if not (object.y >= -1 and
object.y <= (display.height - object.height)) then object.y <= (display.height - object.height)) then
return false return false
end end
return true return true
end end
function create_object(start_x, start_y, width, height, speed) function create_object(start_x, start_y, width, height, speed)
local object = {} local object = {}
object.x = start_x object.x = start_x
@ -32,10 +27,8 @@ function num_is_pos(number)
end end
end end
-- Inspired by https://love2d.org/wiki/BoundingBox.lua -- Inspired by https://love2d.org/wiki/BoundingBox.lua
-- Returns true if boxes overlap -- Returns true if boxes overlap
function hitbox_collision(x1, y1, width1, height1, function hitbox_collision(x1, y1, width1, height1,
x2, y2, width2, height2) x2, y2, width2, height2)
return x1 < x2 + width2 and return x1 < x2 + width2 and
@ -44,13 +37,11 @@ function hitbox_collision(x1, y1, width1, height1,
y2 < y1 + height1 y2 < y1 + height1
end end
-- Takes a string "left" or "right"
--Takes a string "left" or "right"
function bounce(str, ball) function bounce(str, ball)
if str == "right" and if str == "right" and
not num_is_pos(ball.x_speed) then not num_is_pos(ball.x_speed) then
ball.x_speed = ball.x_speed *-1 ball.x_speed = ball.x_speed *-1
elseif elseif
str == "left" and str == "left" and
num_is_pos(ball.x_speed) then num_is_pos(ball.x_speed) then
@ -59,35 +50,29 @@ function bounce(str, ball)
return ball return ball
end end
function ball_bounce(player, ball, dt) function ball_bounce(player, ball, dt)
local paddle_hit = false local paddle_hit = false
ball.x = ball.x + (ball.x_speed *dt) ball.x = ball.x + (ball.x_speed *dt)
ball.y = ball.y + (ball.y_speed *dt) ball.y = ball.y + (ball.y_speed *dt)
--If ball hits screen wall -- If ball hits screen wall
if ball.x >= screen.width or ball.x < 0 then if ball.x >= screen.width or ball.x < 0 then
ball.x_speed = ball.x_speed * -1 ball.x_speed = ball.x_speed * -1
end end
--If ball hits screen top -- If ball hits screen top
if ball.y >= screen.height then if ball.y >= screen.height then
ball.y_speed = ball.y_speed * -1 ball.y_speed = ball.y_speed * -1
ball.score = ball.score + 1 ball.score = ball.score + 1
end end
--If ball hits screen bottom -- If ball hits screen bottom
if ball.y <= 0 then if ball.y <= 0 then
ball.y_speed = ball.y_speed * -1 ball.y_speed = ball.y_speed * -1
end end
-- If ball hits paddle
--If ball hits paddle
if hitbox_collision(ball.x, ball.y, ball.width, ball.height, if hitbox_collision(ball.x, ball.y, ball.width, ball.height,
player.x, player.y, player.width, player.height) then player.x, player.y, player.width, player.height) then
-- We don't need to check left limits because we already know -- We don't need to check left limits because we already know
-- there was a collision, so just bounce left if the ball is -- there was a collision, so just bounce left if the ball is

View File

@ -18,7 +18,6 @@ function love.keyreleased(key)
-- Load -- Load
elseif key == LOAD_KEY then elseif key == LOAD_KEY then
save_exists = love.filesystem.exists(SAVE_FILE) save_exists = love.filesystem.exists(SAVE_FILE)
if save_exists then if save_exists then
@ -47,11 +46,9 @@ function check_keys(object, screen, dt)
if love.keyboard.isDown(MOVE_RIGHT) then if love.keyboard.isDown(MOVE_RIGHT) then
newObject.x = newObject.x + half_speed(newObject.speed * dt) newObject.x = newObject.x + half_speed(newObject.speed * dt)
end end
if love.keyboard.isDown(MOVE_LEFT) then if love.keyboard.isDown(MOVE_LEFT) then
newObject.x = newObject.x - half_speed(newObject.speed * dt) newObject.x = newObject.x - half_speed(newObject.speed * dt)
end end
if screen_col_detect(newObject, screen) then if screen_col_detect(newObject, screen) then
return newObject return newObject
else else

View File

@ -18,7 +18,7 @@ function love.load()
actors.ball.y_speed = (-actors.ball.speed /2) actors.ball.y_speed = (-actors.ball.speed /2)
actors.ball.score = 0 actors.ball.score = 0
-- Timer vars (see keys.lua for loaded timer vars)
actors.start = love.timer.getTime() actors.start = love.timer.getTime()
actors.end_timer = 0 actors.end_timer = 0
end end
@ -30,21 +30,21 @@ function love.update(dt)
end end
function love.draw() function love.draw()
--Draw paddle -- Draw paddle
love.graphics.setColor(255,255,255) love.graphics.setColor(255,255,255)
love.graphics.rectangle("line", actors.player.x, actors.player.y, love.graphics.rectangle("line", actors.player.x, actors.player.y,
actors.player.width, actors.player.height) actors.player.width, actors.player.height)
--Draw ball -- Draw ball
love.graphics.setColor(255,0,0) love.graphics.setColor(255,0,0)
love.graphics.circle("line", actors.ball.x, actors.ball.y, love.graphics.circle("line", actors.ball.x, actors.ball.y,
actors.ball.width, actors.ball.height) actors.ball.width, actors.ball.height)
--FPS counter -- FPS counter
love.graphics.setColor(128,72,25) love.graphics.setColor(128,72,25)
love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 10, 10) love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 10, 10)
--Ball Score -- Ball Score
love.graphics.setColor(255,0,0) love.graphics.setColor(255,0,0)
love.graphics.print("Score: "..actors.ball.score, screen.width -80, 10) love.graphics.print("Score: "..actors.ball.score, screen.width -80, 10)
--Timer -- Timer
love.graphics.setColor(125,42,19) love.graphics.setColor(125,42,19)
love.graphics.print(string.format("Time M: %.2f", actors.end_timer/60), screen.width -85, 50) love.graphics.print(string.format("Time M: %.2f", actors.end_timer/60), screen.width -85, 50)
love.graphics.print(string.format("Time S: %.2f", actors.end_timer), screen.width -85, 30) love.graphics.print(string.format("Time S: %.2f", actors.end_timer), screen.width -85, 30)