Problem Set 9
Up Problem Set 0 Problem Set 1 Problem Set 2 Problem Set 3 Problem Set 4 Problem Set 5 Problem Set 6 Problem Set 7 Problem Set 8 Problem Set 9 Problem Set 10

 

Threads and Animation:
A Simple Video Game

Due 11:59pm on Friday, December 1

  1. Modify Example 13.5 on Page 540 so that the ball bounces, instead of repetitively moving from one spot to another.  In other words, give the ball some initial horizontal and vertical velocity, but when it reaches the edge of the form, it should bounce back at the appropriate angle, and continue bouncing around ad infinitum.

    (For aesthetics, I suggest drawing a line above the buttons at the bottom, and have the ball bounce on that line, so that it doesn't interfere with the buttons.  Also, I would suggest reducing the "sleep time" to about 30 milliseconds to make the animation a little smoother.  Finally, I recommend using the "double-buffering" method talked about in Section 13.3.  This is very easy -- just add the code:

        SetStyle(ControlStyles.DoubleBuffer |
                 ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.UserPaint,
    true
    );

    to your "Form1" constructor.)
     

  2. Add a button called "Reset" to the above problem that clears the ball from the screen and restarts it at a new random location, and moving in a random direction.  If necessary, resize or move the other three buttons to allow for this fourth button.

    (Note: You should add simple exception handling to each of your four buttons!  There are certain sequences (such as trying to resume an aborted thread) that will throw an exception.  Your handler can just ignore the exception, since in fact, from the user's perspective, there's nothing wrong.)
     
  3. Add a "Score" label that starts at zero when "Reset" is pressed, and then increments by one every time the user successfully clicks the mouse on the moving ball.  Place the Score label somewhere that doesn't interfere with the animation or the other buttons (suggestion: place two buttons one on top of the other to the left of the Score label, and two buttons one on top of the other to the right).
     
  4. Extra credit:  Once the score reaches five, increment the velocity of the ball.  Once it reaches 10, increment it again, and so on.  Then see how well you can play your own game!

Turn in one program that implements all three features described above, plus the extra credit if you choose to do so (please indicate in the program comments whether or not you implement the extra credit).

Note: Some of you might run into the problem that, when you terminate your program before pressing the "Abort" key, the next time you try to compile (build) your program, VS.NET will complain that it cannot write onto the file.  To solve this problem, add the line "t.Abort();" just above the line "base.Dispose(disposing);" in the Dispose method that is automatically generated by VS.NET.  In other words:

protected override void Dispose( bool disposing )
	{  if( disposing )
		{  if (components != null)
			{  components.Dispose();
			}
		}
	   t.Abort();
	   base.Dispose( disposing );
	}

This will ensure that the animation thread is killed before the program terminates.

 Solution.