mike: Added function to create object; created bouncing ball

This commit is contained in:
Logen Kain 2017-07-22 15:45:12 -07:00
parent 3953d079d6
commit 841c7a8706
2 changed files with 32 additions and 7 deletions

View File

@ -12,3 +12,27 @@ function col_detect(object, display)
return true
end
function create_object(start_x, start_y, width, height, speed)
local object = {}
object.x = start_x
object.y = start_y
object.width = width
object.height = height
object.speed = speed
return object
end
function ball_bounce(ball)
ball.x = ball.x + ball.x_speed
ball.y = ball.y + ball.y_speed
if ball.x >= screen.width or ball.x < 0 then
ball.x_speed = ball.x_speed * -1
end
if ball.y >= screen.height or ball.y <0 then
ball.y_speed = ball.y_speed * -1
end
return ball
end

View File

@ -10,20 +10,21 @@ function love.load()
screen.height = love.graphics.getHeight()
-- Create a table to hold all the player data
player = {}
player.x = screen.width /2
player.y = screen.height - 50
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.x_speed = ball.speed /2
ball.y_speed = ball.speed /2
player.width = 25
player.height = 5
player.speed = 10
end
function love.update(dt)
player = check_keys(player)
ball = ball_bounce(ball)
end
function love.draw()
love.graphics.rectangle("line", player.x, player.y, player.width, player.height)
love.graphics.rectangle("line", ball.x, ball.y, ball.width, ball.height)
end