love.graphics.print not working?

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
AngusWP
Prole
Posts: 9
Joined: Mon Jan 22, 2018 11:35 pm

love.graphics.print not working?

Post 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
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.graphics.print not working?

Post 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().
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot] and 3 guests