mike: changed col_detect to screen_col_detect; speed up ball; paddle now has collison detection and reversing ability
This commit is contained in:
parent
841c7a8706
commit
c866e889d7
@ -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
|
||||
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user