我试图让位图从窗口的顶部和底部反弹。这似乎工作正常。所示的 Draw 方法在循环中被调用,导致位图不断地用它的新位置重绘。
public class Ball
{
double SpeedX = 4;
double SpeedY = 4;
int ballWidth = 1;
public double X {get; private set;}
public double Y {get; private set;}
public void Draw(Window screen)
{
X += SpeedX;
Y += SpeedY;
if (Y <= 0) {
SpeedY = -SpeedY;
Y = 0;
}
else if (Y >= (screen.Height - ballWidth)) {
SpeedY = -SpeedY;
Y = screen.Height - ballWidth;
}
Bitmap.Draw(X,Y);
}
}
但是,我试图在创建位图时更改初始值 SpeedX 和 Speedy。当我传入这些值时,我的位图卡在窗口的顶部/底部 - SpeedY 似乎导致 0,而 SpeedX 继续工作(导致位图滑过屏幕的顶部/底部)。有什么我做错了吗?我不明白为什么从外部传入两个值会弄乱 Draw 函数。
public Ball RandomBall(Window game)
{
Ball newball = new Ball(_Game);
Random direction = new Random();
newball.SpeedX = 5;
newball.SpeedY = 5;
return newball;