![]() |
![]() |
|
Due:
Standard instructions for all homeworks:
Complete the short fill-in-the-blank questions on D2L. Feel free to make sure they run as actual code, since you'll need it for part (b) anyway.
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
Asteroids,
such as
this example flash not required.
We will write a simplified version which
only needs to deal with:
the ship, bullets, and asteroids.
It does not need to deal with:
score,
having more than one level,
number-of-lives,
the spin of an asteroid,
or
For this Homework, you'll submit two files: a .java and .rkt program file.
We start by representing the ship, and writing a method involving them.tickis an abstract amount of time, but it’s convenient to declare it as the amount of time between two frames).
Remember to include both field-names and their types, in racket as well as Java.
hint: You'll want to handle wrapping around the board. To do that, you need to know the size of the board. Use named-constants for that. Your unit-tests can refer to those named-constants; for convenience they can also assume that each dimension will be at least 100 pixels — i.e. they can assume no wrapping-around will happen for such values. Also, note thatmodulo can help handle wrapping (or, if not using integers,modulo/real as provided in student-extras.rkt).
The following problems only need be done in racket. You are welcome to review and/or use (with citation) any part of ship-soln.rkt.
(5pts)
In racket (only), write the function
hint: Playing the demo above, we see there are only three key-presses that actually do anything.
: You can compare twokey-event? s withstring=? , butkey=? (from(require 2htdp/universe) ) is more appropriate. Equal credit will be awarded, either way.
calculating acceleration: You might want to review notions of distance vs. velocity vs. acceleration; one explanation is at physicsclassroom.com. Also, you can see the note/formula I put inside ship-soln.rkt.
Note: We will not use struct-inheritance2 This means some of our functions will essentially be repeating code, which is indeed annoying.
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 asteroid 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
or libraries like
.
Please cite your source, of course!
hint:How can you get a ship and an asteroid both drawn onto the same background-board? We'll clearly want to call
draw-ship anddraw-asteroid , but: if we call each of these and pass it a blank-background, then we'll have have two images each with a background, and there isn't a way to combine these into a single image.Instead: give
draw-asteroid the background-board (and get back an image including the asteroid); then when callingdraw-ship , for that 2nd argument pass it the image you just got from callingdraw-asteroid ! This exactly how we overlayed two different text-images onto a background meme, in a previous homework.
All the above should have their tests, as well as signatures and (brief) purpose statements.
Optional: viewing the simulation: Only after all tests pass, the following should work (in racket): If you want to see (just) your ship in action, you can writedraw-ship-onto-blackness : ship → image (just a quick wrapper arounddraw-ship of course), and then:In the next homework, we'll make a single
(require 2htdp/universe) (big-bang some-initial-ship [on-key ship-handle-key] [on-tick move-ship] [to-drawdraw-worlddraw-ship-onto-blackness])worldstruct to hold everything in the game, at which point we'll have an updateddraw-world (and,move-world ,world-handle-key ) which will be passed tobig-bang .
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |