C# 在不同坐标位置和速度下移动对象
好的,我以前使用过c#,但在asap.net中使用过数据库,构建表单和数据驱动的站点。我现在正努力学习在动画或游戏中使用它 我仍在尝试,并试图抓住它,所以请容忍我 因此,我有两个对象(C# 在不同坐标位置和速度下移动对象,c#,if-statement,coordinates,C#,If Statement,Coordinates,好的,我以前使用过c#,但在asap.net中使用过数据库,构建表单和数据驱动的站点。我现在正努力学习在动画或游戏中使用它 我仍在尝试,并试图抓住它,所以请容忍我 因此,我有两个对象(Ball&Target),当我单击按钮时,它们在windows应用程序中移动(我使用的时间设置为false,然后单击开始) 当两个物体通过每个覆盖空间时,背景颜色会发生变化。问题是我试图让目标从左向右移动,而不是从一个角移动到另一个角 有人能告诉我如何用我现有的代码做这件事吗,谢谢 代码 using System;
Ball
&Target
),当我单击按钮时,它们在windows应用程序中移动(我使用的时间设置为false,然后单击开始)
当两个物体通过每个覆盖空间时,背景颜色会发生变化。问题是我试图让目标从左向右移动,而不是从一个角移动到另一个角
有人能告诉我如何用我现有的代码做这件事吗,谢谢
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int ballspeed;
int targetspeed;
public Form1()
{
InitializeComponent();
ballspeed = 4;
targetspeed = 2;
}
private void timer1_Tick(object sender, EventArgs e)
{
int newX, newY, newA, newB;
newX = Ball.Location.X + ballspeed;
newY = Ball.Location.Y + ballspeed;
newA = Target.Location.X + targetspeed;
newB = Target.Location.Y + targetspeed;
if (newX > this.Width - Ball.Width)
{
ballspeed = -ballspeed;
}
if (newX < 0)
{
ballspeed = -ballspeed;
}
if (Ball.Bounds.IntersectsWith(Target.Bounds))
{
this.BackColor = Color.Black;
}
else
{
this.BackColor = Color.White;
}
Ball.Location = new Point(newX, newY);
if (newA > this.Width - Target.Width)
{
targetspeed = -targetspeed;
}
if (newA < 0)
{
targetspeed = -targetspeed;
}
Target.Location = new Point(newA, newB);
}
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
国际球速;
国际目标速度;
公共表格1()
{
初始化组件();
球速=4;
目标速度=2;
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
int newX,newY,newA,newB;
newX=球。位置。X+球速;
newY=Ball.Location.Y+球速;
newA=Target.Location.X+targetspeed;
newB=目标位置Y+目标速度;
if(newX>this.Width-Ball.Width)
{
球速=-球速;
}
if(newX<0)
{
球速=-球速;
}
if(球边界与目标边界相交)
{
this.BackColor=Color.Black;
}
其他的
{
this.BackColor=Color.White;
}
Ball.Location=新点(newX,newY);
if(newA>this.Width-Target.Width)
{
targetspeed=-targetspeed;
}
if(newA<0)
{
targetspeed=-targetspeed;
}
目标位置=新点(新点、新点);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
this.timer1.Start();
}
}
}
不要向Y
坐标添加值:
newB = Target.Location.Y;
更新两个坐标将修改两个轴上的位置。因此,将targetspeed
添加到X
和Y
将使Target
对象在两个方向上移动相同的量。然后,对象沿对角线移动。您只需更新X
坐标。感谢您的解释,而不仅仅是回答+1。我将试一试并接受你的回答。