Take the 2-minute tour ×

I'm currently programming a game of Pong. I have the majority of the game completed but I've run into an annoying problem. I'm using a dispatcherTimer with priority set to Send and time span set to 1 millisecond. I'm animating the rectangle using up to dx=9 and dy=9 in order to make the ball move fast enough. Because of the large pixel jumps the ball appears skip across the screen instead of a smooth travel. According to math in 1 millisecond per cycle this ball should be moving MUCH faster than it is. I need to update the ball more often and move it by less...

Are there any suggestions on a better method to do this? Here is a snippet of what I have...

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);
         lblRightPoint.Content = _rightpoint.ToString();

      else // The ball went past the paddle without a collision
         lblLeftPoint.Content = _leftpoint.ToString();

         if (_leftpoint >= _losepoint)
               LoseHappened("You Lost!!");

   if (pongleft < 0)
      _dx *= -1;

   if (pongtop <= _linepady ||
         pongtop + PongBall.Height >= PongCanvas.Height - _linepady)
      _dy *= -1;
   Canvas.SetTop(PongBall, pongtop + _dy);
   Canvas.SetLeft(PongBall, pongleft + _dx);
share|improve this question
Just a random thought, but if you are updating the visual in the tick handler, you might try calling InvalidateVisual on the canvas or ball afterwards –  JerKimball Mar 19 '13 at 17:00
Dude, I don't know what you're doing, but this is not how you're supposed to code in WPF. your code looks too winforms, and also there's no way you will have the 1 ms refresh you're after. the regular rate for any UI is between 30 and 60 fps. –  HighCore Mar 19 '13 at 17:01
Normal minimum timer resolution is 15ms. 1ms would equal 1000fps. Try something more realistic like 50 to 100ms which will be 20 or 10 fps respectably. You will still have some issues with repeatability –  Mark Hall Mar 19 '13 at 17:08
I tried the InvalidateVisual but it doesn't seem to change anything. I'm not after a 1 ms refresh rate...but I have to set my Timer to 1 ms to get my ball to move fast enough. Completely illogical I know...I feel like the Timer is super inaccurate or something. I just need to update the ball more often and move it by less it seems. And yes I'm new to WPF and used to doing everything manually. –  konakid Mar 19 '13 at 17:29
add comment

1 Answer

Instead of doing the movement in a timer callback, you may use one of the animation techniques that are built into WPF.

Start reading Property Animation Techniques Overview, perhaps with special attention to the last section Per-Frame Animation: Bypass the Animation and Timing System.

Then you may proceed to How to: Render on a Per Frame Interval Using CompositionTarget.

share|improve this answer
add comment

Your Answer


