My fps is crashing game, but in other screen run fast

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
User avatar
Cauan
Prole
Posts: 22
Joined: Thu Mar 09, 2023 5:58 pm

My fps is crashing game, but in other screen run fast

Post by Cauan »

Hi, my fps is crashing my game and, i want to know why gives this?
This is code:

Code: Select all

require('variables')

local obj = {}

function addObj(x, y)
    table.insert(obj, {
        x = x,
        y = y,
        numeroaleatorio = math.random(20)
    })
end

function draw(y1, y2, x1, x2)
    love.graphics.draw(love.graphics.newImage( "imagens/spacial.png" ), x1, y1, 0, 0.05, 0.05)

        love.graphics.draw(love.graphics.newImage( "imagens/bomba.png" ), x2, y2, 0, 0.05, 0.05)
end

function checacolisao(x1, y1, w1, h1, x2, y2, w2, h2)
    return x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1
end

function love.load()
    love.graphics.setDefaultFilter("nearest", "nearest")
    
end

function love.update(dt)
    
    addObj(math.random(love.graphics.getWidth()), 0)
    
    for i, obj in ipairs(obj) do
        obj.y = obj.y + gravity
        if obj.y > w/2 then
            table.remove(obj, i)
        end
    end
    
    
    --if checacolisao()

    love.mouse.setVisible(true)
end

function love.draw()
    love.graphics.draw(love.graphics.newImage( "imagens/space.png" ), spaceX, spaceY, 0, 1, 1)
    love.graphics.print("pontos: " .. pontos .. "fps: " .. love.timer.getFPS(), 10, 10)
    
    for i = 1, #obj do
        if obj[i].numeroaleatorio == 8 then love.graphics.draw(love.graphics.newImage( "imagens/spacial.png" ), obj[i].x, obj[i].y, 0, 0.05, 0.05) else
        love.graphics.draw(love.graphics.newImage( "imagens/bomba.png" ), obj[i].x, obj[i].y, 0, 0.05, 0.05)
        end
    end
    
        
        love.graphics.draw(love.graphics.newImage( "imagens/enemy.png" ), posX, 400, 0, 0.2, 0.2, 90, 0)
end
I'm me, and you are you
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: My fps is crashing game, but in other screen run fast

Post by darkfrei »

Code: Select all

function fastRemove (list, index)
	list[index], list[#list] = list[#list], nil
end
Don't spam objects 60 time per second! Just add one after removing one.

Code: Select all

function love.update(dt)
    for i, obj in ipairs(objects) do
        obj.y = obj.y + gravity
        if obj.y > w/2 then
            fastRemove (objects, index)
            addObj(math.random(love.graphics.getWidth()), 0)
        end
    end
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Cauan
Prole
Posts: 22
Joined: Thu Mar 09, 2023 5:58 pm

Re: My fps is crashing game, but in other screen run fast

Post by Cauan »

ok, now the game starts at 1 fps, then jumps straight to 11 fps, then drops the fps every 10 seconds (not specifically) but the game starts pretty fast until :P :P :P :awesome: :crazy: :huh:
I'm me, and you are you
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: My fps is crashing game, but in other screen run fast

Post by BrotSagtMist »

Code: Select all

function love.draw()
    love.graphics.draw(love.graphics.newImage( "imagens/space.png" ), spaceX, spaceY, 0, 1, 1)
This, NEVER have newImage within a repeating function.
You are not showing the picture, you are CREATING it, then showing it. So every frame you have is a massive resource hog.
obey
User avatar
Cauan
Prole
Posts: 22
Joined: Thu Mar 09, 2023 5:58 pm

Re: My fps is crashing game, but in other screen run fast

Post by Cauan »

ok, i solved it and the code is

Code: Select all

require('variables')

local obj = {}

function addObj(x, y)
    table.insert(obj, {
        x = x,
        y = y,
        numeroaleatorio = math.random(20)
    })
end

function fastRemove (list, index)
	list[index], list[#list] = list[#list], nil
end

function draw(y1, y2, x1, x2)
    love.graphics.draw(love.graphics.newImage( "imagens/spacial.png" ), x1, y1, 0, 0.05, 0.05)

        love.graphics.draw(love.graphics.newImage( "imagens/bomba.png" ), x2, y2, 0, 0.05, 0.05)
end

function checacolisao(x1, y1, w1, h1, x2, y2, w2, h2)
    return x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1
end

function move(dt)
  if love.keyboard.isDown("right") then
    posXf = posXf + 70 * dt
  end 
  if love.keyboard.isDown("left") then
    posXf = posXf - 70 * dt
  end
end

function love.load()
    love.graphics.setDefaultFilter("nearest", "nearest")
    spacial = love.graphics.newImage( "imagens/spacial.png" )
    bomba = love.graphics.newImage( "imagens/bomba.png" )
    addObj(math.random(love.graphics.getWidth()), 0)
end

function love.update(dt)
    
for i, objects in ipairs(obj) do
        objects.y = objects.y + gravity
        if objects.y >= 390 then
            fastRemove (obj, i)
            addObj(math.random(love.graphics.getWidth()), 0)
        end
    end
    
    for i = 1, #obj do
    if checacolisao(posXf, h - 100, 0.2, 0.2, obj[i].x, obj[i].y, 0.05, 0.05) then
        if obj[i].numeroaleatorio == 8 or obj[i].numeroaleatorio == 5 or obj[i].numeroaleatorio == 15 or obj[i].numeroaleatorio == 18 or obj[i].numeroaleatorio == 9 or obj[i].numeroaleatorio == 1 or obj[i].numeroaleatorio == 20 then
       table.remove(collidedwith, i)
       pontos = pontos + 1
       if pontos >= 6 then win = true gameover = false end
       else 
           if not pontos <= 0 then pontos = pontos - 1 else gameover = true win = false end
           end
    end
    end
    love.mouse.setVisible(true)
    move(dt)
end

function love.draw()
    love.graphics.draw(love.graphics.newImage( "imagens/space.png" ), spaceX, spaceY, 0, 1, 1)
    love.graphics.print("pontos: " .. pontos .. "fps: " .. love.timer.getFPS(), 10, 10)
    
    for i = 1, #obj do
        if obj[i].numeroaleatorio == 8 or obj[i].numeroaleatorio == 5 or obj[i].numeroaleatorio == 15 or obj[i].numeroaleatorio == 18 or obj[i].numeroaleatorio == 9 or obj[i].numeroaleatorio == 1 or obj[i].numeroaleatorio == 20 then love.graphics.draw(spacial, obj[i].x, obj[i].y, 0, 0.05, 0.05) else
        love.graphics.draw(bomba, obj[i].x, obj[i].y, 0, 0.05, 0.05)
        end
    end
    
        
        love.graphics.draw(love.graphics.newImage( "imagens/enemy.png" ), posXf, h - 100, 0, 0.2, 0.2, 90, 0)
end
but the fps continue decreasing (from massive ways) :crazy: :crazy: :crazy: :o: :o: :o: :o: :o: :o: :o: :o: :o: :o: :o: :o: :o: :o: :o: :o: :o:
I'm me, and you are you
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: My fps is crashing game, but in other screen run fast

Post by BrotSagtMist »

Again: love.graphics.draw(love.graphics.newImage( "imagens/space.png" ), spaceX, spaceY, 0, 1, 1)
This line is madness.
obey
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: My fps is crashing game, but in other screen run fast

Post by knorke »

See https://love2d.org/wiki/love.graphics.newImage for an example of what Brot means.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests