C# 为什么球没有在右边的边缘反弹
球在右边反弹了一半。然而,我希望它从右边的边缘反弹。我可以知道我该怎么做吗?我拥有的代码如下C# 为什么球没有在右边的边缘反弹,c#,algorithm,C#,Algorithm,球在右边反弹了一半。然而,我希望它从右边的边缘反弹。我可以知道我该怎么做吗?我拥有的代码如下 public partial class StartGame : Form { int x; //The x axis from the upper left corner int y; //The y axis from the upper left corner int spdX; //The change of x int spdY; //The change o
public partial class StartGame : Form
{
int x; //The x axis from the upper left corner
int y; //The y axis from the upper left corner
int spdX; //The change of x
int spdY; //The change of y
public StartGame()
{
InitializeComponent();
}
private void StartGame_Load(object sender, EventArgs e)
{
//Loads the ball on the screen at bottom of the window
spdX = 1; //The speed of the ball of y
spdY = 1; //The speed of the ball of x
x = this.ClientSize.Width / 5; //The x axis the ball is loaded at
y = this.ClientSize.Height - 10; //The y axis the ball is loaded at
}
private void StartGame_Paint_1(object sender, PaintEventArgs e)
{
//This is the inner paint color of the circle that is 10 by 10
e.Graphics.FillEllipse(Brushes.Blue, x, y, 10, 10);
//This is the outline paint color of the circle that is 10 by 10
e.Graphics.DrawEllipse(Pens.Blue, x, y, 10, 10);
}
private void timer1_Tick(object sender, EventArgs e)
{
y = y + spdY;
x = x + spdX;
if (y < 0)
{
spdY = -spdY; //if y is less than 0 then we change direction
}
else if (x < -5)
{
spdX = -spdX;
}
else if (y + 10 > this.ClientSize.Height)
{
spdY = -spdY; //if y + 10, the radius of the circle is greater than the form width then we change direction
}
else if (x + 10 > this.ClientSize.Height)
{
spdX = -spdX;
}
this.Invalidate();
}
应该是
else if (x + 10 > this.ClientSize.Width)
复制/粘贴错误?: 应该是
else if (x + 10 > this.ClientSize.Width)
复制/粘贴错误?: 您没有正确检查“x”移动的高度 更改:
else if (x + 10 > this.ClientSize.Height)
致:
您没有正确检查“x”移动的高度 更改:
else if (x + 10 > this.ClientSize.Height)
致:
您编写的代码看起来很好。现在是时候开始培养阅读代码的技能了,试着找出错误行为的原因,而不是询问其他人。你写的代码看起来不错。现在是时候开始培养阅读代码的技能了,试着找出错误行为的原因,而不是询问其他人。不客气。我不能告诉你我做过多少次类似的事情;可能值得一提的是,x和y测试不应该是同一if-else结构的一部分。他们应该是独立的。否则,就有可能出现球反弹到一个角落而不能正确反射的情况,因为只有一种情况发生,而不是两种情况发生。不客气。我不能告诉你我做过多少次类似的事情;可能值得一提的是,x和y测试不应该是同一if-else结构的一部分。他们应该是独立的。否则,当球反弹到一个角落时,可能会出现这样的情况,并且不能正确地反射,因为只有一种情况发生,而不是两种情况发生。没问题。这通常是我该多煮点咖啡的暗示…没问题。这通常是我该多煮点咖啡的暗示。。。