Hey guys, I've been trying to type this code that will replace an old variable's value with a new value. **Im using graphics with this code** Note that the value is also a variable. So in short, it's a variable in a variable.
**(I know the question is long, but please, just bear with me)**
These are the variables that I declared that were key to this part of code in my game
**Game I'm trying to make is called Piano Tiles (it's a my assigned game to remake for my school project, to make things brief here's a video that explains the game (it's quick to the point):
https://www.youtube.com/watch?v=4uZPiynHRsw[
^]
int x, y;
Brush bsh_black, bsh_grey, bsh_white, bsh_red;
Brush bc1, bc2, bc3, bc4;
int[] pat_select;
Random pat_ch = new Random();
int pat_assign;
3
InitializeComponent();
yf1 = 0;
bsh_black = new SolidBrush(Color.Black);
bsh_grey = new SolidBrush(Color.Gray);
bsh_white = new SolidBrush(Color.White);
bsh_red = new SolidBrush(Color.Red);
bc1 = bsh_white;
bc2 = bsh_white;
bc3 = bsh_white;
bc4 = bsh_white;
I declared these variables at the top of the page. The "bc" variables, I used as short for "brush changer." So that the brush will change according to the if conditions (will be displayed below this)
For "testing" purposes of my game, temporarily, I used yf1 as the variable that continually makes the row go down with the **timer** I coded it to. **As the timer ticks, I typed in "yf1++;".
if (dummy_state == 0)
{
game.FillRectangle(bc1, x1, yf1, x2, y2);
game.FillRectangle(bc2, x2, yf1, x2, y2);
game.FillRectangle(bc3, x3, yf1, x2, y2);
game.FillRectangle(bc4, x4, yf1, x2, y2);
}
private void Block_Timer_Tick(object sender, EventArgs e)
{
yf1++;
Invalidate();
}
So what I intended to do here was, when the "yf1" variable's value is greater than the form's vertical length AND whatever the "pat_assign" variable equals then the brushes will change to a certain color. And for the "pat_select" variable (shown from the top of this question), if a certain pattern (for example "pat4" variable) is already chosen, then the arrays list would be rearranged and exclude the currently selected pattern.
if (yf1 > y)
{
if (pat_assign == pat4)
{
pat_select = new int[3] { pat1, pat2, pat3 };
}
if (pat_assign == pat3)
{
pat_select = new int[3] { pat1, pat2, pat4 };
}
if (pat_assign == pat2)
{
pat_select = new int[3] { pat1, pat3, pat4 };
}
if (pat_assign == pat1)
{
pat_select = new int[3] { pat2, pat3, pat4 };
}
#region when pat_assign equals pat1
if (pat_assign == pat1)
{
bc1 = bsh_black;
bc2 = bsh_white;
bc3 = bsh_white;
bc4 = bsh_white;
Invalidate();
}
#endregion
#region when pat_assign equals pat2
if (pat_assign == pat2)
{
bc1 = bsh_white;
bc2 = bsh_black;
bc3 = bsh_white;
bc4 = bsh_white;
Invalidate();
}
#endregion
#region when pat_assign equals pat3
if (pat_assign == pat3)
{
bc1 = bsh_white;
bc2 = bsh_white;
bc3 = bsh_black;
bc4 = bsh_white;
Invalidate();
}
#endregion
#region when pat_assign equals pat4
if (pat_assign == pat4)
{
bc1 = bsh_white;
bc2 = bsh_white;
bc3 = bsh_white;
bc4 = bsh_black;
Invalidate();
}
#endregion
yf1 = 0 - y2;
}
Unfortunately when the graphic went back to the top of the form, it didn't change the pattern at all, but stayed with the current pattern it had. What's the problem with it? I don't really see anything...
UPDATE:
Here is the PainEventHandler:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics game = e.Graphics;
yf2 = yf1 - (y / 4);
if (dummy_state == 0)
{
game.FillRectangle(bchanger1, x1, yf1, x2, y2);
game.FillRectangle(bchanger2, x2, yf1, x2, y2);
game.FillRectangle(bchanger3, x3, yf1, x2, y2);
game.FillRectangle(bchanger4, x4, yf1, x2, y2);
#region When var "yf1" goes past bottom of the form
if (yf1 > y)
{
yf1 = 0 - y2;
if (pat_assign == pat2)
{
bchanger1 = bsh_white;
bchanger2 = bsh_black;
bchanger3 = bsh_white;
bchanger4 = bsh_white;
}
if (pat_assign == pat1)
{
bchanger1 = bsh_black;
bchanger2 = bsh_white;
bchanger3 = bsh_white;
bchanger4 = bsh_white;
pat_select = new int[3] { pat2, pat3, pat4 };
}
if (pat_assign == pat3)
{
bchanger1 = bsh_white;
bchanger2 = bsh_white;
bchanger3 = bsh_black;
bchanger4 = bsh_white;
pat_select = new int[3] { pat1, pat2, pat4 };
}
if (pat_assign == pat4)
{
bchanger1 = bsh_white;
bchanger2 = bsh_white;
bchanger3 = bsh_white;
bchanger4 = bsh_black;
pat_select = new int[3] { pat1, pat2, pat3 };
}
pat_changer.Next(0, pat_select.Length);
}
#endregion
}
}
And here is the Timer Tick Event Handler
private void tile_timer_Tick(object sender, EventArgs e)
{
yf1++;
yf1++;
yf1++;
Invalidate();
if (yf1 > y)
{
if (bchanger1 == bsh_black || bchanger2 == bsh_black || bchanger3 == bsh_black || bchanger4 == bsh_black)
{
tile_timer.Stop();
MessageBox.Show("Game Over");
}
}
}