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?")
- Start with 0) My Own/Sprite_Display.kpl from
branches/experimental.
- File, Save As New Program: Moving_Sprite
- Run. What's in the title bar?
- Change program name.
- Delete sp2 stuff.
- Define Decimal objects
Xpos and Ypos.
- Change MoveTo to MoveTo ( Xpos, Ypos )
- 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?
- Insert above MoveTo:
Xpos = 500
Ypos = 200
- Run. Good.
- Insert after show:
Xpos = Xpos - 100
first_sprite.MoveTo( Xpos, Ypos )
What do we expect to see when we
run it?
- Run. Okay, I think we saw two images. Could we
simulate motion my doing this same thing more times?
- Copy that last segment four more times. (Ctrl-C, Ctrl-V)
- Run. Wow! That was fast! Let's try to slow it
down.
- Delete those four copies, insert Delay(
15 ) prior to MoveTo, and copy that four times.
- 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()?
- Use "Spell" from the
toolbar (Replace from the
Edit menu) to change Delay( 15 ) to Delay( 150 ) everywhere. (Match whole word.)
- That made a difference. I wonder which value for Delay() we
will end up preferring?
- 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.
- Change the 150 to waiting_time with Edit, Replace.
- Run. That's the same behavior as before, but now we're
ready to experiment.
- Change the 150 in the declaration to 50.
- Run. Oh, yeah, that was faster.
- Change 50 to 500.
- Run. That's almost slow enough to talk about it while it's
happening.
- 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.)
- 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.
- Ask a student: what's the
repeated action in this
program?
- 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.
- Run. Tap Esc.
- Change the 15 milliseconds to 1 millisecond. Is it any
faster when we run it? (No.)
- 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.
- Comment the Delay() out. What happens when we run it?
(That's faster still!)
- 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.
- 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.