私は現在ポンのゲームをプログラミングしています。ゲームの大部分を完了しましたが、厄介な問題に遭遇しました。dispatcherTimer
優先度を送信に設定し、期間を 1 ミリ秒に設定して使用しています。ボールを十分に速く動かすために、最大 dx=9 と dy=9 を使用して長方形をアニメーション化しています。ピクセル ジャンプが大きいため、ボールはスムーズに移動するのではなく、画面上をスキップしているように見えます。1 サイクルあたり 1 ミリ秒の計算によると、このボールは実際よりもはるかに速く動いているはずです。ボールをより頻繁に更新し、移動量を減らす必要があります...
これを行うためのより良い方法に関する提案はありますか? ここに私が持っているもののスニペットがあります...
pongballTimer = new DispatcherTimer(DispatcherPriority.Send);
pongballTimer.Tick += new EventHandler(pongballTimer_Tick);
pongballTimer.Interval = new TimeSpan(0, 0, 0, 0, _balldt);
private void pongballTimer_Tick(object sender, EventArgs e)
{
double pongtop = Canvas.GetTop(PongBall);
double pongleft = Canvas.GetLeft(PongBall);
double paddletop = Canvas.GetTop( RightPaddle );
double paddleleft = Canvas.GetLeft( RightPaddle );
if (pongleft + PongBall.Width > paddleleft)
{
if (((pongtop < paddletop + RightPaddle.Height) && (pongtop > paddletop)) ||
((pongtop + PongBall.Height < paddletop + RightPaddle.Height) &&
(pongtop + PongBall.Height > paddletop)))
{
_dx *= -1;
SetBalldy(pongtop, PongBall.Height, paddletop, RightPaddle.Height);
_rightpoint++;
lblRightPoint.Content = _rightpoint.ToString();
meHitSound.Play();
}
else // The ball went past the paddle without a collision
{
RespawnPongBall(true);
_leftpoint++;
lblLeftPoint.Content = _leftpoint.ToString();
meMissSound.Play();
if (_leftpoint >= _losepoint)
LoseHappened("You Lost!!");
return;
}
}
if (pongleft < 0)
{
meHitSound.Play();
_dx *= -1;
}
if (pongtop <= _linepady ||
pongtop + PongBall.Height >= PongCanvas.Height - _linepady)
{
meDeflectSound.Play();
_dy *= -1;
}
Canvas.SetTop(PongBall, pongtop + _dy);
Canvas.SetLeft(PongBall, pongleft + _dx);
}