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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

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

Post 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
Last edited by nice on Wed Feb 22, 2017 8:57 pm, edited 2 times in total.
:awesome: Have a good day! :ultraglee:
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help: How to randomize background color

Post 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.
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.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help: How to randomize background color

Post by nice »

It works but is there a way to make it change color every frame?
:awesome: Have a good day! :ultraglee:
User avatar
joedono
Prole
Posts: 40
Joined: Mon Mar 04, 2013 6:58 pm

Re: Help: How to randomize background color

Post 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.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help: How to randomize background color

Post 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?
:awesome: Have a good day! :ultraglee:
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help: How to randomize background color every frame

Post 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
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.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help: How to randomize background color every frame

Post 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..
:awesome: Have a good day! :ultraglee:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 264 guests