![]() |
![]() |
|
(All times-of-day are start-of-class.)
Standard instructions for all homeworks:
Complete the short fill-in-the-blank questions on the D2L quiz hw04a
.
Your answers should be valid racket expressions.
Do be sure to answer what the question is asking.
Familiarize yourself with the arcade game Snake. We will write our own version, starting with this homework (structs) and finishing with next homework (lists). We will write a simplified version which only needs to deal with: the head, segments, and fruits. However, our snake will not be on a grid: it can wiggle in any direction (steered by the player). To make our lives easy, it will still advance by one segment each tick. If we moved only a few pixels, we'd run in to our our own most-recent segment immediately, which wouldn't be good! It does not need to deal with: score, number-of-lives, having more than one level, or walls/barriers.
There are three types of things (objects) which our program will need to model: heads, segments, and fruits. In particular, we'll view the snake as just its head, separate from the rest of the segments.
In addition to this datatype-definition,
give at least two example head objects/structs
(the design recipe's #2,
hint: Thoughts/advice on fields below.
Create (in both racket and Java),
a function glide-head which
takes in a head, and returns
a head one tick
of time later,
ignoring
any exterior factors like
encountering
segments or fruits or key-presses.
Do account for wrapping around the screen;
you can (should) have a couple of named-constants
for the screen's height and width (perhaps 800x600 or so).
The Java and racket versions should both work in the same way: returning a new object, rather than mutating any fields (Cf. how we did enbiggen in both racket and java).
Note: This method has nothing to do with a gui or drawing. Updating the head's fields is about the model, not the view. (Later we will write draw-head below, though in racket-only.)
You might want to review how moving at some angle corresponds to changing its x- and y-coordinates.
The following functions only need to be done in Racket (not Java).
Note: We will not be using struct-inheritance, so these functions might be very similar to each other.Note that all
notes:
- The background passed in might be any image. For example you can test with
, which is exported from videogame-helpers.rkt as house-with-flowers.
- The library function place-image can be helpful.
- If the head is entirely off the background, then it's reasonable to say it wouldn't be shown in the result (which is what place-image does, since it crops the result to the background).
notes:
- Playing the demo above, we see there are only two key-presses that do anything (perhaps four, if you have a way to speed-up or slow-down). key-presses that actually do anything .
- You can compare two key-event?s with string=?, but key=? (from (require 2htdp/universe)) is more appropriate. Equal credit will be awarded, either way.
- Note that a key-event does not move the player. Only clock-events (ticks) actually move the player; the key-events are instantaneous in the game-model.
We still don't have a running game yet. This will entail processing lists of segments and lists of fruits. We'll deal with lists on the next homework (as an application of union and structure-types working together, rather than any new concept or syntax!). We will also have functions for (say) drawing an entire list of segments or list of fruits, as well as an overarching
When declaring fields, give them good (and perhaps short) names. Do remember to indicate their units (if appropriate), along with any other non-obvious info.
Many fields have multiple possible representations. For example, for a head's direction you might use an angle what units? what does angle 0 mean?, or a pair of coordinates, or even those two coordinates further wrapped inside single field of type coordinate-point (if I defined my own new struct for that). These all have their strengths and weaknesses.
One thing guiding our choice might be the libraries that we want to interact with. In this case, we'll be using the 2htdp/image and 2htdp/universe, so I'll point out the following:
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |