Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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[^]
C#
int x, y;
//x = ClientRectangle.Width;
//y = ClientRectangle.Height;

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;


C#
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++;".

C#
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);
}


C#
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.
C#
if (yf1 > y)
{
    //the original array is 
    //pat_select = new int[4] {pat1, pat2, pat3, pat4};
    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:
C#
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
C#
private void tile_timer_Tick(object sender, EventArgs e)
{
    yf1++;
    yf1++;
    yf1++;
    Invalidate();

    //If any black tile goes beyong the bottom off the game screen
    if (yf1 > y)
    {
        if (bchanger1 == bsh_black || bchanger2 == bsh_black || bchanger3 == bsh_black || bchanger4 == bsh_black)
        {
            tile_timer.Stop();
            MessageBox.Show("Game Over");
        }
    }
}
Posted
Updated 2-Jun-14 7:25am
v3
Comments
gggustafson 2-Jun-14 9:29am    
All of the code you provided gives a hint at your problem. BUT I need your OnPaint event handler and your timer tick event handler. Also you only need one Invalidate ( ). And get rid of the #regions - it was a Microsoft misstep.

Add the code using the Improve question link, above.
Sergey Alexandrovich Kryukov 2-Jun-14 13:25pm    
Please don't re-post, it won't help you to get an advice.
—SA
gggustafson 2-Jun-14 14:08pm    
He's not reposting, he added information that I requested.
Sergey Alexandrovich Kryukov 2-Jun-14 16:02pm    
Oh... but it should be done on a page of original question using "Improve question". Why duplicating?
—SA
gggustafson 2-Jun-14 19:59pm    
I'm not sure I understand. I asked him to use Improve question and he did. What am I missing?

1 solution

After considerable discussions with OP, he solved his problem.
 
Share this answer
 
Comments
ZurdoDev 6-Jun-14 9:09am    
+5 for sticking with it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900