Page 1 of 1

love.graphics.print not working?

Posted: Wed Oct 09, 2019 5:43 pm
by AngusWP
Hi, I am trying to print the score to the screen but it doesn't seem to be working. I start using love2d again recently when the new update came out, and this used to work for me.

This is the whole .lua file, and the print line is right at the bottom so scroll all the way down.

Code: Select all

function love.load()
    width = 400;
    height = 488;
  
    love.window.setMode(width, height);
    love.window.setTitle("Floppy Box");
    
    birdX = 50;
    birdY = 220;
    gravity = 0;
    
    difficulty = "easy";
    score = 0;
  
end

function spawnAsteroid()
    
end

function love.keypressed(key) 
     if (key == "space") then
        jump();
     end
end

function jump()
    if not (birdY < 25) then
      gravity = -160;      
    end
end

function love.update(dt)
    gravity = gravity + (dt * 350)
    birdY = birdY + (gravity * dt);
    
    
    if (love.keyboard.isDown("escape")) then
        love.event.quit();
    end
end

function love.mousepressed(x, y, button, pressed) 
      if (button == 1) then
          jump();
      end
end

function love.draw()
      love.graphics.setColor(.14, .36, .46);
      love.graphics.rectangle("fill", 0, 0, width, height);
      -- background
      
      love.graphics.print("Score: ".. score, 50, 50);
      
      love.graphics.setColor(.87, .84, .27);
      love.graphics.rectangle("fill", birdX, birdY, 30, 25);
end

Re: love.graphics.print not working?

Posted: Wed Oct 09, 2019 7:42 pm
by pgimeno
You're printing the text with the same colour as the background. Use another love.graphics.setColor right before printing the text, with the desired text colour (e.g. 1,1,1 for white).

Note also that it's probably more efficient to use love.graphics.setBackgroundColor than drawing a rectangle. Preferably, use setBackgroundColor before love.draw, otherwise you'll miss the first frame. That's because it affects the colour used by love.graphics.clear(), and the system calls love.graphics.clear() right before calling love.draw().