0

i wrote a little code like one from Flappy Bird (game) and in one of my timer i wrote the following code but when i'm starting this timer it's just showing me one up and one down pipe 3 times and then the painting just going black and it's showing me no more pipes. If you guys telling me where's the problem, that would be thankful..

private void timer2_Tick(object sender, EventArgs e)
    {
        if (Pipe1[0] + PipeWidth <= 0 | start == true)
        {
            Random rnd = new Random();
            int px = this.Width;
            int py = rnd.Next(40, (this.Height - PipeDifferentY));
            var p2x = px;
            var p2y = py + PipeDifferentY;
            int[] p1 = { px, py, p2x, p2y };
            Pipe1 = p1;
        }
        else
        {
            Pipe1[0] = Pipe1[0] - 2;
            Pipe1[2] = Pipe1[2] - 2;
        }
        if (Pipe2[0] + PipeWidth <= 0)
        {
            Random rnd = new Random();
            int px = this.Width;
            int py = rnd.Next(40, (this.Height - PipeDifferentY));
            var p2x = px;
            var p2y = py + PipeDifferentY;
            int[] p1 = { px, py, p2x, p2y };
            Pipe1 = p1;
        }
        else
        {
            Pipe2[0] = Pipe2[0] - 2;
            Pipe2[2] = Pipe2[2] - 2;
        }
        if (start == true)
        {
            start = false;
        }
    }

And here is the declares:

int[] Pipe1 = { 0, 0, 0, 0 };
int[] Pipe2 = { 0, 0, 0, 0 };
int PipeWidth = 55;
int PipeDifferentY = 140;
int PipeDifferentX = 180;
bool start = true;

Here is the Load Form part:

Random rnd = new Random();
int py = rnd.Next(40, (this.Height - PipeDifferentY));
int py2 = py + PipeDifferentY;
int[] p1 = { this.Width, py, this.Width, py2 };
Pipe1 = p1;

py = rnd.Next(40, (this.Height - PipeDifferentY));
py2 = py + PipeDifferentY;
int[] p2 = { this.Width + PipeDifferentX, py, this.Width + PipeDifferentX, py2 };
Pipe2 = p2;

Here is the painting part:

e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe1[0], 0, PipeWidth, Pipe1[1]));
        e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe1[2], Pipe1[3], PipeWidth, this.Height - Pipe1[3]));

        e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe2[0], 0, PipeWidth, Pipe2[1]));
        e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe2[2], Pipe2[3], PipeWidth, this.Height - Pipe2[3]));

And the first timer has just:

this.Invalidate();
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Castiel
  • 77
  • 2
  • 13
  • This is maybe not related to your problem, but you should make the `Random` object a field in your class and create it in the constructor. Do not create it in the timer handler. Otherwise it might keep producing the same value if the timer tick interval is very short. – Matthew Watson Feb 28 '14 at 12:20
  • No is still doing that.., can you show me how by code? – Castiel Feb 28 '14 at 12:22
  • It's a bit hard to see what might be wrong from just this code. One other unrelated thing though: You seem to be using `dynamic` where you should be using `var`. – Matthew Watson Feb 28 '14 at 12:23
  • But it's keep giving me the same 'error' with 'var' too – Castiel Feb 28 '14 at 12:24
  • Yes, that's unrelated, but you shouldn't be using `dynamic` instead of `var`. – Matthew Watson Feb 28 '14 at 12:28
  • oke, please look down what i commented to Starscream1984, if you can help me with it. – Castiel Feb 28 '14 at 12:29
  • Here's my suggestion: Take that code and put it into a Console test app and call it from a loop rather than a timer. Then you'll be able to run it under the debugger and see what's going wrong. – Matthew Watson Feb 28 '14 at 12:37
  • Hey Matthew, but i have this at the beggining: int[] Pipe1 = { 0, 0, 0, 0 }; and i made that int[] p1 with those values, because i couln't put it dirrectly into Pipe1 – Castiel Feb 28 '14 at 12:51

1 Answers1

2

You have a stray Pipe1 at the bottom of your Pipe2 if statement.

Update:

Try change declares to:

List<int> Pipe1 = new List<int>();
List<int> Pipe2 = new List<int>();
int PipeWidth = 55;
int PipeDifferentY = 140;
int PipeDifferentX = 180;
bool start = true;

and timer function to:

 private void timer2_Tick(object sender, EventArgs e)
        {
            if (Pipe1[0] + PipeWidth <= 0 | start == true)
            {
                Random rnd = new Random();
                int px = this.Width;
                int py = rnd.Next(40, (this.Height - PipeDifferentY));
                var p2x = px;
                var p2y = py + PipeDifferentY;
                Pipe1.Clear();
                Pipe1.Add(px);
                Pipe1.Add(py);
                Pipe1.Add(p2x);
                Pipe1.Add(p2y);
            }
            else
            {
                Pipe1[0] = Pipe1[0] - 2;
                Pipe1[2] = Pipe1[2] - 2;
            }
            if (Pipe2[0] + PipeWidth <= 0)
            {
                Random rnd = new Random();
                int px = this.Width;
                int py = rnd.Next(40, (this.Height - PipeDifferentY));
                var p2x = px;
                var p2y = py + PipeDifferentY;
                int[] p1 = { px, py, p2x, p2y };
                Pipe2.Clear();
                Pipe2.Add(px);
                Pipe2.Add(py);
                Pipe2.Add(p2x);
                Pipe2.Add(p2y);
            }
            else
            {
                Pipe2[0] = Pipe2[0] - 2;
                Pipe2[2] = Pipe2[2] - 2;
            }
            if (start == true)
            {
                start = false;
            }
        } 

and your load form part:

Random rnd = new Random();
int py = rnd.Next(40, (this.Height - PipeDifferentY));
int py2 = py + PipeDifferentY;
Pipe1.Clear();
Pipe1.Add(this.Width);
Pipe1.Add(py);
Pipe1.Add(this.Width);
Pipe1.Add(p2y);

py = rnd.Next(40, (this.Height - PipeDifferentY));
py2 = py + PipeDifferentY;
Pipe2.Clear();
Pipe2.Add(this.Width + PipeDifferentX);
Pipe2.Add(py);
Pipe2.Add(this.Width + PipeDifferentX);
Pipe2.Add(p2y);

painting part should be ok

Starscream1984
  • 3,072
  • 1
  • 19
  • 27