Hack Forums
[Lua] Any idea why this keeps going out of bounds LOVE2D - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Lua] Any idea why this keeps going out of bounds LOVE2D (/showthread.php?tid=4137338)



Any idea why this keeps going out of bounds LOVE2D - Toph Beifong - Mar 27, 2014

Basically i have an image which is repeated 5 times and i am trying to make it so they cant go out of the window but no matter what i try i just cant get it to work.

Any help:
This is my full code, originally it was going to be a Snake game but the smooth change in direction absolutly baffled me. So at the min it is just 5 bxes inline that more up,down, left, right.

PHP Code

<?php 
function love.load()
    
love.graphics.setBackgroundColor(000,000,000)
    
segmentImage = love.graphics.newImage('images/segment.png')
    
startingPositionX = 200
    startingPositionY 
= 200
    bodyLength 
= 5;
    
count = 0

    width 
= love.window.getWidth()
    
height = love.window.getHeight()

    --
load body segments image positions into table, call them later
    segmentControl 
= {}
    while 
count <= bodyLength do
        
startingPositionX = startingPositionX - 20
        table
.insert(segmentControl, {x=startingPositionX, y=startingPositionY})
        
count = count + 1
    end
end


--MOVEMENT FUNCTIONS


function rightMove()
    for 
seg = 1, bodyLength do
        
currentSegmentPosition_X = segmentControl[seg].x

        
if currentSegmentPosition_X <= 784 then
            segmentControl
[seg].x = currentSegmentPosition_X + 15
        end
    end
end


function downMove()
    for 
seg = 1, bodyLength do
        
currentSegmentPosition_Y = segmentControl[seg].y
        
if currentSegmentPosition_Y <= 550 then
            segmentControl
[seg].y = currentSegmentPosition_Y + 15
        end
    end
end

function upMove()
    for 
seg = 1, bodyLength do
        
currentSegmentPosition_Y = segmentControl[seg].y
        
if currentSegmentPosition_Y >= 15 then
            segmentControl
[seg].y = currentSegmentPosition_Y - 15
        end
    end
end

function leftMove()
    for 
seg = 1, bodyLength do
        
currentSegmentPosition_X = segmentControl[seg].x
        
if currentSegmentPosition_X >= 200 then
            segmentControl
[seg].x = currentSegmentPosition_X - 15
        end
    end
end



--END OF MOVEMENT



function love.draw()

    if 
love.keyboard.isDown("right") then
        rightMove
()
    
end

    
if love.keyboard.isDown("down") then
        downMove
()
    
end

    
if love.keyboard.isDown("up") then
        upMove
()
    
end

    
if love.keyboard.isDown("left") then
        leftMove
()
    
end

    
for i = 1, bodyLength do
        
love.graphics.draw(segmentImage, segmentControl[i].x, segmentControl[i].y)
    
end

    
if bodyLength == 0 then
        love
.graphics.setBackgroundColor(255, 0, 0)
    
end
end 



* - Aelustelin - Mar 27, 2014

What is broken? Just that its not bounded by the window or?

Just make it so it checks x and y against 0 and scrw or scrh (when applicable) every time a move happens. Then if it finds the move went out of bounds end the game.


* - .eXe - Mar 27, 2014

The way I do it in Corona is create 4 invisible objects (or images) that are on the outside walls, then have a function then when it comes in contact with the snake, it reverses the velocity to turn it around or kills it and ends the game.

Could you not do something like that?


* - Toph Beifong - Mar 27, 2014

(Mar 27, 2014 05:09 AM)Evil.eXe Wrote:  The way I do it in Corona is create 4 invisible objects (or images) that are on the outside walls, then have a function then when it comes in contact with the snake, it reverses the velocity to turn it around or kills it and ends the game.

Could you not do something like that?

Yer hadn't thought about using a system like that, so far i'd just been using a set width and is the end of the snake touched it it would stop


* - .eXe - Mar 27, 2014

(Mar 27, 2014 06:24 AM)Mr. P-teo Wrote:  Yer hadn't thought about using a system like that, so far i'd just been using a set width and is the end of the snake touched it it would stop

Give it go! Let me know if it works.