love:interacting_with_boundaries
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
love:interacting_with_boundaries [2021/10/05 02:52] – [Bounce] mithat | love:interacting_with_boundaries [2021/10/05 02:53] (current) – ["Clip"] mithat | ||
---|---|---|---|
Line 49: | Line 49: | ||
- Mithat Konar | - Mithat Konar | ||
-------------------------------------]]-- | -------------------------------------]]-- | ||
- | |||
- | ----------------------- | ||
- | -- Utility functions -- | ||
- | ----------------------- | ||
- | |||
- | --[[ Convert a boolean value to a string representation. ]]-- | ||
- | function booleanToStr(bool) | ||
- | if bool then | ||
- | return " | ||
- | end | ||
- | return " | ||
- | end | ||
-------------------- | -------------------- | ||
Line 80: | Line 68: | ||
--[[ Update values of game parameters. ]]-- | --[[ Update values of game parameters. ]]-- | ||
function love.update(dt) | function love.update(dt) | ||
- | -- -- wrap around | + | |
- | -- circleX = circleX + CIRCLE_SPEED * dt | + | circleX = circleX + CIRCLE_H_SPEED |
- | -- if circleX > (love.graphics.getWidth() + CIRCLE_SIZE) then | + | if circleX > (love.graphics.getWidth() - CIRCLE_SIZE) then |
- | -- circleX = -CIRCLE_SIZE | + | circleX = love.graphics.getWidth() - CIRCLE_SIZE |
- | -- end | + | |
- | + | ||
- | -- | + | |
- | -- | + | |
- | -- if circleX > (love.graphics.getWidth() - CIRCLE_SIZE) then | + | |
- | -- | + | |
- | -- end | + | |
- | + | ||
- | -- -- bounce | + | |
- | -- -- four possible cases: | + | |
- | -- -- moving right, not reached the edge -> keep moving right | + | |
- | -- -- moving right, reached the edge -> move left | + | |
- | -- -- moving left, not reached the edge -> keep moving left | + | |
- | -- -- moving left, reached the edge -> move right | + | |
- | -- if isMoveRight and circleX < (love.graphics.getWidth() - CIRCLE_SIZE) then -- moving right, not reached the edgee | + | |
- | -- isMoveRight = true | + | |
- | -- elseif isMoveRight and circleX >= (love.graphics.getWidth() - CIRCLE_SIZE) then -- moving right, reached the edge | + | |
- | -- isMoveRight = false | + | |
- | -- elseif not isMoveRight and circleX > (CIRCLE_SIZE) then -- moving left, not reached the edgee | + | |
- | -- isMoveRight = false | + | |
- | -- elseif not isMoveRight and circleX <= (CIRCLE_SIZE) then -- moving right, reached the edge | + | |
- | -- isMoveRight = true | + | |
- | -- end | + | |
- | + | ||
- | -- bounce improved | + | |
- | if isMoveRight then | + | |
- | if circleX < (love.graphics.getWidth() - CIRCLE_SIZE) then -- moving right, not reached the edgee | + | |
- | isMoveRight = true | + | |
- | else -- moving right, reached the edge | + | |
- | isMoveRight = false | + | |
- | end | + | |
- | else | + | |
- | if circleX > (CIRCLE_SIZE) then -- moving left, not reached the edgee | + | |
- | isMoveRight = false | + | |
- | else -- moving right, reached the edge | + | |
- | isMoveRight = true | + | |
- | end | + | |
end | end | ||
| | ||
- | if isMoveRight then | ||
- | circleX = circleX + CIRCLE_H_SPEED * dt | ||
- | else | ||
- | circleX = circleX - CIRCLE_H_SPEED * dt | ||
- | end | ||
end | end | ||
--[[ Render the game elements. ]]-- | --[[ Render the game elements. ]]-- | ||
function love.draw() | function love.draw() | ||
- | love.graphics.print(' | ||
love.graphics.circle(" | love.graphics.circle(" | ||
end | end |
love/interacting_with_boundaries.txt · Last modified: 2021/10/05 02:53 by mithat