![]() |
![]() |
|
Due:
Your submitted file should be named “hw04.rkt”, plus any .java files.
Standard instructions for all homeworks:
Consider the following information, which might be part of a sports-simulation:
A team is:
- a name (non-empty string), AND
- an offense rating (real number), AND
- a defense rating (real number).
(5pts) Give a data definition (including examples of the data, and the template for any function which processes a team), as per steps 1-3 of the design recipe. Give at least three examples of a team.
(5pts)
We’ll say one team is “greater” than another if its offense is
higher than the other’s defense, and its defense is higher than the other’s offense.
Write the function
Familiarize yourself with the arcade game Pac-Man, such as this example. We will write a simplified version which only needs to deal with: the pacman, eating dots, ghosts, and walls. It does not need to deal with: score, having more than one level, number-of-lives, fruit, or ghosts becoming temporarily vulnerable.
For this Homework, you'll submit two files: a .java and .rkt program file.
We start by representing the pacman, and writing a method involving them.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 if you later need to know how wide the mouth should be, you can take thecos ine of the age (or, call any other function2 which maps numbers to [0,1]).3
Remember to include both field-names and their types, in racket as well as Java.
(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
The following problems only need be done in racket.
hint:place-image is a handy function; it is similar tooverlay/xy except that it crops the result to the background.
hint: For test-cases, include drawing a dot that is: (a) near the center of a small image; and (b) one that is mostly off the left-edge but has just a few pixels showing.
Note: Here’s an image you can (modify to) use in your test-cases, in addition to a solid rectangle or whatever else you might choose: house-with-flowers.rkt. If you place this file in the same directory as other functions, you can just(require "house-with-flowers.rkt") , and then use its exported id (house-with-flowers , coincidentally). You don’t need to print this file, but do submit it on D2L so that I can run your program.
I suggest using simple shapes (like mere rectangles) for
all your drawing-functions for the game.
You are welcome to be much fancier, though it won’t earn you additional credit.
You are welcome to use jpg/png/bitmaps, but make sure
you have the legal right to copy them —
e.g. from sites like
classroomclipart.com
or libraries like
.
Please cite your source, of course!
(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 glide-pacman] [to-draw draw-world]) |
; 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) |
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |