CMU 15-112 Summer 2018: Fundamentals of Programming and Computer Science
Homework 13 (Due Wed 1-Aug, at 11:59pm)



  1. Feedback Form [5 pts]
  2. Fill out this form!

  3. Asteroids [95 pts]


    Using our animation framework and assuming that run() is already written, write init(data), mousePressed(event, data), keyPressed(event, data), and redrawAll(data) for an animation with the following elements:
    • Starter Code:
      • The hw15.py file starts with two classes written for you: Rocket and Bullet. You should not need to modify these classes, but you can change them if you like. An instance of rocket is also stored in data and drawn on the canvas.
      • Read over both classes. You'll need to call methods from each.
      • Copy and paste your three Asteroid classes from hw14 at the top of the file.
    • Rocket:
      • The rocket is already drawn for you, but it cannot move. When the user presses the right arrow key, the rotate should rotate clockwise. When the user presses the left arrow key, the rocket should rotate counter-clockwise. It can rotate however much you like.
      • When the user presses the space bar, the rocket should create a bullet.
      • Are there methods in the Rocket class that will help with this?
    • Bullets:
      • You should store and draw the bullets.
      • Every timerFired, the bullets move in the appropriate direction. Hint: There is a method written in the bullet class that handles this.
    • By this point, you should have a Rocket that can rotate and shoot.
    • Asteroids:
      • You should not be writing a lot of new code here. Instead, use the methods you wrote in hw14.
      • Every 2 seconds, an asteroid is created with a random cx and cy on the canvas, a random radius between 20 and 40 pixels, a random speed between 5 and 20 pixels per timerFired call, and a random direction in [(0, 1), (0, -1), (1, 0), (-1, 0)]. Hint: Use random.choice here.
      • The asteroid is chosen randomly from the three types: Asteroid, ShrinkingAsteroid, and Splitting Asteroid. Hint: Use random.choice here.
      • Asteroids are purple, ShrinkingAsteroids are pink, and SplittingAsteroids are blue.
      • You should be able to store and draw the asteroids. Hint: Where could you put a draw method?
      • Movement:
        • Asteroids wrap around when they get to an edge of the canvas.
        • Shrinking Asteroids bounce when they get to an edge of the canvas.
        • Splitting Asteroids wrap around when they get to an edge of the canvas.
      • Collisions (reactToBulletHit):
        • You do not need to handle asteroids colliding with each other. You do need to handle when they collide with bullets.
        • Hint: There is a method in the bullet class that will help with collison.
        • Asteroids stop moving when they collide with a bullet. Every 10 seconds, all frozen Asteroids are removed from the canvas.
        • Shrinking Asteroids shrink by shrinkAmount when they get hit by a bullet. You can change shrinkAmount from the default, but we won't test this. When a ShrinkingAsteroid's radius is 15 or less, it is removed from the canvas.
        • Splitting Asteroids split into two when they get hit by a bullet. They are never removed from the canvas.
      • Hint: You should write an onTimerFired method within each asteroid class, just like we did in lecture.
      • Extra Hint: Be careful in the case that 2 bullets hit the same asteroid!
      • Study the code from Dots Galore in class or the dots example in the notes for help!