Lab 3h The World
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, use templates, 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.
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.
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, you will break down the above problem into smaller pieces according to the design recipe for world program.
Exercise 1 Add appropriate constant definitions to your definitions area.
Exercise 2 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.
Exercise 3 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:
; World -> 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.
Should the game ever stop? If so, how should main express the fact?
Exercise 4 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.
Exercise 5 Design the function shrink-world, which takes a World. 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.
Exercise 6 Design the function grow-world, which takes a World, an x coordinate, and y coordinate, and a MouseEvent. If the mouse button has been pressed, then the returned World 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.
Exercise 7 Change the world to also have the previous world’s value. Track how quickly the world is growing. Change draw-world to draw a blue circle if the circle is growing faster than it is shrinking, and a red circle if the circle is shrinking faster than it is growing. Use yellow if they are shrinking/growing at exactly the same rate.
Designing Chip the Cheap Sheep
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. Or copy and paste.
0
1
2
3
Switch roles.
Exercise 8 Draw some still frames from the game. Which aspects remain the same? Which change?
Exercise 9 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 10 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 11 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 12 Create a main function to start your world program. Here is a sketch:
; PositiveNumber -> World ; 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 13 Develop a template for functions that work on the state of the Chip game.
Exercise 14 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 15 Design the rendering function.
Exercise 16 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 17 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 18 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.
A Psychology Experiment
You have been hired by the local psychology department to assist with the software for a perception experiment. The lab director would like you to design a world program that will help study how well humans perform visual approximation under a time pressure.
In this experiment, a launch-experiment function is given two posns. A screen appears displaying both of them as two small "DarkGoldenrod" circles. The circles disappear after 3 seconds. The “subject”Psychologists use the word “subject” for the victims that participate in their experiments. then clicks the screen where they think the midpoint of the two posns was. When they click, a "MediumForestGreen" circle appears where they click, along with the original two circles, a line connecting them, and a "LightSeaGreen" circle appears in the actual midpoint. This screen appears for another 2 seconds, and the final output of the launch-experiment function should be the distance from the subject’s approximation to the actual midpoint in pixels.
Exercise 19 Design the experiment described above as a big-bang program.
Pay careful attention to the various phases of the problem: how many are there? How will our functions be able to tell them apart? What information does each one need to keep track of? How will we ensure a certain phase appear for the appropriate amount of time?
Succeeding in this problem will depend on your adherance to the design recipe, especially in the formulation of your data definition(s)!
Note, since you cannot anticipate where a person will click, the launch-experiment function does not need to be tested. Every single helper, however, does.