diff --git a/love/mike/lib/functions.lua b/love/mike/lib/functions.lua index e71878b..88a1fec 100644 --- a/love/mike/lib/functions.lua +++ b/love/mike/lib/functions.lua @@ -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 + + diff --git a/love/mike/main.lua b/love/mike/main.lua index 196034a..e3bf0e5 100644 --- a/love/mike/main.lua +++ b/love/mike/main.lua @@ -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