0

Flappy Bird (ゲーム) のような小さなコードを書き、タイマーの 1 つに次のコードを書きましたが、このタイマーを開始すると、パイプが上下に 3 回表示され、それから絵が動き出します。黒くて、もうパイプがないことを示しています。どこに問題があるのか​​教えてくれたら、それはありがたい..

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;
        }
    }

そして、ここに宣言があります:

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

フォームのロード部分は次のとおりです。

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;

以下が塗装部分です。

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]));

そして、最初のタイマーには次のものがあります。

this.Invalidate();
4

1 に答える 1

2

if ステートメントPipe1の下部に迷いがあります。Pipe2

アップデート:

宣言を次のように変更してみてください。

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

およびタイマー機能で次のことができます。

 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;
            }
        } 

そしてあなたのロードフォーム部分:

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);

塗装部分は大丈夫でしょう

于 2014-02-28T12:23:14.283 に答える