RU beehive logo ITEC dept promo banner
ITEC 380
2020fall
ibarland

structs
hw04: asteroids

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 D2L. Feel free to make sure they run as actual code, since you'll need it for part (b) anyway.

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.)


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 ufos.

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

  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 move-ship which takes in a ship, and returns a ship one "tick" of time later, ignoring any exterior factors like colliding with asteroids or any keys getting pressed. 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 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 that modulo can help handle wrapping (or, if not using integers, modulo/real as provided in student-extras.rkt).

part (c)

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.

  1. Write accel-ship, which takes in a ship and either the string "up" or "down", and accelerates or decelerates (respectively) the ship in the direction it's facing.
  2. (5pts) In racket (only), write the function ship-handle-key, which takes a ship and a key-event? (e.g. "right" (which rotates the ship) 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.
    : 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.
    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.

  3. Give a data-definitions for an asteroid, and a bullet, each with at least two examples of the data. Include a template for asteroid and bullet.
    Note: We will not use struct-inheritance2 This means some of our functions will essentially be repeating code, which is indeed annoying.
  4. Write move-asteroid and move-bullet, each similar to move-ship.
  5. (5pts) Drawing functions:
    1. Write the function draw-asteroid : asteroid, image -> image, which takes a asteroid and a “background” image, and returns that background image with the asteroid 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 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.
    2. Write draw-ship, similarly.
    3. Write draw-bullet, 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!

  6. (3pts) (racket-only) Write a function draw-world which takes in a ship and one asteroid, and returns an image: that ship and asteroid placed onto a(n otherwise blank) board. Only one test-case is required. (In the future, this function will get upgraded to place all game-objects onto a background board.)
    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 and draw-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 calling draw-ship, for that 2nd argument pass it the image you just got from calling draw-asteroid! This exactly how we overlayed two different text-images onto a background meme, in a previous homework.

  7. Finally, write ship-collide-asteroid?. 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.

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 write draw-ship-onto-blackness : ship → image (just a quick wrapper around draw-ship of course), and then:
(require 2htdp/universe)

(big-bang some-initial-ship
    [on-key  ship-handle-key]
    [on-tick move-ship]
    [to-draw draw-world draw-ship-onto-blackness])
In the next homework, we'll make a single world struct to hold everything in the game, at which point we'll have an updated draw-world (and, move-world, world-handle-key) which will be passed to big-bang.

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