On this page:
Shrinking Balloons
Chip the Cheap Sheep
Extra Problems

Lab 3 The World

home work!

Purpose The purpose of this lab is to give you some hands-on experience with designing [interactive] programs and programs that process structures and unions (itemizations) of data. Students should know how to define constants, create data and structure definitions, create the main function for world programs, and create/process wish lists.


Shrinking Balloons

See design of world programs for background

Your TA has created an elaborate backstory for the below exercises, and you completely ignore it before jumping straight to the instructions on how to do the exercises.

Sample Problem Create a game in which you compete against time to stop a small red circle from shrinking into nothingness. Initially, you have a size 50 red circle. On each clock tick the circle will shrink. At first the circle will shrink slowly, but as time goes by, it will shrink more quickly. Each time the player clicks the left mouse button, the circle will grow slightly.

Draw a time-indexed series of scenes from the game. Discuss transitions from one scene to next. What stays the same (dimension of game canvas)? What changes? For the former we need basic constant definitions; for the latter we need a data definition.

With the next few sample problems, your TA will break down the above problem into smaller pieces according to the design recipe for world program.

Sample Problem Add appropriate constant definitions to your definitions area. Here are some of our choosing:
(require 2htdp/image)
(require 2htdp/universe)
(define BACKGROUND (empty-scene WIDTH HEIGHT))

Sample Problem Define a structure type for the world and then develop a data definition. Recall that initially the world is a size 50 red circle. While you’re at it, define a constant initial world.

(define-struct shrinking (size time))
; A SW is a structure: (make-shrinking Number Number)
; interpretation: (make-shrinking s t) combines the size s
; of the circle in the world and the running time t of the world
(define initial-world (make-shrinking 50 0))
(define intermediate-world (make-shrinking 50 10))
Stop! Interpret these last two lines.

Sample Problem big-bang requires a rendering function. It uses it to re-draw the world every time something changes. Add the function to your wish list:
; SW -> Image
; draws a red circle whose size depends on the world
(define (draw-world world)
  BACKGROUND) ; TODO: finish function
Think about other things in your game that will change over time. Recall that the world changes on clock ticks and on mouse clicks. You need functions to trigger transitions on these events. This tells you which event handling functions your big-bang call needs.

From that you can create your wish list. Remember to give a signature, purpose statement, and meaningful name for each function.

; SW -> SW
; creates a new SW in which time has been
; incremented by 1 and the size has decreased
(define (shrink-world an-sw)
; SW Number Number MouseEvent -> SW
; creates a new SW in which the size is slightly
; larger than in world, if the left mouse button is pressed.
(define (grow-world an-sw x y mouse-ev)

Sample Problem Define the main function, which “composes” the handlers and the rendering functions.
; SW -> SW
(define (main world0)
  (big-bang world0
            (to-draw draw-world)
            (on-tick shrink-world)
            (on-mouse grow-world)))
Should the game ever stop? If so, how should main express the fact?

Exercise 1 Define 3 numeric constants, SHRINK-TIME0, SHRINK-TIME1, and SHRINK-TIME2 such that


These constants represent times in the game and shrink-world uses these times to determine how quickly to shrink the world.

Exercise 2 Design the function shrink-world, which takes a SW. It increments the world’s time and shrinks the world’s size depending on how the world’s time compares with SHRINK-TIME0, SHRINK-TIME1, and SHRINK-TIME2.

Hint That’s two distinct tasks. Did you notice the "two" here?

You may choose by how much the world should shrink in each interval.

Switch roles.

Exercise 3 Design the function grow-world, which takes a SW, an x coordinate, and y coordinate, and a MouseEvent. If the mouse button has been pressed, then the returned SW should have a slightly larger size than the give one.

You may choose by how much the world’s circle should grow.

Remember that mouse events are special strings. Be sure to ignore all mouse events except "button-down".

Play your game. How long can you hold out against time? Change constants to make the game more (less) challenging.


Chip the Cheap Sheep

Switch roles

An animation studio is working on a game called "Chip, the Cheap Sheep". They have some basic idea, and that is to have a sheep running across the screen. Here are some sheep shapes that their artist has drawn:

You should be able to drag the images from the webpage into DrRacket, once they are there you should know what to do. If not save them to the desktop, and use "Insert">"Insert Image..." from the DrRacket menu bar.











Your goal is to create a simple proof-of-concept game with Chip running from offscreen to the (x coordinate of the) point the player clicks on first. Before Chip runs, the screen shows the text


The following exercises take you through the design steps for world programs again.

Switch roles.

Exercise 4 Draw some still frames from the game. Which aspects remain the same? Which change?

Exercise 5 Gather constant definitions for the properties of the world frames that remain the same. Here are some:
(define CHIP1 ...)
(define WIDTH (* 20 (image-width CHIP1)))
(define HEIGHT (* 2 (image-height CHIP1)))
(define PLAYGROUND (empty-scene WIDTH HEIGHT))
(define TXT (text "click the mouse to get Chip running" 33 "pink"))

Exercise 6 Develop a structure type definition so that your program can keep track of all the quantities that change between frames.

Hint In addition to where Chip currently is, your program must know where Chip is going. After all, at the very beginning you have no clue where Chip is supposed to go.

Develop a data definition so that you can represent the state of the game. Remember that data definitions come with an interpretation and sample data.

Exercise 7 Develop a wish list. You know you need a function that renders the current state of the game. What else do you need? Which kind of events make the game transition from one state to another?

Exercise 8 Create a main function to start your world program. Here is a sketch:
; PositiveNumber -> ... class of data that big-bang manages ...
; given the animation rate, play a game of Chip, the cheap sheep
; NOTE: by supplying the animation rate you can slow down the game
(define (chip rate)
  (big-bang initial-world
            [to-draw ... what name did you choose? ... rate]
            ; add your event handlers here))
Use your answer to the previous exercise to determine which event handlers you need.

Exercise 9 Develop a template for functions that work on the state of the Chip game.

TAs: time for a short lecture on: comment with "#;" and copy-paste to re-use the template from now on.

Exercise 10 Design the function pick-chip. It takes a number between 0 and 3 and returns the corresponding image of Chip from the sequence above.

Exercise 11 Design the rendering function.

Exercise 12 Design the clock tick event handler. The function takes world and returns a world where Chip is moved to the left, toward his goal. Use the function pick-chip to change the image according to where Chip currently is. Hint Check out modulo and/or remainder.

Switch roles.

Exercise 13 Design the mouse click handler. The function that takes a world, x and y coordinates, and a mouse event and returns a new world. The new world will have the mouse’s coordinates as Chip’s new destination and Chip teleported from offscreen to a location close to the right edge.

Remember that mouse events are just strings. Be sure to ignore all mouse events except "button-down".

Exercise 14 Design a function that determines whether Chip is close to his destination. We let you choose what "close" means. Then equip the chip function from above with a stop-when clause.

Extra Problems

If you finish the problems before lab is over, try modifying your game to include a Difficulty. Define a data and structure definition for Difficulty. A Difficulty consists of 3 successively larger Numbers, representing times. Modify your data and structure definitions for your world to include a Difficulty. Modify shrink-world to use the Difficulty instead of the constants SHRINK-TIME0, SHRINK-TIME1, and SHRINK-TIME2.

When you finish that, modify the game to track how quickly the size is shrinking or growing, and change the color of the circle to blue if the size is growing faster than it is shrinking, yellow if the size is growing exactly as fast as it is shrinking, and red if the size if shrinking faster than it is growing.