Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Lua 

Any idea why this keeps going out of bounds LOVE2D

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 
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.
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?
aint my sig design skills sick?
(Mar 27, 2014 05:09 AM).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
(Mar 27, 2014 06:24 AM)Toph Beifong 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.
aint my sig design skills sick?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Lua] by BinaryFoxMar 16, 2021 11:10 AM 3 370 Mar 16, 2021 11:10 AM
Last Post: CanadianBear
by KazzMar 11, 2016 08:29 PM 2 171 Mar 11, 2016 08:29 PM
Last Post: Mikeymkwii
[Lua] by Toph BeifongMar 26, 2014 02:11 AM 3 150 Mar 26, 2014 02:11 AM
Last Post: .eXe