====== Interacting with boundaries ====== ===== Wrap around ===== --[[------------------------------------- - Interacting with screen boundaries. - Mithat Konar -------------------------------------]]-- -------------------- -- LÖVE functions -- -------------------- a --[[ Create game globals and initial conditions. ]]-- function love.load() -- circle's constants CIRCLE_SIZE = 30 CIRCLE_H_SPEED = 100 -- circle's variables circleX = CIRCLE_SIZE -- put circle in upper left of screen circleY = CIRCLE_SIZE -- put circle in upper left of screen end --[[ Update values of game parameters. ]]-- function love.update(dt) -- wrap around circleX = circleX + CIRCLE_H_SPEED * dt -- if cicle is at or beyone the right edge, move it back to the left edge if circleX > (love.graphics.getWidth() + CIRCLE_SIZE) then circleX = -CIRCLE_SIZE end end --[[ Render the game elements. ]]-- function love.draw() love.graphics.circle("fill", circleX, circleY, CIRCLE_SIZE) end ===== "Clip" ===== --[[------------------------------------- - Interacting with screen boundaries. - Mithat Konar -------------------------------------]]-- -------------------- -- LÖVE functions -- -------------------- --[[ Create game globals and initial conditions. ]]-- function love.load() -- circle's constants CIRCLE_SIZE = 30 CIRCLE_H_SPEED = 100 -- circle's variables circleX = CIRCLE_SIZE -- put circle in upper left of screen circleY = CIRCLE_SIZE -- put circle in upper left of screen isMoveRight = true -- whether the circle is moving right or left (only needed for bounce) end --[[ Update values of game parameters. ]]-- function love.update(dt) -- "clip" the motion circleX = circleX + CIRCLE_H_SPEED * dt if circleX > (love.graphics.getWidth() - CIRCLE_SIZE) then circleX = love.graphics.getWidth() - CIRCLE_SIZE end end --[[ Render the game elements. ]]-- function love.draw() love.graphics.circle("fill", circleX, circleY, CIRCLE_SIZE) end ===== Bounce ===== --[[------------------------------------- - Interacting with screen boundaries. - Mithat Konar -------------------------------------]]-- ----------------------- -- Utility functions -- ----------------------- --[[ Convert a boolean value to a string representation. ]]-- function booleanToStr(bool) if bool then return "true" else return "false" end end -------------------- -- LÖVE functions -- -------------------- --[[ Create game globals and initial conditions. ]]-- function love.load() -- circle's constants CIRCLE_SIZE = 30 CIRCLE_H_SPEED = 100 -- circle's variables circleX = CIRCLE_SIZE -- put circle in upper left of screen circleY = CIRCLE_SIZE -- put circle in upper left of screen isMoveRight = true -- whether the circle is moving right or left (only needed for bounce) end --[[ Update values of game parameters. ]]-- function love.update(dt) -- -- 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 -- bounce improved if isMoveRight then isMoveRight = circleX < (love.graphics.getWidth() - CIRCLE_SIZE) else isMoveRight = not (circleX > CIRCLE_SIZE) end if isMoveRight then circleX = circleX + CIRCLE_H_SPEED * dt else circleX = circleX - CIRCLE_H_SPEED * dt end end --[[ Render the game elements. ]]-- function love.draw() love.graphics.print('isMoveRight: ' .. booleanToStr(isMoveRight), 5,love.graphics.getHeight() - 24) love.graphics.circle("fill", circleX, circleY, CIRCLE_SIZE) end