[英]Shoot balls at mouse location in c# GDI
Hello I am trying to shoot balls to mouse location by clicking.您好,我正在尝试通过单击将球射到鼠标位置。 I really do not know if mistake is in form or in class game or if I am using wrong wrong event.我真的不知道错误是在形式上还是在 class 游戏中,或者我是否使用了错误的错误事件。 Right now when I click nothing happens.现在当我点击什么都没有发生。
public class Game
{
public List<Ball> Balls { get; set; } = new List<Ball>();
public List<Base> Base { get; set; } = new List<Base>();
public void Draw(Graphics g)
{
foreach (Ball item in this.Balls)
{
item.Draw(g);
}
public void Next()
{
foreach (Ball item in this.Balls)
{
item.Move();
}
}
I think there is mistake in this part but I dont know what I am doing wrong我认为这部分有错误,但我不知道我做错了什么
public void Click(int x, int y)
{
foreach(Ball item in this.Balls)
{
Ball b = new Ball(this)
{
X = x,
Y = y,
Vx = x / 10,
Vy = y / 10
};
this.Balls.Add(b);
}
}
}
public partial class Form1 : Form
{
private Game game;
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
this.game.Draw(e.Graphics);
}
private void Timer1_Tick(object sender, EventArgs e)
{
this.game.Next();
this.pictureBox1.Refresh();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
this.game.Click(e.X, e.Y);
}
}
In the Click
method, you add a new Ball(...)
for each item in Balls
.在Click
方法中,您为Balls
中的每个项目添加一个new Ball(...)
。
So if you start with 10 balls, clicking would cause 10 more balls to be added.因此,如果您从 10 个球开始,单击会导致再添加 10 个球。 If you start with 0 balls, clicking would cause 0 more balls to be added.如果您从 0 个球开始,单击将导致添加 0 个球。
Seems to me you should just remove the foreach
from that method.在我看来,您应该从该方法中删除foreach
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.