User Tools

Site Tools


love:interacting_with_boundaries

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
love:interacting_with_boundaries [2021/10/05 02:46] – [Bounce] mithatlove:interacting_with_boundaries [2021/10/05 02:52] – [Bounce] mithat
Line 9: Line 9:
 - Mithat Konar - Mithat Konar
 -------------------------------------]]-- -------------------------------------]]--
- 
------------------------ 
--- Utility functions -- 
------------------------ 
- 
---[[ Convert a boolean value to a string representation. ]]-- 
-function booleanToStr(bool) 
-    if bool then 
-        return "true" 
-    end 
-    return "false" 
-end 
  
 -------------------- --------------------
 -- LÖVE functions -- -- LÖVE functions --
 -------------------- --------------------
 +a
 --[[ Create game globals and initial conditions. ]]-- --[[ Create game globals and initial conditions. ]]--
 function love.load() function love.load()
Line 35: Line 23:
     circleX = CIRCLE_SIZE   -- put circle in upper left of screen     circleX = CIRCLE_SIZE   -- put circle in upper left of screen
     circleY = 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 end
  
 --[[ Update values of game parameters. ]]-- --[[ Update values of game parameters. ]]--
 function love.update(dt) function love.update(dt)
---    -- wrap around +    -- wrap around 
---    circleX = circleX + CIRCLE_SPEED * dt +    circleX = circleX + CIRCLE_H_SPEED * dt 
---    if circleX > (love.graphics.getWidth() + CIRCLE_SIZE) then +    -- if cicle is at or beyone the right edge, move it back to the left edge 
---        circleX = -CIRCLE_SIZE +    if circleX > (love.graphics.getWidth() CIRCLE_SIZE) then 
---    end +        circleX = -CIRCLE_SIZE
-     +
---    -- "clip" the motion +
---    circleX = circleX + CIRCLE_SPEED * dt +
---    if circleX > (love.graphics.getWidth() - CIRCLE_SIZE) then +
---        circleX = love.graphics.getWidth() - CIRCLE_SIZE +
---    end +
-     +
---    -- bounce +
---    -- four possible cases: +
---    --  moving right, not reached the edge  -> keep moving right +
---    --  moving rightreached 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 +
-     +
-    if isMoveRight then +
-        circleX = circleX + CIRCLE_H_SPEED * dt +
-    else +
-        circleX = circleX - CIRCLE_H_SPEED * dt+
     end     end
 end end
Line 92: Line 37:
 --[[ Render the game elements. ]]-- --[[ Render the game elements. ]]--
 function love.draw() function love.draw()
-    love.graphics.print('isMoveRight: ' .. booleanToStr(isMoveRight), 5,love.graphics.getHeight() - 24) 
     love.graphics.circle("fill", circleX, circleY, CIRCLE_SIZE)     love.graphics.circle("fill", circleX, circleY, CIRCLE_SIZE)
 end end
Line 209: Line 153:
     if bool then     if bool then
         return "true"         return "true"
 +    else
 +        return "false"
     end     end
-    return "false" 
 end end
  
love/interacting_with_boundaries.txt · Last modified: 2021/10/05 02:53 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki