This assignment is due on Wednesday, 2/13 at 11:59 PM. Submit it using the Handin server as assign
Remember to follow the design recipe for each function. In particular, every type mentioned in a sig introduced by a data definition, except for these well-known types: Number, Image, String, Boolean, MouseEvent, Anything, Posn.
In this assignment and the next assignment, you will build a simple single-player video game. The pl be to escape hungry zombies for as long as possible. In this section, you will build part of the game, player and no zombie. So the world will be drawn as a scene containing just a player icon (for examp green circle).
You will store the position of the player icon in the scene as a Posn. The actual human player will w another position, to avoid being eaten by a zombie. While playing, the player will see their icon mov position of a mouse click (or drag). The player icon has a fixed speed, which is the distance (in pixels) each time tick. The player icon will move in a straight line towards the mouse click position. (And w zombies in the next assignment, they will also move in a straight line, towards the player position.)
Now the problem is, given a player position and a mouse click position, how do you compute the ne position after one tick? You will define a function approach to answer this question. This function will take two positions p and goal and a speed s and return a position p. (In the next assignment, yo same function to compute new zombie positions.)
To find p, here is the basic idea. Determine the direction from p to goal (we will represent a directio whose distance from the origin is 1). The direction from p should be the same, but the distance shou smaller. Youll build up to this idea in this section.
Exercise 1 Design the following three functions:
; posn-sum : Posn Posn -> Posn
; posn-diff : Posn Posn -> Posn
; posn-scale : Number Posn -> Posn
posn-sum computes the sum of two Posns by creating a new Posn from the sum of the x-coordinat of the y-coordinates. posn-diff similarly computes the difference of two Posns by subtracting the coordinate from the first and the second y-coordinate from the first. Finally, posn-scale scales th by multiplying both the x- and the y-coordinates by the given number.
Exercise 2 Here we provide the definitions of three helper functions that compute distance, direction approach. (Note that these definitions may work only as well as your functions from Exercise 1.) Do these definitions.
Provide the missing tests for dist and for approach-helper and the missing purpose statemen approach-helper. If you have trouble understanding what these functions do, write examples a on graph paper.
; dist : Posn Posn -> Number
; computes the distance between two Posns (define (dist p1 p2)
(sqrt (+ (sqr (- (posn-x p1) (posn-x p2))) (sqr (- (posn-y p1) (posn-y p2))))))
(check-expect ??? ???) (check-expect ??? ???)
n K
a m
a e
h
w u
n l
e e
n
t n
noisrucerdnaserutcurtS:6tnemngissA
hcaorppa emaG 1
; direction : Posn Posn -> Posn
; computes the direction from p to goal,
; represented as a Posn whose distance from the origin is 1 (define (direction p goal)
(posn-scale (/ 1 (dist p goal)) (posn-diff goal p)))
(check-expect (direction (make-posn 230 240) (make-posn 200 200)) (make-posn -3/5 -4/5))
(check-expect (direction (make-posn 170 240) (make-posn 200 200)) (make-posn 3/5 -4/5))
(check-expect (direction (make-posn 230 160) (make-posn 200 200)) (make-posn -3/5 4/5))
(check-expect (direction (make-posn 170 160) (make-posn 200 200)) (make-posn 3/5 4/5))
; approach-helper : Posn Posn Number -> Posn ; ???
(define (approach-helper p goal s)
(posn-sum p (posn-scale s (direction p goal)))) (check-expect ??? ???)
(check-expect ??? ???)
Exercise 3 Develop a data definition Pworld and a struct called pworld with two fields. One field st which is the player position. The other field stores a Posn which is the goal position that the player Do not name the fields x or y.
Exercise 4 Define background, a background image for your game. (A 400 x 400 empty scene work to be creative.)
Using background, design a function draw-pworld which takes a Pworld and returns an Image you should have an image for the player positioned on the background according to the player po goal) stored in the input Pworld. (A small green circle works, but feel free to express yourself.)
Exercise 5 Design a function update-pgoal which takes a Pworld, two Numbers, and a MouseEv returns a Pworld. This function update-pgoal will be an on-mouse handler for big-bang. In either a button-down or drag event from the mouse will change the goal position towards player is moving. Any other event leaves the world unchanged.
Exercise 6 Design a function move-player which takes and returns a Pworld. This function move will be an on-tick handler for big-bang. In the game, move-player will move the player clo position stored in the input Pworld. Use approach-helper for now; fix a speed between 2 and 1 tick).
Exercise 7 Design a function run-pworld which takes an initial Pworld and runs big-bang with pworld, update-pgoal, and move-player.
Exercise 8 Try using run-pworld to make sure that player movement is working. If you have done correctly so far, the player will eventually jitter or even crash. This problem is because approach- not handle an important case: Suppose that p and goal are already close: that is, within s pixels. The just equal goal, because the player does not overshoot its destination (and a zombie stops moving wh found its meal).
Design a new function approach that is like approach-helper but handles this case and fixes The first input is the position to be updated. The second input is the unchanging position being appr
m
s e
s
0
h
n
t o
third input is the speed (pixels per tick). The function should return the new position.
; approach : Posn Posn Number -> Posn ; ???
(define (approach p goal s) ???)
(check-expect ??? ???) (check-expect ??? ???)
Your definition of approach should use approach-helper, but make sure the purpose stateme for approach-helper and approach describe their difference clearly. Change move-player approach instead of approach-helper. Try run-pworld again to make sure that player mo working fully.
Calder mobiles are a kind of sculpture that can be represented using a self-referential data definition Mouettes (The Seagulls), 1965. Here are some ways to learn about Calder mobiles:
The Museum of Contemporary Art in Chicago is exhibiting Calder mobiles until March 24. Bot Calder mobiles in this section are from there.
The Saint Louis Art Museum (free!) houses a nice Calder mobile in its collection.
The Calder Foundation Web site shows a variety of Calder mobiles.
Wikipedia describes a mobile as a number of rods, from which weighted objects or further rods objects hanging from the rods balance each other, so that the rods remain more or less horizont
Read about binary trees in the textbook, in part IV Intertwined Data.
Typically, each rod in a mobile has two sub-mobiles hanging off of it. Lets represent such a mobile
definition:
; A Mobile is one of:
; (make-leaf Number)
; (make-rod Mobile Number Number Mobile) (define-struct leaf [weight]) (define-struct rod [lm ld rd rm])
n v
h
u
selibom redlaC 2
In the field names of rod, the letters l r m d stand for left, right, mobile, and distance, respectively. Exercise 9 Define three examples of Mobiles as constants, a leaf and two rods. Call them mobile1,
mobile3.
Exercise 10 Design a function weight that takes a Mobile as input and computes its total weight. F
assume that the connection between the two sub-mobiles of a rod adds no weight.
Exercise 11 Design a function average-leaf-weight that takes a Mobile as input and compute average weight. Youll need a helper function that also takes a Mobile as input.
Exercise 12 Design a function all-balanced? that takes a Mobile as input and returns a Boolea whether it is balanced everywhere. Use the following helper function to compute and compare torqu providing its missing examples as tests.
; balanced? : Mobile -> Boolean
; determines whether the given mobile is balanced at the top (define (balanced? m)
(cond [(leaf? m) true]
[(rod? m) (= (* (rod-ld m) (weight (rod-lm m)))
(* (rod-rd m) (weight (rod-rm m))))]))
Exercise 13 Design a function lighten that takes a Mobile as input and returns a new Mobile that given one except everything weighs half as much as the given one. (So, your all-balanced? funct produce the same result for the given and returned mobiles.)
Exercise 14 Design a function enlarge that takes a Number and Mobile as inputs and returns a ne is like the given one except all the distances are that many times as long as the given one. (So, your w average-leaf-weight, and all-balanced? functions should produce the same results for t returned mobiles.)
Exercise 15 Design a function all-balanced-mobile that takes a positive integer as input and Mobile that has exactly that many leaves and satisfies all-balanced?.
Hint 1: Here is a data definition for positive integers:
; A PositiveInteger is one of: ;-1
; (+ PositiveInteger 1)
And here is the template for a function that processes a positive integer:
; process-positive-integer : PositiveInteger ->
(define (process-positive-integer n) (cond [(= n 1) ]
[else ( (process-positive-integer (- n 1)))]))
Hint 2: If you already have a Mobile that satisfies all-balanced?, then one way to make a Mobil exactly one more leaf and also satisfies all-balanced? is to balance the existing mobile against whose weight is the total weight of the entire existing mobile. So this helper function is useful (but if provide its missing examples as tests):
; counterbalance : Mobile -> Mobile
; adds a leaf to a given mobile in a balanced? way (define (counterbalance m)
(make-rod m 20 20 (make-leaf (weight m)))) Hint 3: Study the excerpt below from The Ghost (maquette), 1964.
n e
h r
a y
Here are the data and structure definitions for a Painting:
; A Painting is one of:
; (make-solid String)
; (make-hsplit Painting Painting) ; (make-vsplit Painting Painting) (define-struct solid [color]) (define-struct hsplit [top bottom]) (define-struct vsplit [left right])
Every Painting can be drawn as a rectangle of any size. A solid Painting is drawn as a rectangle fil with the given color. An hsplit Painting is divided into two rectangles of equal size, one on the on the bottom. A vsplit Painting is also divided into two rectangles of equal size, but one on the l the right. So h stands for horizontal and v stands for vertical.
After you have defined several example Paintings, finish designing the following function so that you them:
; draw-painting : Painting Number Number -> Image
; draws the given painting with the given width and height ; (define (draw-painting p w h) )
The goal of this challenge is to design an interactive big-bang program that allows you to view a Pai update the colors by clicking. For example, when I click on the rightmost rectangle of this picture
t e
n
gnitniap tcartsba :egnellahC 3
I see
.
To get started, we suggest that you design a program which turns any rectangle you click on to a par (e.g. black). This should be done with a function update which takes a Painting, a width, a hei coordinate and a y-coordinate. The latter two inputs are understood as the point inside the rectangle you would change. Note that the functions draw-painting and update do not have the require to be used directly by big-bang; so you will have design two corresponding functions which call th using a width and a height that you pick.
Once you have this working, define an enumeration which contains a selection of colors (your palett function next-color which can be used to cycle the color of a rectangle by repeated clicking.
t g
d e
e
Reviews
There are no reviews yet.