mike: Added function to create object; created bouncing ball
This commit is contained in:
parent
3953d079d6
commit
841c7a8706
@ -12,3 +12,27 @@ function col_detect(object, display)
|
|||||||
return true
|
return true
|
||||||
end
|
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
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,20 +10,21 @@ function love.load()
|
|||||||
screen.height = love.graphics.getHeight()
|
screen.height = love.graphics.getHeight()
|
||||||
|
|
||||||
-- Create a table to hold all the player data
|
-- Create a table to hold all the player data
|
||||||
player = {}
|
player = create_object(screen.width /2, screen.height - 50, 25, 5, 10)
|
||||||
player.x = screen.width /2
|
-- Create a table to hold ball data
|
||||||
player.y = screen.height - 50
|
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
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
player = check_keys(player)
|
player = check_keys(player)
|
||||||
|
ball = ball_bounce(ball)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
love.graphics.rectangle("line", player.x, player.y, player.width, player.height)
|
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user