CMU 15-112 Summer 2020: Fundamentals of Programming and Computer Science
Homework 14 (Due Wed 17-Jun, at 5:00pm)
- This assignment is SOLO. This means you may not look at other student's code or let other students look at your code for these problems. See the syllabus for details.
- To start:
- Create a folder named 'hw14'
- Edit hw14.py using VSCode
- When you are ready, download hw14-driver.py, move it to the folder where hw14.py and asteroids.py are located, and run it. If you have named all your files appropriately, it automatically generates a zip file, hw14.zip.
- Submit hw14.zip to Autolab. For this hw, you may submit as many times as you want, but only your last submission counts.
- This homework is all manually graded.
- This homework may be graded for style.
- TP Mentor Pref/Antipref Form [0pts]
As we mentioned in lecture, you can use this form to let us know who you'd like to have (or not have) as your term project mentor for whatever reason. Many students pref TAs they get along with particularly well and antipref TAs they have conflicts of interest with (friends, teammates, etc).
- Asteroids [100 pts]
Using our animation framework, write an animation with the following elements. A demo of the functionality is also included here:
- Starter Code:
- The hw14.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 hw13 into a file named "asteroids.py" and import asteroids to hw14.py
- 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. Asteroids are removed from the canvas after being frozen for 10 seconds.
- 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. Once a splitting aseroid has a radius smaller than 5 pixels, it is removed from the canvas.
- Smooth Play:
- Depending on where shrinking asteroids are created, they may bounce on the edge of the screen forever. Make sure they're created all the way in the screen where this doesn't happen.
- If you don't remove bullets after they leave the screen, your game may eventually become very slow
- Overall, make sure you have a smooth user experience.
- Hint: You may want to write an onTimerFired method within each asteroid class.
- Extra Hint: Be careful in the case that 2 bullets hit the same asteroid!