Background image. I'm stuck, need help

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Lexeres
Prole
Posts: 5
Joined: Fri Jun 25, 2021 6:58 am

Background image. I'm stuck, need help

Post by Lexeres »

I have functions:

Code: Select all

function love.draw()

    for n, myButton in ipairs (buttons) do
        myButton:draw()
    end
    
end

Function code of  myButton:draw() :

    function myButton:draw()
        
        if self.isHover then
        love.graphics.setColor(self.HoverColor)
            if  currentStateMouse == true then
                love.graphics.setColor(self.chooseColor)
            end
            else 
                love.graphics.setColor(self.color)
            end
            
            love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
            love.graphics.setColor(0,0,0)
            love.graphics.print(self.text,self.x,self.y+2)
            
        end
    
    return myButton
end

function myButton:update(dt)

        mouse = {}
        mouse.x,mouse.y = love.mouse.getPosition()  

        currentStateMouse = love.mouse.isDown(1)

        self.isHover = mouse.x >= self.x and mouse.x <= (self.x + self.width) and
                                    mouse.y >= self.y and mouse.y <= (self.y + self.height)
                                    
        if self.isHover and currentStateMouse == true
                        and self.oldStateButton == false
                then   
                                
                    print("It's a button - ", p.text)
                    love.graphics.setColor(self.chooseColor)

                    if p.text=="Exit" then -- выход из игры по кнопке
                        love.quit()
                    end   

                    if p.text=="New Game" then -- Начало игры
                        print("LOADING...")
                        love.game()
                    end 

                    if p.text=="Options" then -- Настройки
                        print("Settings")
                        
                    end 
        end 
        self.oldStateButton = currentStateMouse   
    end
In love.Load I added backgroundofMenu = love.graphics.newImage("/graphics/123.bmp").
When I add code love.graphics.draw(backgroundofMenu,0,0) in "love.draw()" before cycle - image shows only first frame. If I add love.graphics.draw(backgroundofMenu,0,0) in update-function - Nothing happens.
If I add in myButton:draw() before all the code - all menu buttons are no longer displayed except exit.

How can I add the background image?
Last edited by Lexeres on Sat Jun 26, 2021 12:00 pm, edited 2 times in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Background image. I'm stuck, need help

Post by zorg »

this should work:

Code: Select all

function love.draw()
  love.graphics.draw(backgroundofMenu,0,0)
  for n, myButton in ipairs (buttons) do
    myButton:draw()
  end
end
though your code is riddled with globals that i'm assuming you wanted to create as instance variables.
also do use code tags next time
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Lexeres
Prole
Posts: 5
Joined: Fri Jun 25, 2021 6:58 am

Re: Background image. I'm stuck, need help

Post by Lexeres »

zorg wrote: Fri Jun 25, 2021 1:33 pm this should work:

Code: Select all

function love.draw()
  love.graphics.draw(backgroundofMenu,0,0)
  for n, myButton in ipairs (buttons) do
    myButton:draw()
  end
end
though your code is riddled with globals that i'm assuming you wanted to create as instance variables.
also do use code tags next time
I wrote what "When I add code love.graphics.draw(backgroundofMenu,0,0) in "love.draw()" before cycle - image shows only first frame."
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Background image. I'm stuck, need help

Post by zorg »

Then you're doing something else wrong; upload your project or show more code... this is just a guess since i can't know, but you might be redefining love.draw elsewhere.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Lexeres
Prole
Posts: 5
Joined: Fri Jun 25, 2021 6:58 am

Re: Background image. I'm stuck, need help

Post by Lexeres »

