我试图在一个矩形上弹出一个椭圆。我正在使用计时器在x和y方向上移动椭圆。我的想法是创建一个if语句来查看elipse的坐标是否与矩形的坐标匹配。
这是我到目前为止编写的代码:
public partial class Form1 : Form
{
Class1 square = new Class1();
public int before;
public int after;
public int c;
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
after = 590 - (b * b) / 100;
before = 100 + (a * a) / 100;
c = a + b;
Graphics g = e.Graphics;
SolidBrush Brush = new SolidBrush(Color.White);
g.FillEllipse(Brush, a, before, 10, 10);
square.Draw(g);
if (k >= square.y && a >= square.x && a <= square.x + 40)
{
a=c;
before= after;
timer1.Start();
timer2.Stop();
}
else if (k >= square.y + 10)
{
timer2.Stop();
MessageBox.Show("You lost");
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
square.x = e.X;
Cursor.Hide();
}
public int a = 0;
public int b = 0;
public void timer2_Tick(object sender, EventArgs e)
{
a += 1;
Invalidate();
}
private void timer1_Tick(object sender, EventArgs e)
{
b += 1;
Invalidate();
}
}
我知道有问题。我有一些问题:
我知道这个问题可能有点不明确或抽象,但任何帮助都是适当的。如果你想让我在某些方面更清楚,请告诉我!感谢
答案 0 :(得分:0)
使边缘椭圆反弹的一种简单方法是检查边缘点与边界的关系,然后反转正确的方向。所以,如果你原谅伪代码
,这样的事情应该有用loop used to animate the ellipse
before moving, check the position:
if right-most position == right wall
invert x velocity
if left-most position == left wall
invert x velocity
if top-most position == top wall
invert y velocity
if bottom-most position == bottom wall
invert y velocity
move ellipse to next position
这是一个非常简单的模拟,但它应该让您了解如何进步和开发更复杂的模型。希望这有帮助!