From c866e889d785cab8752cf7bd64fdeba91a343332 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Sat, 22 Jul 2017 16:43:07 -0700 Subject: [PATCH] mike: changed col_detect to screen_col_detect; speed up ball; paddle now has collison detection and reversing ability --- love/mike/lib/functions.lua | 33 ++++++++++++++++++++++++++++++++- love/mike/lib/keys.lua | 2 +- love/mike/main.lua | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/love/mike/lib/functions.lua b/love/mike/lib/functions.lua index 88a1fec..191038b 100644 --- a/love/mike/lib/functions.lua +++ b/love/mike/lib/functions.lua @@ -1,4 +1,4 @@ -function col_detect(object, display) +function screen_col_detect(object, display) if not (object.x >= -1 and object.x <= (display.width - object.width)) then return false @@ -22,17 +22,48 @@ function create_object(start_x, start_y, width, height, speed) return object end +function num_is_pos(number) + if number > 0 then + return true + else return false + end +end + function ball_bounce(ball) + local paddle_hit = false ball.x = ball.x + ball.x_speed ball.y = ball.y + ball.y_speed + --If ball hits screen wall if ball.x >= screen.width or ball.x < 0 then ball.x_speed = ball.x_speed * -1 end + + --If ball hits screen top/bottom if ball.y >= screen.height or ball.y <0 then ball.y_speed = ball.y_speed * -1 end + + --If ball hits paddle + if ball.y + 1 >= player.y and + ball.x >= player.x and + ball.x <= player.x + player.width then + ball.y_speed = ball.y_speed *-1 + paddle_hit = true + end + + if paddle_hit == true then + if ball.x < player.x + (player.width/2) and + num_is_pos(ball.x_speed) then + ball.x_speed = ball.x_speed *-1 + elseif ball.x > player.x + (player.width/2) and + not num_is_pos(ball.x_speed) then + ball.x_speed = ball.x_speed *-1 + end + end + return ball end + diff --git a/love/mike/lib/keys.lua b/love/mike/lib/keys.lua index a172964..00a2503 100644 --- a/love/mike/lib/keys.lua +++ b/love/mike/lib/keys.lua @@ -57,7 +57,7 @@ function check_keys(object) -- newObject.y = newObject.y + newObject.speed -- end - if col_detect(newObject, screen) then + if screen_col_detect(newObject, screen) then return newObject else return object diff --git a/love/mike/main.lua b/love/mike/main.lua index e3bf0e5..903b6c1 100644 --- a/love/mike/main.lua +++ b/love/mike/main.lua @@ -12,7 +12,7 @@ function love.load() -- Create a table to hold all the player data player = create_object(screen.width /2, screen.height - 50, 25, 5, 10) -- Create a table to hold ball data - ball = create_object(screen.width /2, screen.height /2, 1, 1, 5) + ball = create_object(screen.width /2, screen.height /2, 1, 1, 10) ball.x_speed = ball.speed /2 ball.y_speed = ball.speed /2