O'Reilly logo

Microsoft®C# Programming for the absolute beginner by Andy Harris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling the Passage of Time

The program has a timer control used to pace the game. When the timer ticks, three things happen. The clock is updated, showing how many seconds are left of playing time. The player picture boxes are moved randomly, and the opponent picture boxes are also moved randomly. It shouldn’t surprise you that each of these three tasks is relegated to a method, the updateTime(), movePlayers(), and moveOpp() methods.

private void timer1_Tick(object sender, System.EventArgs e) {

  updateTime();
  movePlayers();
  moveOpp();

} // end timerTick

Again, encapsulation comes to the rescue. The timer_tick() method calls three other methods to do all the work, but it’s easy to see from the tick method exactly which tasks occur whenever the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required