Choosing a colour only once

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.
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Choosing a colour only once

Post by Oblivion_123 »

Hi,

I am currently working on a case simulator type thing (from csgo). Yes, it's gonna look terrible but I'm doing it to improve on my Lua. I have made a function where it can choose a colour at random, to try and simulate the randomness of the selection. (This function will be worked on a bit more in the future to make it even more random). Although the function works, it randomly spams the function and causes it to jitter between the 2 current colours. So, in short, it's changing between red and blue constantly, is there a way that I can get it to select a colour randomly once and only once?

All help is much appreciated, thanks

Code: Select all


function pickAColour()
	reds = { 255, 0, 0 }
	blues = { 0, 0, 255 }
	Colours = { reds, blues }

	return Colours[ math.random(#Colours)]
end

function love.draw()

	love.graphics.setColor( pickAColour() )
	love.graphics.rectangle("fill", x+500, y, 75, 75 )

end

User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Choosing a colour only once

Post by MadByte »

Sure, make the colour table constant:

Code: Select all


function pickAColour()
   reds = { 255, 0, 0 }
   blues = { 0, 0, 255 }
   Colours = { reds, blues }

   return Colours[ math.random(#Colours)]
end

local colour = pickAColour()
function love.draw()

   love.graphics.setColor(colour)
   love.graphics.rectangle("fill", x+500, y, 75, 75 )

end
btw, you may need to unpack the table when applying the colour.
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Choosing a colour only once

Post by Oblivion_123 »

MadByte wrote:Sure, make the colour table constant:

Code: Select all


function pickAColour()
   reds = { 255, 0, 0 }
   blues = { 0, 0, 255 }
   Colours = { reds, blues }

   return Colours[ math.random(#Colours)]
end

local colour = pickAColour()
function love.draw()

   love.graphics.setColor(colour)
   love.graphics.rectangle("fill", x+500, y, 75, 75 )

end
btw, you may need to unpack the table when applying the colour.
This worked, thanks, but now the colours are all the same.
When I draw more than one box, they are all the exact same colour, even though there are multiple colours for it to choose from.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Choosing a colour only once

Post by MadByte »

You may want to use an array of boxes to manage that:

Code: Select all

local boxes ={}

function pickAColour()
   reds = { 255, 0, 0 }
   blues = { 0, 0, 255 }
   Colours = { reds, blues }
   return Colours[love.math.random(#Colours)]
end


function newBox(x, y)
  local box = {x = x, y = y, colour = pickAColour(), width = 75, height = 75}
  table.insert(boxes, box)
end


function drawBoxes()
  for k, box in ipairs(boxes) do
    love.graphics.setColor(box.colour)
    love.graphics.rectangle("fill", box.x, box.y, box.width, box.height)
    love.graphics.setColor(255, 255, 255, 255)
  end
end


function love.load()
  newBox(100, 100)
end


function love.draw()
  drawBoxes()
end
its untested btw.
edit: now it should work fine.
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Choosing a colour only once

Post by Oblivion_123 »

Unfortunately it didn't work. It doesn't draw a single box.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Choosing a colour only once

Post by MadByte »

I edited the post. You just have to "spawn" a box using the newBox function somewhere.
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Choosing a colour only once

Post by Oblivion_123 »

This works, but, now my spin function doesn't work. The spin function would make it so that it would make everything move 500 pixels to the left.

Code: Select all

        x = x - 500
Now, this doesn't work, they just stay there. Also, this is probably expected but, whenever you open up the game again, it stays the same colour and doesn't re-randomise it.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Choosing a colour only once

Post by zorg »

instead of math.random use love.math.random
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
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Choosing a colour only once

Post by MadByte »

This works, but, now my spin function doesn't work
Sure, the code I provided is just for what you ask for. Everything else is on you.
Also, this is probably expected but, whenever you open up the game again, it stays the same colour and doesn't re-randomise it.
That is wrong. when using the code as I provided above the colour get selected properly (tested). Make sure to use love.math.random instead of math.random. if you prefer to use math.random you need to create a random seed as well using math.randomseed(os.time()). the love.math module do that automatically.

Now to add additional functionality you just have to expend the code. if you're not sure how to do that, then you should take a deeper look into lua and löve itself. Here are some good resources to learn lua and LÖVE. :

Programming in Lua (Online Book)
LÖVE Tutorials

Here is an example for the wheel movement (but its not exactly what you need and its not perfect, its just an example):

Code: Select all



local screen_width, screen_height = love.graphics.getDimensions()
local wheel_velocity = 300
local boxes ={}

function pickAColour()
   reds = { 255, 0, 0 }
   blues = { 0, 0, 255 }
   Colours = { reds, blues }
   return Colours[ love.math.random(#Colours)]
end


function newBox(x, y)
  local box = {x = x, y = y, colour = pickAColour(), width = 60, height = 60}
  table.insert(boxes, box)
end


function updateBoxes(dt)
  for k, box in ipairs(boxes) do
    -- Move the box --
    box.x = box.x + wheel_velocity * dt
    
    -- Reset position if the box is outside of the screen --
    if box.x > screen_width then box.x = -box.width end
  end
end


function drawBoxes()
  for k, box in ipairs(boxes) do
    love.graphics.setColor(box.colour)
    love.graphics.rectangle("fill", box.x, box.y, box.width, box.height)
    love.graphics.setColor(255, 255, 255, 255)
  end
end


function love.load()
  for i = 1, 10 do
    newBox(-100 + 85 * i, 200)
  end
end


function love.update(dt)
  updateBoxes(dt)
end


function love.draw()
  drawBoxes()
end


function love.keypressed(key) if key == "escape" then love.event.quit() end end
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Choosing a colour only once

Post by Oblivion_123 »

Thank you very much. Without you I couldn't have done this. Your example was perfect and it explained everything that I needed to know.
Post Reply

Who is online

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