Page 1 of 2

Choosing a colour only once

Posted: Sat Nov 07, 2015 9:05 pm
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


Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 9:17 pm
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.

Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 9:27 pm
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.

Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 9:33 pm
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.

Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 9:41 pm
by Oblivion_123
Unfortunately it didn't work. It doesn't draw a single box.

Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 9:42 pm
by MadByte
I edited the post. You just have to "spawn" a box using the newBox function somewhere.

Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 9:58 pm
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.

Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 10:12 pm
by zorg
instead of math.random use love.math.random

Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 10:16 pm
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

Re: Choosing a colour only once

Posted: Sat Nov 07, 2015 11:05 pm
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.