汽车撞到屏幕的末端并向后移动

时间:2015-02-26 19:42:10

标签: c# winforms 2d picturebox

如果我没有正确描述这个标题,我很抱歉,因为我在这个问题的标题上苦苦挣扎。我正在克隆Crossy Road(不卖)。我的游戏有2D图形,从鸟瞰角度来看。无论如何,我的问题是我需要在他们到达屏幕的末端并使用随机的汽车图像后将车辆移回到起点。我的游戏是在Winforms中,因此汽车是图片框。我已经搜索了很多,但没有发现任何解释我需要做什么,因为这是我需要的。所有答案请直接解释并彻底解释,我不知道很多c#。我有点像初学者。 这是我的负荷(6和65是猜测)

private void Frogger_Load(Object sender, EventArgs e)
{
timer1.Enabled = true;
Car1Hitbox = new Rectangle(x1, y1, 6, 65);
Car2Hitbox = new Rectangle(x2, y2, 6, 65);
Car3Hitbox = new Rectangle(x3, y3, 6, 65);
Car4Hitbox = new Rectangle(x4, y4, 6, 65);
Car5Hitbox = new Rectangle(x5, y5, 6, 65);
}

这是我的计时器

private void timer1_Tick(object sender, EventArgs e)
{
this.SuspendLayout();
Car1.Location = new Point(Car1.Location.X + 19, Car1.Location.Y);
x1 += 19;
Car2.Location = new Point(Car2.Location.X + 30, Car2.Location.Y);
x2 += 30;
Car3.Location = new Point(Car3.Location.X + 16, Car3.Location.Y);
x3 += 16;
Car4.Location = new Point(Car4.Location.X + 18, Car4.Location.Y);
x4 += 18;
Car5.Location = new Point(Car5.Location.X + 25, Car5.Location.Y);
x5 += 25;
this.ResumeLayout();
{

我的车在屏幕上水平移动。我的屏幕尺寸是642乘654.任何和所有帮助表示赞赏。

感谢您抽出宝贵时间阅读本文。

1 个答案:

答案 0 :(得分:1)

我不认为这对这个网站来说是一个非常好的问题,但是因为我正在等待一些懒惰的DBA,所以我会咬人。

您的代码可以改进很多内容,但要解决您的具体问题,您需要确定何时需要“回收”每辆车。要执行此操作,当您移动时,例如Car1,通过将框的宽度添加到其当前位置来检查它是否位于屏幕边缘(或超出)。如果该值大于或等于您的屏幕宽度,则重置汽车(将该点的x组件设置为0):

if(Car1.Location.X + Car1.Size.Width >= this.Width) {
  Car1.Location = new Point(0, Car1.Location.Y);
}

如果您将所有车辆保持在List<PictureBox>并且每次计时器勾选时都在列表中重复,那么这一切都会变得更容易。

相关问题