【无标题】Visual Studio代码实现反弹球

一、设计界面如下

按钮是上面一个pictureBox,用于控制小球移动的开始。

二、创建类数据成员

Point p1;//球的实时位置
        Point p2;//球下一时刻的位置
        Point p3;//一个变量,用来存放小球碰到边界时小球的坐标
        Point rePt;//球的初始位置
        Bitmap mybmp;//引入图片
        Bitmap oldbmp;//保留初始图片
        System.Timers.Timer t200ms = new System.Timers.Timer(200);
        delegate void t200msDo(); //定义一个委托

三、完成图片加载,以及定时器初始坐标的初始化

public Form1()
        {
            InitializeComponent();
            mybmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = mybmp;
            var g = Graphics.FromImage(pictureBox1.Image);
            var brush = new SolidBrush(Color.White);
            g.FillRectangle(brush, 0, 0, pictureBox1.Width, pictureBox1.Height);
            oldbmp = (Bitmap)pictureBox1.Image.Clone();
            p1.X = 30;
            p1.Y = 0;
            p2.X = 40;
            p2.Y = 10;
            rePt = p1;
            t200ms.Elapsed += new System.Timers.ElapsedEventHandler(t200msOut);
            t200ms.AutoReset = false;
            t200ms.Enabled = false;
        }

四、进行小球的绘制

 public void t200msOut(object source, System.Timers.ElapsedEventArgs e)
        {
            Invoke(new t200msDo(t200msInvoke));
        }

        void t200msInvoke()
        {
            var g = Graphics.FromImage(pictureBox1.Image);
            g.DrawImage(oldbmp, 0, 0);
            int x = 600; int y = 400;
            if (p1.Y == 0 || p1.X == 0 || p1.Y == y || p1.X == x)
            {
                p3.X = p1.X; p3.Y = p1.Y;
            }           
            if (p1.Y == y && p2.X == (p1.X + 10) && p2.Y == (p1.Y + 10))//下边右上转
            {
                p2.Y -= 20;
            }
            if (p1.Y == y && p2.X == (p1.X - 10) && p2.Y == (p1.Y + 10))//下边左上转
            {
                p2.Y -= 20;
            }
            if (p1.X == x && p2.X == (p1.X + 10) && p2.Y == (p1.Y - 10))//右边左上转
            {
                p2.X -= 20;
            }
            if (p1.X == x && p2.X == (p1.X + 10) && p2.Y == (p1.Y + 10))//右边左下转
            {
                p2.X -= 20;
            }
            if (p1.X == 0 && p2.X == (p1.X - 10) && p2.Y == (p1.Y - 10))//左边右上转
            {
                p2.X += 20;
            }
            if (p1.X == 0 && p2.X == (p1.X - 10) && p2.Y == (p1.Y + 10))//左边右下转
            {
                p2.X += 20;
            }
            if (p1.Y == 0 && p2.X == (p1.X - 10) && p2.Y == (p1.Y - 10))//上边下左转
            {
                p2.Y += 20;
            }            
            if (p1.Y == 0 && p2.X == (p1.X + 10) && p2.Y == (p1.Y - 10))//上边下右转
            {
                p2.Y += 20;
            }
            if (p3.Y == 0 && p2.X > p1.X && p2.Y > p1.Y)//上边右下移动
            {

                p1.X += 10;
                p1.Y += 10;
                var brush = new SolidBrush(Color.Black);
                g.FillEllipse(brush, p1.X, p1.Y, 20, 20);
                t200ms.Start();
                p2.X += 10;
                p2.Y += 10;
            }
            if (p3.Y == y && p2.X > p1.X && p2.Y < p1.Y)//下边右上移动
            {

                p1.X += 10;
                p1.Y -= 10;
                var brush = new SolidBrush(Color.Black);
                g.FillEllipse(brush, p1.X, p1.Y, 20, 20);
                t200ms.Start();
                p2.X += 10;
                p2.Y -= 10;
            }
            if (p3.X == x && p2.X < p1.X && p2.Y < p1.Y)//右边左上移动
            {

                p1.X -= 10;
                p1.Y -= 10;
                var brush = new SolidBrush(Color.Black);
                g.FillEllipse(brush, p1.X, p1.Y, 20, 20);
                t200ms.Start();
                p2.X -= 10;
                p2.Y -= 10;
            }
            if (p3.X == x && p2.X < p1.X && p2.Y > p1.Y)//右边左下移动
            {

                p1.X -= 10;
                p1.Y += 10;
                var brush = new SolidBrush(Color.Black);
                g.FillEllipse(brush, p1.X, p1.Y, 20, 20);
                t200ms.Start();
                p2.X -= 10;
                p2.Y += 10;
            }
            if (p3.Y == 0 && p2.X< p1.X && p2.Y > p1.Y)//上边左下移动
            {

                p1.X -= 10;
                p1.Y += 10;
                var brush = new SolidBrush(Color.Black);
                g.FillEllipse(brush, p1.X, p1.Y, 20, 20);
                t200ms.Start();
                p2.X -= 10;
                p2.Y += 10;
            }
            if (p3.Y == y && p2.X < p1.X && p2.Y < p1.Y)//下边左上移动
            {

                p1.X -= 10;
                p1.Y -= 10;
                var brush = new SolidBrush(Color.Black);
                g.FillEllipse(brush, p1.X, p1.Y, 20, 20);
                t200ms.Start();
                p2.X -= 10;
                p2.Y -= 10;
            }
            if (p3.X == 0 && p2.X > p1.X && p2.Y < p1.Y)//左边右上移动
            {

                p1.X += 10;
                p1.Y -= 10;
                var brush = new SolidBrush(Color.Black);
                g.FillEllipse(brush, p1.X, p1.Y, 20, 20);
                t200ms.Start();
                p2.X += 10;
                p2.Y -= 10;
            }
            if (p3.X == 0 && p2.X > p1.X && p2.Y > p1.Y)//左边右下移动
            {

                p1.X += 10;
                p1.Y += 10;
                var brush = new SolidBrush(Color.Black);
                g.FillEllipse(brush, p1.X, p1.Y, 20, 20);
                t200ms.Start();
                p2.X += 10;
                p2.Y += 10;
            }
            pictureBox1.Refresh();
            g.Dispose();

        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

五、按键响应

private void button1_Click(object sender, EventArgs e)
        {

            p1 = rePt;
            t200ms.Start();

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值