problem when cheking if somethings true (making powerup)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
amisdatanomi
Prole
Posts: 2
Joined: Mon Mar 04, 2019 8:57 am

problem when cheking if somethings true (making powerup)

Post by amisdatanomi »

Hello!

I have problem and I don't understand what I'm doing wrong.

Code: Select all

				if snakeSegments[1].x == powerPosition.x
                and snakeSegments[1].y == powerPosition.y then
                    movePowerups()
					ispowerup = true
					end
					
					
				ispowerup == true
					if snakeSegments[1].x == powerPosition.x
					and snakeSegments[1].y == powerPosition.y then
		                    movePowerups()
					ispowerup = false
					end
code works when powerup is picked, speed of the player is increased tenfold, but when its picked up again (in different position) powerup is still active. I tried to make ispowerup == true check, but its not a valid argument for love. Would be interested to see what I'm doing wrong and why it doesn't work. Tried several other checkers, but they did nothing. Yes, im newb.
User avatar
keharriso
Citizen
Posts: 92
Joined: Fri Nov 16, 2012 9:34 pm

Re: problem when cheking if somethings true (making powerup)

Post by keharriso »

amisdatanomi wrote: Mon Mar 04, 2019 9:03 am Hello!

I have problem and I don't understand what I'm doing wrong.

-snip-

code works when powerup is picked, speed of the player is increased tenfold, but when its picked up again (in different position) powerup is still active. I tried to make ispowerup == true check, but its not a valid argument for love. Would be interested to see what I'm doing wrong and why it doesn't work. Tried several other checkers, but they did nothing. Yes, im newb.
So, just to be clear, what do you want to happen when the player picks up a powerup when they already have one active? It looks like you want the powerup to stop. In that case you want to make a complete if statement with ispowerup == true. Like this:

Code: Select all

if snakeSegments[1].x == powerPosition.x and snakeSegments[1].y == powerPosition.y then
	movePowerups()
	ispowerup = true
end

if ispowerup then
	if snakeSegments[1].x == powerPosition.x and snakeSegments[1].y == powerPosition.y then
		movePowerups()
		ispowerup = false
	end
end
EDIT: See Nelvin's answer below for the correct implementation.
Last edited by keharriso on Tue Mar 05, 2019 1:35 pm, edited 1 time in total.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
amisdatanomi
Prole
Posts: 2
Joined: Mon Mar 04, 2019 8:57 am

Re: problem when cheking if somethings true (making powerup)

Post by amisdatanomi »

Yes, thats should happen. Oddly enough my code is still broken after that addon. Looks like i have still lot to learn. I added the full code.

Code: Select all

function love.load()
    gridXCount = 40
    gridYCount = 30
	ispowerup = false


