CSE 203 Discussion 01


1. Objectives

To learn about moving graphic sprites in a window, including the idea of Decimal objects changing value.  (If you have a lot of time, there's enough material here to fill it!  If you begin running out of time, try to make sure you cover points 28-30.)


2. Method (Instructor Demonstrates to Class, Asks Questions Such As, "What do you think will happen?")

  1. Start with 0) My Own/Sprite_Display.kpl from branches/experimental.
  2. File, Save As New Program: Moving_Sprite
  3. Run.  What's in the title bar?
  4. Change program name.
  5. Delete sp2 stuff.
  6. Define Decimal objects Xpos and Ypos.
  7. Change MoveTo to MoveTo ( Xpos, Ypos )
  8. Run.  What's in the title bar?  Where is the flag now?  Why?  I'm interested in moving this flag across the window.  It looks like this image is arranged to move from right to left: that's how the relative motion of the air would push it, right?  So I want to start this image over on the right-hand side of the screen.  What "X" value would be good for doing so?
  9. Insert above MoveTo:
    Xpos = 500
    Ypos = 200
  10. Run.  Good.
  11. Insert after show:
    Xpos = Xpos - 100
    first_sprite.MoveTo( Xpos, Ypos )
    What do we expect to see when we run it?
  12. Run.  Okay, I think we saw two images.  Could we simulate motion my doing this same thing more times?
  13. Copy that last segment four more times.  (Ctrl-C, Ctrl-V)
  14. Run.  Wow!  That was fast!  Let's try to slow it down.
  15. Delete those four copies, insert Delay( 15 ) prior to MoveTo, and copy that four times.
  16. Run.  Still pretty fast.  The question is how much to slow it down.  What is the unit of time for this number in method Delay()?
  17. Use "Spell" from the toolbar (Replace from the Edit menu) to change Delay( 15 ) to Delay( 150 ) everywhere.  (Match whole word.)
  18. That made a difference.  I wonder which value for Delay() we will end up preferring?
  19. Well, if we're going to experiment with this, using Replace from the Edit menu could get old after a while.  Let's declare an Integer object, waiting_time, and use it in all our calls to Delay().  We can even give a value to waiting_time in the declaration:
    Define waiting_time As Integer = 150
    Doing so also works for Decimal objects, so let's make a similar change to Xpos and Ypos.
  20. Change the 150 to waiting_time with Edit, Replace.
  21. Run.  That's the same behavior as before, but now we're ready to experiment.
  22. Change the 150 in the declaration to 50.
  23. Run.  Oh, yeah, that was faster.
  24. Change 50 to 500.
  25. Run.  That's almost slow enough to talk about it while it's happening.
  26. We have another magic number sitting around: 100 pixels.  That's how far the flag moves each time.  We could declare a Decimal object for that, too!  What could we call it?  (Use the student's answer.  Maybe someone will call it flag_speed.)
  27. Run it with flag_speed of 100, then run it again with 10.  Okay, there's some interaction between waiting_time and flag_speed to determine the speed the viewer perceives.
  28. Ask a student: what's the repeated action in this program?
  29. Let's give this repeated action to Phrogram just once and ask it to repeat it many times until we tap the Escape key.  (While Not IsKeyDown( Escape ))  We'll change the amount to subtract from Xpos from 10 to 1 and change the delay from 500 milliseconds to 15 milliseconds.
  30. Run.  Tap Esc.
  31. Change the 15 milliseconds to 1 millisecond.  Is it any faster when we run it?  (No.)
  32. Change the 1 millisecond to 0 milliseconds.  Is it any faster when we run it?  (Yes!)  Where did the flag go?  Let's tap Esc to stop the program.  So there's a big cut-off between 0 and 1 millisecond in Delay()'s behavior.
  33. Comment the Delay() out.  What happens when we run it?  (That's faster still!) 
  34. Okay, let's put a delay back in.  Now, I've been experimenting to study Delay()'s strange behavior, and discussed it with a colleague.  We think the interrupt timer is capable of interrupting processing only in multiples of 1/60th of a second.  How many milliseconds is that?  About 16.67.  As it turns out, Delay() tends to have the same amount of delay for values between 1 and 15.  It also has the same amount for values between 17 and 31.  So, this number 16 is somehow special for Delay().  The closest to a right answer we get for the quickest delay is for Delay( 15 ), so that's what I use.
  35. Experiment with variations on saying the same thing in the continuation condition of the While loop: fully qualified names and comparison with a Boolean constant.