2

フォームにもっとボールを入れたいです。ただし、バウンドさせるには、別のボールに対して同じアルゴリズムを何度もコーディングする必要があります。何度も何度も書かずにそれを行う方法はありますか?私が持っているコードは以下の通りです。

int bBA1; //The x axis from the upper left corner
int bBA2; //The y axis from the upper left corner 
int spdBBA1; //The change of x
int spdBBA2; //The change of y

public StartGame()
{
    InitializeComponent();
}

private void StartGame_Load(object sender, EventArgs e)
{
    //Loads the ball on the screen at bottom of the window
    bBA1 = this.ClientSize.Width / 5; //The x axis the ball is loaded at
    bBA2 = this.ClientSize.Height - 10; //The y axis the ball is loaded at
    spdBBA1 = 1; //The speed of the ball of y
    spdBBA2 = 1; //The speed of the ball of x
}

private void StartGame_Paint_1(object sender, PaintEventArgs e)
{
    //This is the inner paint color of the circle that is 10 by 10
    e.Graphics.FillEllipse(Brushes.Blue, bBA1, bBA2, 10, 10);
    //This is the outline paint color of the circle that is 10 by 10
    e.Graphics.DrawEllipse(Pens.Blue, bBA1, bBA2, 10, 10); 
}

private void timer1_Tick(object sender, EventArgs e)
{
    bBA2 = bBA2 + spdBBA2;
    bBA1 = bBA1 + spdBBA1;

    if (bBA2 < 0)
    {
        spdBBA2 = -spdBBA2; //If y is less than 0 then it changes direction
    }
    else if (bBA1 < -5)
    {
        spdBBA1 = -spdBBA1;
    }
    else if (bBA2 + 10 > this.ClientSize.Height)
    {
        // If y + 10, the radius of the circle is greater than the
        // form width then we change direction
        spdBBA2 = -spdBBA2;
    }
    else if (bBA1 + 10 > this.ClientSize.Width)
    {
        spdBBA1 = -spdBBA1;
    }

    this.Invalidate(); 
}

ありがとうございました。

4

2 に答える 2