C
C#•5mo ago
marine2oorah4u

how do i make a loop or array to choose between different color[]? (csharp)

im new to c# so i dont know a whole lot, but im trying to do something with my version of snake. So what i want to try to do is, essentially i want to add in a way to choose between different color combinations for the tail, i.e: like the different colors of different irl snakes as a cool little feature this is my code that i added to the post for the game. this is the code for the color of the snake tail currently
tailColorList = new Color[] {Color.Red, Color.Black };
tailColorList = new Color[] {Color.Red, Color.Black };
nothing special lol this is the draw code for the snake
int colorOn = 0;
for (int i = 0; i < snake.TailLength; ++i)
{
spriteBatch.Draw(texture, new Rectangle(snake.Tail[i].X * cellSize, snake.Tail[i].Y * cellSize + worldScreenTopY, cellSize, cellSize), tailColorList[colorOn]);

if (++colorOn >= tailColorList.Length)
{
colorOn = 0;
}
}
int colorOn = 0;
for (int i = 0; i < snake.TailLength; ++i)
{
spriteBatch.Draw(texture, new Rectangle(snake.Tail[i].X * cellSize, snake.Tail[i].Y * cellSize + worldScreenTopY, cellSize, cellSize), tailColorList[colorOn]);

if (++colorOn >= tailColorList.Length)
{
colorOn = 0;
}
}
i have a gamestate that i was starting to work on that i would have the skin selector active, in which you could swap through and choose different color combinations to use. basically, how would i go about adding support for this, or what code would i do to do so? I imagine that it would involve either a Color[] array of each combination of colors, maybe an array for the picker? otherwise i dont really know what i would do. Again im still learning, so my code probably isn't the best, or my thinking/logic is post likely wrong, via how i would have to do this. But if youre able to help, then that would be super helpful. let me know 🙂
4 Replies
Angius
Angius•5mo ago
You would read the tailColorList from somewhere, like the gamestate for example, sure In the initialize, instead of hardcoding the color list, you would pick that
marine2oorah4u
marine2oorah4uOP•5mo ago
how could it be done though?
Angius
Angius•5mo ago
Actually, could probably do it as late as InitGamePlay Looking at your code... you'd have something like
private Color[] _tailColors;
// ...
void DrawSkinChooser()
{
// ...
var selectedColor = // select the color somehow
_tailColors = selectedColor;
}
private Color[] _tailColors;
// ...
void DrawSkinChooser()
{
// ...
var selectedColor = // select the color somehow
_tailColors = selectedColor;
}
and use that _tailColors instead of your tailColorList Basically, store the skin in a field, then in the skin chooser set the value of that field And when drawing the snake, pick the colors from that field
marine2oorah4u
marine2oorah4uOP•5mo ago
Ok I’ll try after work The thing that I’m worried about or know I’ll have to figure out too is Which I’m sure I’ll have to do it in the game state or draw? Is that I’m sure I’ll have to link said skin chopper in a picker scroll system Which idk how I would do that I’m sure I can figure it out once I add or get the above code added successfully
Want results from more Discord servers?
Add your server