2

I am trying to create a pause function in my CS50G fifty-bird problem set. However, the image I have to come up is behind the pipes and bird when it is rendered. I am new to using Love2d and Lua but I am not finding any documentation on how to move an image to the front when a key is pressed.

function PlayState:update(dt)
   
    if love.keyboard.wasPressed('p') then

        if self.pause then
            self.pause = false
            scrolling = true
            sounds['music']:play()
            sounds['pause']:pause()
        else
            self.pause = true
            scrolling = false
            sounds['music']:pause()
            sounds['pause']:play()

        end
        
    end

    if not self.pause then
   
    function PlayState:render()
        for k, pair in pairs(self.pipePairs) do
            pair:render()
        end
        if self.pause then
            love.graphics.draw(pause, 235, 100, center)
        end

        love.graphics.setFont(flappyFont)
        love.graphics.print('Score: ' .. tostring(self.score), 8, 8)
    
        self.bird:render()

I have messed around with the code a bit and tried moving the picture to main.lua but that did not work either, or I just did it wrong.

1
  • 1
    Did you try moving love.graphics.draw(pause, 235, 100, center) to the bottom of the render() function? Commented Oct 30, 2023 at 18:09

1 Answer 1

3

Converting my comment to an answer...

Drawing occurs in layers from bottom to top, so moving the pause drawing code after other entities you're drawing should place it in a layer above them:

-- ...
for k, pair in pairs(self.pipePairs) do
    pair:render()
end

love.graphics.setFont(flappyFont)
love.graphics.print('Score: ' .. tostring(self.score), 8, 8)

self.bird:render()

if self.pause then -- moved down to bottom
    love.graphics.draw(pause, 235, 100, center)
end
-- ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.