RU beehive logo ITEC dept promo banner
ITEC 380
2021spring
ibarland

structs
hw04: galaga, part 1

Due:

Sep.14 (Mon) at the start of class, on D2L.
Your submitted file should be named “hw04.rkt”, plus any .java files.

Standard instructions for all homeworks:


part (a)

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.

part (b)

(This is the completion of hw04a, from D2L.)
Consider the following information, which might be part of a sports-simulation:

A team is:

  1. (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.

  2. (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 team>? to implement this. Follow all the steps of the design recipe1. As always, be sure to think about enough test-cases to cover the interesting situations (half of the pts). (In this case, six or seven should suffice, if they each cover a different situation.)


part (c) details to be updated, but the gist is correct

Familiarize yourself with the arcade game Galaga, such as this example. We will write our own version, starting with this homework (structs) and finishing with next homework (lists). Our simplified version need only deal with: aliens parading back and forth, diving (in an straight-line, perhaps angled or perhaps just vertical) the player's ship ("ship"), and the missiles (both the player's and the alien's). It does not need to deal with: score, having more than one level, number-of-lives, nor the “entrance” phase of the aliens. Also, after an alien dives past the bottom of the screen, any of the following are totally fine: (a) keep diving forever (wrapping around), (b) warp back to somewhere in the fleet, (c) go into a gently travel back to fleet-position mode, or (d) disappear off the bottom. Note that the first three options are checked by move-alien's unit tests; the last option would be implemented as part of next week's assignment.

There are three types of things (objects) which our program will need to model: ships, missiles, and aliens. Code for each of these is similar in spirit to the Book functions (racket & java) we wrote in lecture). Here is our recording zoom-lect about figuring what fields we want.

Racket & Java

We start by representing the ship, and writing a method involving one.
  1. (3pts) In both Java and Racket, develop a data-definition for a ship. For each field, give a short (two-to-three word) comment describing what it stands for, and its units. Natural units for screen-based games are pixels, and ticks (where a tick is 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.

    1. Give a Java class to represent a ship (non-static fields and a constructor only; your constructor should take as many arguments as there are fields). Note that no images are involved.
    2. Give a corresponding Racket define-struct statement.
      1. What is the signature 2 of of the constructor which racket creates for you?
      2. What is the name and signature of one of the ship accessor functions which racket creates for you?
  2. (3pts) Make at least two different examples of the data; do this both in Java and in racket. (In Java, where all code must be inside a method, put these inside a static method void tests().)
  3. (3pts) Write the template for ship-processing functions, in both racket and Java.
    (In Java, you'll have to comment out the template, since the ... won't compile.)
  4. (3pts) Create (in both racket and Java), a function ship-handle-key which takes in a ship and a key-event, and returns a new ship having responded to the keypress.
    API: Represent a key-event as a String, e.g. "x" or "right". In racket, 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.
    hint: Playing the demo above, we see there are only three key-presses that actually do anything, and firing is not something that changes the ship, so there are only two key-events that will have an effect. …Of course, you must always return some ship, even if it's the same as the one passed in.
    The Java and racket versions should work in the same way: returning a new object, rather than mutating any fields (Cf. enbiggened books, in the lecture notes).
    hint: You'll want to handle stopping at the left/right edges (or perhaps, wrapping around). 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 values less than 100 aren't near the right-edge. Also, note that modulo can help handle wrapping (or, if not using integers, modulo/real as provided in student-extras.rkt).
    Create (in both racket and Java), a function move-ship which takes in a ship, and returns a new ship one "tick" of time later, ignoring any exterior factors like colliding with aliens or any keys getting pressed.

Racket-only

The following problems only need be done in racket.

  1. (5pts) In racket (only), write the function ship-handle-key, which takes a ship and a key-event? (e.g. "right" (which moves the ship to the right) or "y" (which does nothing)), and returns a new ship which has responded to that keypress.

    hint: Playing the demo above, we see there are only three key-presses that actually do anything.
    API: In racket, 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.

  2. Give a data-definitions for an alien, and a missile, each with at least two examples of the data. Include a template for alien and missile.
    Note: We will not use struct-inheritance3 This means some of our functions will essentially be repeating code, which is indeed annoying.
  3. Write move-missile and move-alien: each takes their input, and returns a new version of it which accounts for one tick of time (one frame) passing. Do not take into account collisions with other objects, but do take into account wrapping around (if allowed). similar to move-ship. In class Thu., we will mention some nuances of how an alien moves: in particular, the direction it moves in is part of the entire fleet's direction (unless it's diving). So I made move-alien take in a second input, for the fleet's direction.
  4. (5pts) Drawing functions:
    1. Write the function draw-alien : alien, image -> image, which takes a alien and a “background” image, and returns that background image with the alien drawn on top of it.
      hint: place-image is a handy function; it is similar to overlay/xy except that it crops the result to the background.
      hint: For test-cases, include drawing a alien 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 submit print this file, but do submit it on D2L so that I can run your program.
    2. Write draw-ship, similarly.
      Note: My own version of draw-alien took in a second parameter — the "fleet's" location. Your version does not need to do this.
      Note: If you want a diving-alien to be drawn differently — at an angle perhaps — you may, but that complication is certainly not required.
    3. Write draw-missile, similarly.

    I suggest using simple shapes (like mere isosceles-triangles or star-polygons) 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 planetcute . Please cite your source, of course!

  5. Finally, write alien-collide-missile?. You may download and (require "overlap.rkt") (but you shouldn't need to modify that file).
    Remember, don’t copy/paste a .rkt file; right-click to download, and then you can open it in DrRacket if you like.

All the above should have their tests, as well as signatures and (brief) purpose statements.


1 Note that the stub-function, step 5e, gets replaced with the real body (step 7), so it won’t be visible in what you submit. Also, you don’t need to include the names of constructors/selectors; that was part of quiz03 but not part of the design recipe.      
2 Recall from lecture's struct-intro-book.rkt, a function's signature is its name, along with the types of its parameters and its return type. For example, the signature of substring is substring : string, int, int → string and the signature of addition is + : number, number → number.      
3 If you really want to use struct-inheritance, check with the instructor first, and see student-extras.rkt. It requires bumping up to intermediate-student, which means you'll no longer get error messages for leaving off an open-paren in front of a function's name.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.