function movePowerups()
        local possiblePowerPositions = {}
        for powerX = 1, gridXCount do
            for powerY = 1, gridYCount do
                local possible = true

                for segmentIndex, segment in ipairs(snakeSegments) do
                    if powerX == segment.x and powerY == segment.y then
                        possible = false
                    end
                end

                if possible then
                    table.insert(possiblePowerPositions, {x = powerX, y = powerY})
                end
            end
        end
		
        powerPosition = possiblePowerPositions[love.math.random(1, #possiblePowerPositions)]
    end

		
    function moveFood()
        local possibleFoodPositions = {}

        for foodX = 1, gridXCount do
            for foodY = 1, gridYCount do
                local possible = true

                for segmentIndex, segment in ipairs(snakeSegments) do
                    if foodX == segment.x and foodY == segment.y then
                        possible = false
                    end
                end

                if possible then
                    table.insert(possibleFoodPositions, {x = foodX, y = foodY})
                end
            end
        end

        foodPosition = possibleFoodPositions[love.math.random(1, #possibleFoodPositions)]
    end

    function reset()
        snakeSegments = {
            {x = 3, y = 1},
            {x = 2, y = 1},
            {x = 1, y = 1},
        }
        directionQueue = {'right'}
        snakeAlive = true
        timer = 0
		powertimer = 0
        moveFood()
		movePowerups()
    end

    reset()
end

function love.update(dt)
    timer = timer + dt
	if ispowerup == true then
	timer = timer + dt - powertimer
	powertimer = powertimer - dt
	end
	
	if ispowerup == false then
	 timer = timer + dt
	end

    if snakeAlive then
        timerLimit = 0.15
        if timer >= timerLimit then
            timer = timer - timerLimit

            if #directionQueue > 1 then
                table.remove(directionQueue, 1)
            end

             nextXPosition = snakeSegments[1].x
             nextYPosition = snakeSegments[1].y

            if directionQueue[1] == 'right' then
                nextXPosition = nextXPosition + 1
                if nextXPosition > gridXCount then
                    nextXPosition = 1
                end
            elseif directionQueue[1] == 'left' then
                nextXPosition = nextXPosition - 1
                if nextXPosition < 1 then
                    nextXPosition = gridXCount
                end
            elseif directionQueue[1] == 'down' then
                nextYPosition = nextYPosition + 1
                if nextYPosition > gridYCount then
                    nextYPosition = 1
                end
            elseif directionQueue[1] == 'up' then
                nextYPosition = nextYPosition - 1
                if nextYPosition < 1 then
                    nextYPosition = gridYCount
                end
            end

            local canMove = true

            for segmentIndex, segment in ipairs(snakeSegments) do
                if segmentIndex ~= #snakeSegments
                and nextXPosition == segment.x 
                and nextYPosition == segment.y then
                    canMove = false
                end
            end

            if canMove then
                table.insert(snakeSegments, 1, {x = nextXPosition, y = nextYPosition})

                if snakeSegments[1].x == foodPosition.x
                and snakeSegments[1].y == foodPosition.y then
                    moveFood()
					
                else
                    table.remove(snakeSegments)
                end
				
				if snakeSegments[1].x == powerPosition.x and snakeSegments[1].y == powerPosition.y then
				movePowerups()
				ispowerup = true
				end

				if ispowerup then
				if snakeSegments[1].x == powerPosition.x and snakeSegments[1].y == powerPosition.y then
				ispowerup = false
				movePowerups()
				
				end
				end

					
            else
                snakeAlive = false
            end
        end
    elseif timer >= 2 then
        reset()
    end
end

					

  if ispowerup == true then
  powertimer = 0.07 / dt
  powertimer = powertimer - timer / dt
  end
  
   --[[ if ispowerup == false then
  powertimer = timer
  end]]

function love.draw()
    local cellSize = 15

    love.graphics.setColor(.28, .28, .28)
    love.graphics.rectangle(
        'fill',
        0,
        0,
        gridXCount * cellSize,
        gridYCount * cellSize
    )

    local function drawCell(x, y)
        love.graphics.rectangle(
            'fill',
            (x - 1) * cellSize,
            (y - 1) * cellSize,
            cellSize - 1,
            cellSize - 1
        )
    end

    for segmentIndex, segment in ipairs(snakeSegments) do
        if snakeAlive then
            love.graphics.setColor(.6, 1, .32)
        else
            love.graphics.setColor(.5, .5, .5)
        end
        drawCell(segment.x, segment.y)
    end

    love.graphics.setColor(1, .3, .3)
    drawCell(foodPosition.x, foodPosition.y)
	    love.graphics.setColor(1, .3, 255)
    drawCell(powerPosition.x, powerPosition.y)
end

function love.keypressed(key)
    if key == 'right'
    and directionQueue[#directionQueue] ~= 'right'
    and directionQueue[#directionQueue] ~= 'left' then
        table.insert(directionQueue, 'right')

    elseif key == 'left'
    and directionQueue[#directionQueue] ~= 'left'
    and directionQueue[#directionQueue] ~= 'right' then
        table.insert(directionQueue, 'left')

    elseif key == 'up'
    and directionQueue[#directionQueue] ~= 'up'
    and directionQueue[#directionQueue] ~= 'down' then
        table.insert(directionQueue, 'up')

    elseif key == 'down'
    and directionQueue[#directionQueue] ~= 'down'
    and directionQueue[#directionQueue] ~= 'up' then
        table.insert(directionQueue, 'down')
    end
end][/code
Nelvin
Party member
Posts: 124
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

Re: problem when cheking if somethings true (making powerup)

Post by Nelvin »

It's just a minor logic issue.
First thing you do, when you reach the powerup, is to activate and move it, whether it's currently active or not so the second codeblock to deactivate the powerup never has a chance to be executed. Removing a bit of redundancy in the code may help to make it simpler to find such cases.

Code: Select all

if snakeSegments[1].x == powerPosition.x and snakeSegments[1].y == powerPosition.y then
    movePowerups()
    if ispowerup then
        ispowerup = false
    else
        ispowerup = true
    end
end
Toggles can be implemented even simpler but it depends on what's more easy to read for you. F.i.

Code: Select all

if snakeSegments[1].x == powerPosition.x and snakeSegments[1].y == powerPosition.y then
    movePowerups()
    ispowerup = not ispowerup
end
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests