有没有一种方法,以避免在C#编写重复code? [英] Is there a way to avoid writing duplicate code in C#?

查看:126
本文介绍了有没有一种方法,以避免在C#编写重复code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想插入更多的球到表单中。然而,为了允许其反弹我不得不code中的相同的算法一遍一遍为不同的球。可我知道,有没有什么办法,我可以不用一遍又一遍写它?在codeS我有如下所示。

I would like to insert more balls into the form. However, in order to allow it to bounce I have to code the same algorithm over and over again for different ball. May I know is there any way I can do it without writing it over and over again? The codes I have are as below.

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

感谢您。

推荐答案

当然可以!这是面向对象编程的许多很酷的功能之一。

Yes you can! This is one of the many cool features of object oriented programming.

创建一个类。当您启动游戏创建你需要并将其存储在一个列表中的所有球。从那里,你可以使用的foreach 循环修改每个对象的属性。

Create a Ball class. When you start the game create all the balls you need and store them in a list. From there you can use foreach loops to modify the properties of each of your Ball objects.

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

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

Now you have a blueprint for any balls you need to create. Then in your game you can do something like this:

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%肯定你的游戏是如何工作的,所以我做了一些猜测的变量,但我希望你的想法。

I wasn't 100% sure how your game worked so I made some guesses with the variables but I hope you get the idea.

这篇关于有没有一种方法,以避免在C#编写重复code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