Omnimaga
Calculator Community => TI Calculators => Lua => Topic started by: Nick on November 11, 2011, 04:50:21 am
-
I was looking for a fast way to switch between colors for my tetris project, so i tried to find a faster way of choosing a color and setting it with setColorRGB(). I don't know if it's the fastest way, but it goes nice and the colde is tidy :)
It's only to get the most basic colors: Red, Green, Blue, turquoise, pink, orange and yellow.
This is what you would have normally (or better, i had):
color = math.random(1,7)
if color == 1 then
gc:setColorRGB(255,0,0)
elseif color == 2 then
gc:setColorRGB(0,255,0)
elseif color == 3 then
gc:setColorRGB(0,0,255)
elseif color == 4 then
gc:setColorRGB(255,0,255)
elseif color == 5 then
gc:setColorRGB(0,255,255)
elseif color == 6 then
gc:setColorRGB(255,255,0)
elseif color == 7 then
gc:setColorRGB(255,150,0)
end
and this is what you get now:
color=math.random(1,7)
colortable={0,0,0}
if color<=3 then
colortable[color]=255
elseif color==4 or color==5 then
colortable[3]=255
colortable[color-3]=255
elseif color==6 then
colortable[1]=255
colortable[2]=255
else
colortable[1]=255
colortable[2]=150
end
gc:setColorRGB(colortable[1],colortable[2],colortable[3])
both results are the same (luckily xp )
(http://img.removedfromgame.com/imgs/ChooseColor.gif)
does anyone know if this is more relevant than the upper one?
-
You could use this:
colortable = {{255,0,0}, {0,255,0}, {0,0,255}, {255,0,255}, {0,255,255}, {255,255,0}, {255,150,0}}
setColorRGB(unpack(colortable[math.random(#colortable)]))
This way you can easily add colors, and it will continue to work :)
-
wow, that looks really easy :) thanks! now my code will finally get a little more tidy :)
but i do not understand the unpack method, what does it do? (always wanting to learn something new xp)
-
When you type unpack({1,2,3}), the function returns 1,2,3.
It basically unpacks a table.
-
oh, thanks a lot, that might be useful for some other functions too :)
-
Good method for doing this! The unpack command looks quite helpful.
-
Apcalc, mind sharing your technique for your Axe tunnel? Becuase your game seemed to flash between colors even faster than in the screenshot above. Both are nice nonetheless, though.