随机化3种颜色 [英] Randomize 3 Colors
问题描述
我正在制作Breakout Game,我希望积木获得随机的BackColor。
现在,我的积木充满了所有类型的颜色:红色,绿色,蓝色,黄色,粉红色和所有其他颜色。
I'm making a Breakout Game and I want the bricks to get a random BackColor. Right now I have the bricks getting filled with all type of colors: red, green, blue, yellow, pink and all others.
我想做什么
这就是我现在拥有的:
private Random rnd = new Random();
Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
bricks[x, y].BackColor = randomColor;
我搜索并尝试了许多不同的方法,但没有任何效果!我觉得这可能很简单,但实际上并不能完全解决。我考虑过要列出这三种颜色的列表,然后尝试将列表随机化,但是没有成功!
I've searched and tried many different things and nothing has worked! I feel like this might be simple to do but somehow can't really get to it. I thought about making a list of the 3 colors and try randomizing the list but it didn't work out!
推荐答案
免责声明:我是新手(对C#)。
Disclaimer: I'm a newbie (to C#).
private Random rnd = new Random();
Color[] colorArr = {Color.FromArgb(0, 0, 255), Color.FromArgb(255, 255, 0), Color.FromArgb(0, 255, 0)};
bricks[x, y].BackColor = colorArr[rnd.Next(colorArr.Length)];
(colorArr.Length编辑由 Corak )
(colorArr.Length edit suggested by Corak)
使用数组存储将要选择的所有颜色。接下来,从数组中调用一个随机元素。
Uses an array to store all the colors from which one will be picked. Next, a random element is called from the array.
这篇关于随机化3种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!