球在右邊的中途彈回。但是,我希望它在右邊反彈。我可以知道我該怎麼做對嗎?我擁有的代碼如下。爲什麼球沒有在右邊的邊緣彈跳
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();
}
你*寫*代碼看起來不錯。現在是時候開始開發技能來閱讀你的代碼,並試圖找出爲什麼它有「錯誤」的行爲,而不是問其他人。 – paddy