c# - 有没有办法避免在 C# 中编写重复代码?

标签 c# arrays

我想在表格中插入更多球。但是,为了让它反弹,我必须为不同的球一遍又一遍地编写相同的算法。我可以知道有什么办法可以不用一遍又一遍地写吗?我的代码如下。

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

谢谢。

最佳答案

是的,你可以!这是面向对象编程的许多很酷的特性之一。

创建一个Ball 类。当您开始游戏时,创建您需要的所有球并将它们存储在一个列表中。从那里您可以使用 foreach 循环来修改每个 Ball 对象的属性。

public class Ball
{
    public int speedX { get; private set; }
    public int speedY { get; private set; }
    public int positionX { get; private set; }
    public int positionY { get; private set; }

    public Ball(int speedX, int speedY, int positionX, int positionY)
    {
        this.speedX = speedX;
        this.speedY = speedY;
        this.positionX = positionX;
        this.positionY = positionY;
    }

    public int setSpeedX(int newSpeed)
    {
        this.speedX = newSpeed;
    }

    //Add any other setters you need.
} 

现在您有了需要创建的任何球的蓝图。然后在你的游戏中你可以做这样的事情:

public class StartGame
{
    public List<Ball> ballList { get; private set; }

    public StartGame()
    {
        this.ballList = new List<Ball>();
        InitializeComponent();
    }

    private void StartGame_Load(object sender, EventArgs e)
    {
        //Add any balls you need here.
        ballList.add(new Ball(5, 10, 1, 1));
        ballList.add(new Ball(2, 17, 2, 9));
        ballList.add(new Ball(4, 12, 7, 5));
    }

    private void StartGame_Paint_1(object sender, PaintEventArgs e)
    {
        //This foreach loop will run through all the balls in ballList
        foreach(Ball ball in ballList)
        {
            e.Graphics.FillEllipse(Brushes.Blue, ball.positionX, ball.positionY, 10, 10);
            e.Graphics.DrawEllipse(Pens.Blue, ball.positionX, ball.positionY, 10, 10);
        }
    }
}

我不是 100% 确定你的游戏是如何运作的,所以我对变量做了一些猜测,但我希望你能理解。

关于c# - 有没有办法避免在 C# 中编写重复代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683297/

相关文章:

c# - 迭代 IDictionary/Dictionary 对象

javascript - 从 .ascx 文件调用 Javascript 函数

arrays - 如何在 Kotlin 中将两个数组合并为一个数组?

c++ - 动态数组和求值之和的问题

Java 队列、数组和 JNI

javascript - Algolia 搜索和嵌套数组

c# - ASP.Net Core 2 身份验证

c# - 层次结构不会在 View 中更新

c# - using 语句中的单个程序集

c - 如何使用用户输入的结构填充动态数组的元素?