On this page:
Signatures
Purpose Statements
Animations and big-bang
Additional practice:   Updating the game
Additional practice:   Launching a rocket
6.6

Lab 1 The Universe Begins

home work!

Purpose: The purpose of this lab is to give you some hands-on experience with the BSL programming language and with the DrRacket programming environment for BSL.

Textbook references: Preface (DrRacket and the Teaching Languages), Prologue: How To Program, Chapter 1: Arithmetic, Chapter 2: Functions and Programs (up to but not including 2.5)

You will need to place the following 2 lines of code at the top of your file:
(require 2htdp/image)
(require 2htdp/universe)

Signatures

Recall from lecture that a signature explains what type of data can be the inputs and outputs of a function. We have given some examples of functions with signatures below.

; my-function : Number Number String -> Number
(define (my-function x y z)
  (+ (* x y) (/ (string-length z) 2)))
 
; my-other-function : Image String -> Boolean
(define (my-other-function a b)
  (or (< (image-width a) (string-length b))
      (> (image-height a) 100)))

Exercise 1 Provide a signature for each of the following functions:
(define (function1 x) (substring "abcdefghijklmnopqrstuvwxyz" 0 x))
 
(define (function2 y z) (+ (string-length y) (image-width z)))
 
(define (function3 a b c)
  (cond [a (overlay b c)]
        [else (overlay c b)]))

Purpose Statements

Recall from lecture that a purpose statement is a SHORT statement about what your function does. The length of your purpose statement should not exceed 100 characters.

Exercise 2 Provide a purpose statement for each of the following functions:
(define (function4 a b)
  (string-append (substring a 0 1) (substring b 0 1)))
 
(define (function5 x y z)
  (or (= x y) (= x z)))
 
(define (function6 c) (substring "abcdefghijklmnopqrstuvwxyz" c (add1 c)))

Animations and big-bang

Recall from lecture that big-bang allows us to create animated programs and interact with them. You may wish to take a look at the documentation for big-bang and the explanation here of how it works. The TAs will go over the various handlers again in lab before you begin.

We want to design a program where we can move a sheep around on the screen using the left and right arrow keys. We will break this down into steps. You should think through these steps whenever you are asked to design a big-bang program.

Step 1: What stays the same?

The things that stay the same will have to be constants. Below are some examples of constants you might want to add to your program, but you are welcome to add more if you want.

(define MY-SHEEP-IMAGE
  (overlay/xy
   (ellipse 40 20 "solid" "Beige") -70 0
   (overlay/xy
    (overlay/xy (ellipse 80 40 "solid" "Beige") 20 20 (rectangle 5 30 "solid" "tan"))
    50 20 (rectangle 5 30 "solid" "tan"))))
(define BG-WIDTH 400)
(define BG-HEIGHT 100)
(define GRASS-IMAGE (rectangle BG-WIDTH (* BG-HEIGHT 1/5) "solid" "SeaGreen"))
(define SKY-IMAGE (rectangle BG-WIDTH (* BG-HEIGHT 4/5) "solid" "SkyBlue"))
(define BACKGROUND-IMAGE (above SKY-IMAGE GRASS-IMAGE))

Step 2: What changes?

Our WorldState will have to keep track of the information that is changing in the world. In our case, that’s the x-coordinate of the sheep on the screen. So for this program the world state should be a number, which represents the x-coordinate of the sheep.

Step 3: Which handlers do we need?

Recall that the handlers we have available are: to-draw, on-tick, on-key, on-mouse, and stop-when. Well, every big-bang program needs a to-draw clause since otherwise we won’t know how to display anything. Then we need to consider when the state of the world changes. We want the sheep to move when we press the arrow keys so it seems we need an on-key clause. Other than that we don’t need any additional clauses.

Step 4: Design your handlers

Exercise 3 Define the function draw-sheep that draws an image of a sheep at a given x-coordinate on the screen. You can also utilize your draw-sheep function from the previous lab if you have it. Which clause does this function belong to?

Exercise 4 Define the function move-sheep that takes a Number, representing the x-coordinate of the sheep, and a KeyEvent representing a key the user has pressed. If the key is "left" we want to subtract from the x-coordinate, but if the key is "right" we want to add to the x-coordinate. Otherwise we don’t want to change the x-coordinate at all. Be sure to include this third case or your program will break when you press any non-arrow key.

Step 5: Put it all together!

Exercise 5 Define the function sheep-program which calls big-bang and uses the functions you defined above in the appropriate clauses. What should be the initial world? Be sure to include a signature for this function. What does it produce? Ask a tutor or TA to double check your signature before you move on.

Additional practice: Updating the game

Exercise 6 Update your game so that the user cannot move the sheep left if it is already at the left edge of the screen and the user cannot move the sheep right if it is already at the right edge of the screen. Which function(s) will need to change?

Exercise 7 Update your game so that initially the user sees a screen that says "press space to play" and only after pressing space are they able to see and move the sheep. What will we have to change about our WorldState?

Additional practice: Launching a rocket

Exercise 8 Design a program that launches a rocket. Your program should wait 5 seconds and then the rocket should move at a steady pace up the screen. The program should end when the rocket is no longer on the screen. Be sure to follow the steps outlined above to design your program.