一、设计界面如下
按钮是上面一个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();
}