mike: changed col_detect to screen_col_detect; speed up ball; paddle now has collison detection and reversing ability

This commit is contained in:
Logen Kain 2017-07-22 16:43:07 -07:00
parent 841c7a8706
commit c866e889d7
3 changed files with 34 additions and 3 deletions

View File

@ -1,4 +1,4 @@
function col_detect(object, display) function screen_col_detect(object, display)
if not (object.x >= -1 and if not (object.x >= -1 and
object.x <= (display.width - object.width)) then object.x <= (display.width - object.width)) then
return false return false
@ -22,17 +22,48 @@ function create_object(start_x, start_y, width, height, speed)
return object return object
end end
function num_is_pos(number)
if number > 0 then
return true
else return false
end
end
function ball_bounce(ball) function ball_bounce(ball)
local paddle_hit = false
ball.x = ball.x + ball.x_speed ball.x = ball.x + ball.x_speed
ball.y = ball.y + ball.y_speed ball.y = ball.y + ball.y_speed
--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/bottom
if ball.y >= screen.height or ball.y <0 then if ball.y >= screen.height or 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.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 return ball
end end

View File

@ -57,7 +57,7 @@ function check_keys(object)
-- newObject.y = newObject.y + newObject.speed -- newObject.y = newObject.y + newObject.speed
-- end -- end
if col_detect(newObject, screen) then if screen_col_detect(newObject, screen) then
return newObject return newObject
else else
return object return object

View File

@ -12,7 +12,7 @@ function love.load()
-- Create a table to hold all the player data -- Create a table to hold all the player data
player = create_object(screen.width /2, screen.height - 50, 25, 5, 10) player = create_object(screen.width /2, screen.height - 50, 25, 5, 10)
-- Create a table to hold ball data -- 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.x_speed = ball.speed /2
ball.y_speed = ball.speed /2 ball.y_speed = ball.speed /2