如何让我的线在winforms上反弹

时间:2015-10-16 18:32:28

标签: c# winforms

我一直在尝试用C#创建一个winforms程序来说明Mystify屏幕保护程序,有些我怎么能让这条线顺利流动。我不知道我的计算或关闭,但我会很感激提示。线条在表格上随意出现,而不是以阴影或尾巴流动。

 private bool playButton = false;

    private int xP1 = 10;
    private int yP1 = 10;
    private int xP2 = 100;
    private int yP2 = 50;
    private int xSpeed = 0;
    private int ySpeed = 0;
    private int windowHeight = 400;
    private int windowWidth = 600;
    private int xSpeedChange = 2;
    private int ySpeedChange = 2;
    private Random rand = new Random();

    public SETMystify()
    {
        InitializeComponent();
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        playButton = true;
    }

    private void canvas_Paint(object sender, PaintEventArgs e)
    {
        Graphics paintLine = e.Graphics;
        Pen penColor = new Pen(Color.Red, 3);
        if (playButton == true)
        {


            xP1 = rand.Next(0, windowHeight);
            yP1 = rand.Next(0, windowWidth);
            yP2 = rand.Next(0, windowHeight);
            xP2 = rand.Next(0, windowWidth);

            xSpeed = rand.Next(5, 10);
            ySpeed = rand.Next(5, 10);

            xP1 += xSpeed;
            yP1 += ySpeed;
            xP2 += xSpeed;
            yP2 += ySpeed;
        }
        if (xP1 > windowWidth)
        {
            xP1 = windowWidth;
            xSpeed = xSpeedChange * -1;
        }
        if (xP1 < 0)
        {
            xP1 = 0;
            xSpeed = xSpeedChange;
        }
        if (yP1 > windowHeight)
        {
            yP1 = windowHeight;
            ySpeed = ySpeedChange * -1;
        }
        if (yP1 < 0)
        {
            yP1 = 0;
            ySpeed = ySpeedChange;
        }
        //-----------------------//
        if (xP2 > windowWidth)
        {
            xP2 = windowWidth;
            xSpeed = xSpeedChange * -1;
        }
        if (xP2 < 0)
        {
            xP2 = 0;
            xSpeed = xSpeedChange;
        }
        if (yP2 > windowHeight)
        {
            yP2 = windowHeight;
            ySpeed = ySpeedChange * -1;
        }
        if (yP2 < 0)
        {
            yP2 = 0;
            ySpeed = ySpeedChange;
        }
        paintLine.DrawLine(penColor, xP1, yP1, xP2, yP2);


        canvas.Invalidate();
    }

    }

0 个答案:

没有答案