zorg wrote: Sun Jun 27, 2021 7:12 am Then you're doing something else wrong; upload your project or show more code... this is just a guess since i can't know, but you might be redefining love.draw elsewhere.
Added attach, my test project.
I think what cycle of buttons somehow overlaps my background picture, but i dont know how to fix it :(
Last edited by Lexeres on Wed Jun 30, 2021 7:03 am, edited 1 time in total.
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Background image. I'm stuck, need help

Post by GVovkiv »

Code: Select all

function love.draw()

    love.graphics.setColor(1, 1, 1, 1) -- you forget somewhere to change colours

    love.graphics.draw(mainMenuPicture) --IT DOESN'T WORK______________________________!!!!!!!!!!!!!!

    love.audio.setVolume(soundVolume)
    love.audio.play(mainManuMp3)

    love.graphics.setFont(sizeFont)

    for n, myButton in ipairs (buttons) do
        myButton:draw()
    end
    
    if love.keyboard.isDown("q") then 
    love.quit()
    end

end
So, i kinda have hard times reading this code, but i guess problem somewhere in buttons draw function
Probably, buttons draw function overwrite colours for "is hover" effect, but don't reset it to default one
Lexeres
Prole
Posts: 5
Joined: Fri Jun 25, 2021 6:58 am

Re: Background image. I'm stuck, need help

Post by Lexeres »

GVovkiv wrote: Sun Jun 27, 2021 3:55 pm

Code: Select all

function love.draw()

    love.graphics.setColor(1, 1, 1, 1) -- you forget somewhere to change colours

    love.graphics.draw(mainMenuPicture) --IT DOESN'T WORK______________________________!!!!!!!!!!!!!!

    love.audio.setVolume(soundVolume)
    love.audio.play(mainManuMp3)

    love.graphics.setFont(sizeFont)

    for n, myButton in ipairs (buttons) do
        myButton:draw()
    end
    
    if love.keyboard.isDown("q") then 
    love.quit()
    end

end
So, i kinda have hard times reading this code, but i guess problem somewhere in buttons draw function
Probably, buttons draw function overwrite colours for "is hover" effect, but don't reset it to default one
love.graphics.setColor(1, 1, 1, 1) -- you forget somewhere to change colours
look at MainMenu.lua, all the colors set here:
...
color = p.color or {0.5,0.5,0.5,1},
HoverColor = p.HoverColor or {0.8,0.8,0.8,1},
chooseColor = p.chooseColor or {1,1,1,1},
...

I know what the problem is in Draw and cycle, but how to fix - i don't have any ideas((
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Background image. I'm stuck, need help

Post by GVovkiv »

I know what the problem is in Draw and cycle, but how to fix - i don't have any ideas((

Code: Select all

    function myButton:draw()
        local r, g, b, a = love.graphics.getColor() -- before you draw buttons, you should get old colors (and alpha)
        volumeInPercent = soundVolume * 100
        if p.text=="Sound" and volumeInPercent > 0 then 
            self.text = "Sound: " .. soundVolume
        
            if soundVolume > 1.05 then
                print("Sound: OFF")
            else
                self.text = "Sound: " .. volumeInPercent
                print("Sound:" .. volumeInPercent)
            end
        end
            if self.isHover then
                love.graphics.setColor(self.HoverColor)
                    if  currentStateMouse == true then
                        love.graphics.setColor(self.chooseColor)
                    end
            else 
                love.graphics.setColor(self.color)
            end

            love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
            love.graphics.setColor(r, g, b, a) -- and after you done, return old colours
            love.graphics.print(self.text,self.x,self.y+2)
            
    end
Image
Lexeres
Prole
Posts: 5
Joined: Fri Jun 25, 2021 6:58 am

Re: Background image. I'm stuck, need help

Post by Lexeres »

GVovkiv wrote: Mon Jun 28, 2021 8:26 am
I know what the problem is in Draw and cycle, but how to fix - i don't have any ideas((

Code: Select all

    function myButton:draw()
        local r, g, b, a = love.graphics.getColor() -- before you draw buttons, you should get old colors (and alpha)
        volumeInPercent = soundVolume * 100
        if p.text=="Sound" and volumeInPercent > 0 then 
            self.text = "Sound: " .. soundVolume
        
            if soundVolume > 1.05 then
                print("Sound: OFF")
            else
                self.text = "Sound: " .. volumeInPercent
                print("Sound:" .. volumeInPercent)
            end
        end
            if self.isHover then
                love.graphics.setColor(self.HoverColor)
                    if  currentStateMouse == true then
                        love.graphics.setColor(self.chooseColor)
                    end
            else 
                love.graphics.setColor(self.color)
            end

            love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
            love.graphics.setColor(r, g, b, a) -- and after you done, return old colours
            love.graphics.print(self.text,self.x,self.y+2)
            
    end
Image
Thank you very much! It works :D
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests