CSE 203 Homework 4

You may complete this homework, as any homework in this course, either alone as an individual, or in partnership with one other person.  Please make sure every author's name is listed at the top.
  1. Complete Closed Lab 3 if you haven't already done so.
  2. Write (or print) out answers to all questions asked below (8, 9, 11, 12, and 13).
  3. In Closed Lab 1, Homework 1, and Quiz 1, we encountered the idea that the order in which a programmer lists calls to methods matters.  This idea is called sequencing.  It's one of the three big ideas in what's called program control.  In class on Monday, and in Homework 3, we told the program to repeat an action as long as the Esc key is not tapped down.  This repetition idea is called iteration.  The programming construct that describes iteration is often called a loop.  We've been using while loops: the keyword involved is While.  The iteration happens as long as the continuation condition stated right after this keyword is true.  The repeated action performed is often called the body of the loop.  We introduce the third big idea in program control, selection, below.
  4. Until now, when our while loops have moved sprites around the window, those sprites have migrated out of the window, unless we stopped the program before that happened.  We could change this behavior by making our program detect when the sprite has reached an edge of the screen, and taking some action when that occurs.  This means that, if the sprite has not reached an edge of the screen, we want the program to do nothing extra, but, otherwise, take the special action.  That is to say, we want the program to select between two alternative actions (sometimes selecting between doing something and doing nothing, sometimes between doing something and doing something else).  In our case, maybe we want the sprite simply to stop moving when it reaches an edge of the window.  Then, perhaps the action we should take is to change the sprite's speed to zero.
  5. Let's begin with the following program (OSU\003 Animated_Sprite.kpl):
    // Author: Wayne Heym
    // Date: September 18, 2006
    Program Animated_Sprite
    Method Main()

    Define Xpos As Decimal = 100
    Define Ypos As Decimal = 200
    Define UFO As Sprite
    Define waiting_time As Integer = 15
    Define UFO_X_Speed As Decimal = 2
    Define UFO_animation_timeline As Integer [] = { 100, 100, 100, 100, 100, 100 }

    UFO.Load( "UFO.gif" )

    UFO.AnimationTimeline = UFO_animation_timeline

    UFO.MoveTo( Xpos, Ypos )
    UFO.Show()

    While Not Keyboard.IsKeyDown( Keys.Escape )
    RefreshScreen()
    DoEvents()
    Xpos = Xpos + UFO_X_Speed
    Delay( waiting_time )
    UFO.MoveTo( Xpos, Ypos )
    End While

    End Method

    End Program
  6. Now modify it by inserting the following If-Then selection statement as one of the first of the repeated actions of the while loop, somewhere before the assignment statement "Xpos = Xpos + UFO_X_speed":
                If ScreenWidth() < Xpos + UFO.Width + UFO_X_speed Then
    UFO_X_speed = 0
    End If
  7. Try it out to see what happens.  Does the UFO keep spinning at the edge of the screen?  Tap the Esc key to stop this spinning.  Close the program window.
  8. Use the hover captions to find out which Library method ScreenWidth() is in.  Which Library is it?
  9. How about "UFO.Width"?  Now that we know what library UFO.Width is in, we turn our attention briefly to what sort of thing it is.  Because the hover caption doesn't contain any parentheses (the characters '(' and ')') when it says "Sprite.Width As Integer", we can infer that UFO.Width is neither a Method nor a Function.  It is a Property.  If you type "UFO." in your program here, Intellisense should give you a drop-down list.  In that list, Properties are shown with the symbol property symbol, and Methods and Functions are shown with the symbol method or function symbol.
  10. Xpos refers to the left-hand side of the sprite.  We add UFO.Width to it to find the coordinate of the sprite's right-hand side.  Then, we want to know if adding the speed to that would place the right-hand side of the sprite past the window's right-hand border (ScreenWidth()).  If so, we want to change the speed to zero.
  11. What other name is often given to a loop's repeated action?
  12. What are the three big ideas composing program control?
  13. Well, we took care of the right-hand edge of the window.  There are three more edges.  Make your program handle all four edges, and test it.  Print out your program and attach it to this homework.
  14. In Closed Lab 4 tomorrow, we'll be having our sprite "bounce" off the edge of the window instead of stopping there.  Think carefully about how you would do this, even writing down snatches of program to tell yourself clearly how you would do this.  The more carefully you prepare here, the easier closed lab will go.