Page 1 of 1

[SOLVED]Help: How to randomize background color every frame

Posted: Wed Feb 22, 2017 7:30 pm
by nice
So I familiarizing myself with Löve2D and lua again and right now I have a small project where I want to randomize the background color (for fun) and this is what I currently have:

Code: Select all

function love.load()
	cake = love.graphics.newImage("cake.png")
	pos1 = 400
	pos2 = 400
	rad = 0
	scaleX = 1
	scaleY = 1
	originX = 120
	originY = 104
end

function love.draw()
    love.graphics.print("Hello World", 370, 200)
    love.graphics.setBackgroundColor()
    love.graphics.draw(cake, pos1, pos2, rad, scaleX, scaleY, originX, originY)
end
I have a vague memory that it were fairly simple but I can't remember how it's supposed to be..

[Edit] added two more words to the title to better explain what I want

Re: Help: How to randomize background color

Posted: Wed Feb 22, 2017 7:36 pm
by zorg
Put backgroundcolor = {love.math.random(0,255),love.math.random(0,255),love.math.random(0,255)} in love.load, and pass backgroundolor into love.graphics.setBackgroundColor as a parameter.

Re: Help: How to randomize background color

Posted: Wed Feb 22, 2017 7:46 pm
by nice
It works but is there a way to make it change color every frame?

Re: Help: How to randomize background color

Posted: Wed Feb 22, 2017 8:21 pm
by joedono
love.draw() is called every frame. If you put the code that zorg gave you in there, it should produce the seizure-inducing effect you want.

Re: Help: How to randomize background color

Posted: Wed Feb 22, 2017 8:29 pm
by nice
joedono wrote: Wed Feb 22, 2017 8:21 pm love.draw() is called every frame. If you put the code that zorg gave you in there, it should produce the seizure-inducing effect you want.
Well then I must be missing something because right now it only changes color once when I run the program the first time:

Code: Select all

function love.load()
	
	-- Cake stuff 
	cake = love.graphics.newImage("cake.png")
	pos1 = 400
	pos2 = 400
	rad = 0
	scaleX = 1
	scaleY = 1
	originX = 120
	originY = 104

	-- Background Stuff
	-- randomBackgroundColor = {love.math.random(0, 255), love.math.random(0, 255), love.math.random(0, 255)}
	backgroundcolor = {love.math.random(0,255),love.math.random(0,255),love.math.random(0,255)}
end

function love.draw()
    love.graphics.print("Hello World", 370, 200)
    love.graphics.setBackgroundColor(backgroundcolor)
    love.graphics.draw(cake, pos1, pos2, rad, scaleX, scaleY, originX, originY)
end
Am I misunderstanding something?

Re: Help: How to randomize background color every frame

Posted: Wed Feb 22, 2017 8:33 pm
by zorg
Yes, major comprehension failure, in fact. :3 don't take it too hard though, it happens

As he said, you want to put both parts of my code into your love.draw function.

Code: Select all

function love.load()
	
	-- Cake stuff 
	cake = love.graphics.newImage("cake.png")
	pos1 = 400
	pos2 = 400
	rad = 0
	scaleX = 1
	scaleY = 1
	originX = 120
	originY = 104
end

function love.draw()
	-- Background Stuff
	-- also, it doesn't really matter what you call the variable.
	backgroundcolor = {love.math.random(0,255),love.math.random(0,255),love.math.random(0,255)}
    love.graphics.print("Hello World", 370, 200)
    love.graphics.setBackgroundColor(backgroundcolor)
    love.graphics.draw(cake, pos1, pos2, rad, scaleX, scaleY, originX, originY)
end

Re: Help: How to randomize background color every frame

Posted: Wed Feb 22, 2017 8:39 pm
by nice
zorg wrote: Wed Feb 22, 2017 8:33 pm Yes, major comprehension failure, in fact. :3 don't take it too hard though, it happens

As he said, you want to put both parts of my code into your love.draw function.
So I didn't understand that you were supposed to put both parts of the code inside the same function:

Code: Select all

function love.draw()
	-- Background Stuff
    backgroundcolor = {love.math.random(0,255),love.math.random(0,255),love.math.random(0,255)} -- Here
    love.graphics.print("Hello World", 370, 200)
    love.graphics.setBackgroundColor(backgroundcolor) -- and here
    love.graphics.draw(cake, pos1, pos2, rad, scaleX, scaleY, originX, originY)
end
I really need to learn to read more carefully..