Start on lab 5
cd
cd cs101
mkdir lab5
cd lab1
Your program
Purpose The purpose of this lab is to give you more hands-on experience with designing interactive programs, programs that process structures.
Sample Problem Create a game in which you compete against time to stop a small pumpkin from shrinking into nothingness. Initially, you have a size 200 pixel wide pumpkin. On each tick the pumpkin will shrink. At first the pumpkin will shrink slowly, but as time goes by, it will shrink more quickly. Each time the player clicks the left mouse button, the pumpkin will grow slightly.
Think about your program design before you begin. What will remain constant? What changes? For the former we need basic constant definitions; for the latter we need a data definition.
Part 1 Add your program header and basic constant definitions to your definitions area. Here are some to start with:
Part 2 Define a structure type for the world and then develop a data definition. Recall that initially the world is a 200 pixel wide pumpkin image. While you’re at it, define a constant initial world—you can make it more elaborate later.
(define-struct shrinking [size time])
;; a structure: (make-shrinking Number Number)
;; interpretation: (make-shrinking s t)
combines the
;; size s
of the pumpkin in the world and the running; you can use size in pixels, or I used this to represent a size factor that could be used with scale
;; time t
of the world
Stop! Interpret these last two lines.
Sample Problem You know you need a rendering function. Big-bang applies it to the current state of the world every time it wishes to draw something. Make a wish:
; WorldState -> Image
; draws a pumpkin whose scale depends on the WorldState
(define (render ws)
BACKGROUND)
You also need events to trigger transitions between game scenarios. Recall that the world changes on clock ticks and on mouse clicks. This tells you which event handling functions your big-bang needs.
From that you can create your wish list. Remember to give a signature, purpose statement, and meaningful name for each function.
;; WorldState -> WorldState
;; creates a new WorldState in which time has been
;; incremented by 1 and the size has decreased
(define (shrink-pumpkin an-ws)
an-ws)
;; WorldState MouseEvent -> WorldState
;; creates a new WorldState in which the size is slightly
; larger than in world, if the left mouse button is pressed.
(define (grow-pumpkin an-ws m)
an-ws)
Sample Problem Define the main function, which sets up the event handlers and the rendering function.
;; WorldState -> WorldState
(define (main worldS)
(big-bang worldS
(to-draw render-pumpkin)
(on-tick shrink-pumpkin)
(on-mouse grow-pumpkin)))
Should the game ever stop? If so, how should main express the fact?
Step 1 Define 3 numeric constants, SHRINK-TIME0, SHRINK-TIME1
, and SHRINK-TIME2
such that
(< SHRINK-TIME0 SHRINK-TIME1 SHRINK-TIME2)
These constants represent times in the game and shrink-world uses these times to determine how quickly to shrink the world.
Step 2 Design the function shrink-pumpkin
, which takes a WorldState
. 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. Notice there are two!
You may choose by how much the pumpkin should shrink (hint: shrink = scale) in each interval.
Step 3 Design the function grow-pumpkin
, which takes a WorldState
, an x
coordinate, and y
coordinate, and a MouseEvent
. If the mouse button has been pressed, then the returned WorldState
should have a slightly larger size than the given one.
You may choose by how much the pumpkin 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.
Save your game—that will be the first file you will submit. Now save it to a new name so that you have a copy of Program 1. MAKE SURE YOU DO NOT OVERWRITE PROGRAM 1!
Modify your WorldState 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
.
Submit both Program 1 & 2 on Moodle.