Last active
October 5, 2020 21:40
-
-
Save davidsharp/cd9a3baee407b8241e4ae4f912adfac1 to your computer and use it in GitHub Desktop.
quick dumb lua/love2d script, keyboard and mouse controls and a bouncy ball
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- built for love | |
| ball = { | |
| x = 400, | |
| y = 100, | |
| r = 25 | |
| } | |
| paddle = { | |
| w = 100, | |
| h = 20, | |
| y = 500, | |
| x = 400 | |
| } | |
| bounce = 0 | |
| momentum = 0 | |
| function love.update(dt) | |
| if love.keyboard.isDown("left") then | |
| paddle.x = paddle.x - 200 * dt | |
| end | |
| if love.keyboard.isDown("right") then | |
| paddle.x = paddle.x + 200 * dt | |
| end | |
| if mx~=love.mouse.getX() then | |
| mx = love.mouse.getX() | |
| paddle.x = mx | |
| end | |
| if(bounce > 0)then | |
| ball.y = ball.y - (200 * dt * bounce) | |
| bounce = bounce - .3 * dt | |
| elseif( | |
| ball.y+ball.r>paddle.y | |
| and ball.y+ball.r<(paddle.y+paddle.h) | |
| and ball.x+ball.r>paddle.x | |
| and ball.x+ball.r<(paddle.x+paddle.w) | |
| )then | |
| bounce = 1.2 | |
| momentum = 1 | |
| elseif(ball.y > 550)then | |
| ball.x, ball.y = 400, 100 | |
| else | |
| ball.y = ball.y + (200 * dt * (1 - momentum)) | |
| if(momentum>0)then momentum = momentum - .3 * dt end | |
| end | |
| end | |
| function love.draw() | |
| love.graphics.circle( 'fill', ball.x, ball.y, ball.r ) | |
| love.graphics.rectangle( 'fill', paddle.x, paddle.y, paddle.w, paddle.h ) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment