Ping-Pong


pingp-play.ss

The teachpack pingp-play.ss implements a demonic ping-pong game. The goal is to keep the ball in play as long as possible. The ball bounces on the northern and southern walls but must be kept in play with the paddles at the eastern and western walls. The paddles can be moved with mouse clicks. Occasionally a demon blows the ball into a different direction so that the game becomes more interesting. Enjoy!
The teachpack provides one major operation:
  • go : symbol -> void
    opens a window with a "ping pong" playing field -- plays demonic game
    use first to demonstrate the game to students
  • It also provides four operations that change properties of the game:
  • change-speed : number -> void
    changes how quickly the game is displayed (not the speed of the ball)
  • change-wind : number -> void
    changes how frequently a demon "blows" the ball into a different direction
  • change-width : number -> void
    changes the width of the playing field
  • change-height : number -> void
    changes the height of the playing field

  • Sample session: Set teachpack to pingp-play.ss execute and play:
    > (go 'MyName)
    > (change-speed 120)
    > (go 'MyName)

    > (change-wind 88)
    > (go 'MyName)

    pingp.ss

    The teachpack pingp.ss provides the graphical help functions for a student implementation of the demonic ping-pong game:
  • trace : posn speed mover number -> true
    shows movement of a ball according to mover; for example,
    (trace (make-posn 10 10) (make-vec 1 2) mover 55)
    constructs a 55-unit trace of a ball that starts at (10,10) and moves (1,2) each time unit. The function mover is ternary and consumes the current position, the current speed, and a time period (number).
  • trace-ball : a-ball ball-posn mover number -> true
    shows movement of a ball according to mover; for example,
    (trace-ball (make-ball (make-posn 10 10) (make-posn 1 2)) ball-posn mover 55)
    constructs a 55-unit trace of a ball that starts at (10,10) and moves (1,2) each time unit. The function mover is binary and consumes a ball and a time period (number).
  • landed-on-paddle? : posn -> boolean
    tests whether the position is on the paddle in the current configuration
  • play : make-ball make-speed ball-posn move -> boolean
    opens a window and plays the game with programmer-defined functions
  • The use of the operations is explained in detail in subsection 5.3.