![]() |
![]() |
|
home—lectures—recipe—exams—hws—D2L—breeze (snow day)
Due Sep.30 (Mon) in class,
Note: #1-5 and #14 are due Mon. in class (D2L and hardcopy). The remainder are due Wed. (Wed.'s hardcopy doesn't need to include any of Monday's work, and vice versa — I actually prefer leaner printouts.)
Note: I will also accept hw03b up through the start of class Fri., at a 10%-late penalty.with hard copy at the start of the following class. Your name and the assignment-number must be in a comment at the start of the file, and your hardcopy must be stapled. All functions/data must include the steps1 of the-design-recipe—the design recipe: final version. In particular, test cases alone might be worth half the credit for a function.
You may download and
Reading:
The following questions relate to the beginning stages of designing a Pac-man-like game.
For this Homework, you'll submit two files:
a .java and
hint: To represent directions (or keypresses, or both), use the strings"left" ,"down" , etc.; rather than symbols, and rather than"west" ,"south" , etc.. This will play nicely with the keypress events of the2htdp/universe library. This will be a union type.
pro tip: Rather than keep track of both how wide-open the mouth currently is and whether it's opening or closing, I recommend being a little sneaky and just keep the pacman's “age”. Then you can use that number to generate how wide open the mouth should be, by taking thecos ine of the age (or, call any other function2 which maps numbers to [0,1]).3
(5pts) In both racket and in Java: Write a function that converts a pacman's direction to an x-displacement (“dx”), and another to compute the y-displacement (“dy”).4 That is, heading left corresponds to an dx of +1 and a dy of 0, while heading north corresponds to a dx of 0 and a dy of -1. The function should take in just the direction, not an entire pacman.
(You might have functions
(5pts)
In racket (only), write the function
(For now, you can have the keypress take effect immediately, although in our final version we may “store” the keystroke until the next time the pacman can reaches an intersection.)
All the above should have their tests, as well as signatures and (brief) purpose statements. Only after all tests pass, the following should work (in racket):
(require 2htdp/universe) (big-bang some-initial-pacman [on-key pacman-handle-key] [on-tick move-pacman] [to-draw draw-world]) |
(6pts) To do collision detection, a suitable helper function would be to detect when two rectangles overlap (e.g. a rectangle and the pacman's bounding box).
Write test cases and code for
cool hint, from robotics: We can reduce this problem to a simpler one: checking if an expanded version of the first rectangle merely contains one particular point: the southeast corner of the second rectangle.
For example: consider a 20x30 rectangle whose northwest corner is at (500,400) and a second rectangle which is 80x60. These two rectangles overlap exactly when the second rectangle's southeast corner is inside the (20+80)×(30+60) rectangle rooted at (500,400).
(check-expect (count-bigs 7 empty) 0) (check-expect (count-bigs 7 (cons 5 empty)) 0) (check-expect (count-bigs 7 (cons 7 empty)) 0) (check-expect (count-bigs 7 (cons 9 empty)) 1) (check-expect (count-bigs 7 (cons 3 (cons 7 empty))) 0) (check-expect (count-bigs 7 (cons 9 (cons 7 empty))) 1) (check-expect (count-bigs 7 (cons 3 (cons 9 empty))) 1) (check-expect (count-bigs 7 (cons 8 (cons 9 empty))) 2) (check-expect (count-bigs 7 (cons 3 (cons 7 (cons 8 (cons 2 empty))))) 1) |
(check-expect (map-sqr empty) empty) (check-expect (map-sqr (cons 7 empty)) (cons 49 empty)) (check-expect (map-sqr (cons 9 (cons 7 empty))) (cons 81 (cons 49 empty))) |
1 Your final program doesn't need to include any "transitional" results from the template: For example, you don't need the stubbed-out version of a function from step 5. However, you should still have passed through this phase. ↩
; sawtooth : integer?, (and/c integer? positive?) -> real? ; Return a number between 0 and 1. ; As x increases, it's a rising-then-falling pattern: /\/\/\/\... ; with a period of 2 * `period/2`. ; (bug: this should allow real inputs, not just integer.) ; (define (sawtooth x period/2) (/ (abs (- (modulo (- x period/2) (* 2 period/2)) period/2)) period/2)) (check-expect (sawtooth 0 100) 0/100) (check-expect (sawtooth 1 100) 1/100) (check-expect (sawtooth 2 100) 2/100) (check-expect (sawtooth 99 100) 99/100) (check-expect (sawtooth 100 100) 100/100) (check-expect (sawtooth 101 100) 99/100) (check-expect (sawtooth 102 100) 98/100) (check-expect (sawtooth 199 100) 1/100) (check-expect (sawtooth 200 100) 0/100) (check-expect (sawtooth 201 100) 1/100) (check-expect (sawtooth 300 100) 100/100) (check-expect (sawtooth 301 100) 99/100) (check-expect (sawtooth 400 100) 0/100) (check-expect (sawtooth 401 100) 1/100) |
3 Keeping track of age is definitely a hack, so that we can collapse two fields to one, but it feels relatively natural. It also suggests that we can use a pacman's age later on (for calculating score, or having the pacman evolve, or ...). Hopefully we won't want to, in the future, need to know whether the mouth is waxing or waning; if so we'll end up with uglier and less understandable code than the natural, straightforward two-field represention. ↩
4If you are already representing the direction via x- and y-offsets, then instead have a function that translates keypresses into the direction(s). ↩
5 You are encouraged to allow and test for pacman wrapping around the screen, but it won't be required; we can choose to later create walls which won't allow wrapping around. … On the other hand, it's only a few extra function calls and tests to support this feature. ↩
6In a real project, you'd use something like
7
Please distinguish between what words/punctation are required, and what gets
replaced with a certain feature.
For example, the syntax of an
8 I'll use “natnum” for “natural number” —0,1,2,…. ↩
home—lectures—recipe—exams—hws—D2L—breeze (snow day)
©2013, Ian Barland, Radford University Last modified 2013.Oct.21 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |