CMU 15-112 Fall 2018: Fundamentals of Programming and Computer Science
Homework 11 (Due Friday, November 9th, at 6pm).
- Note that the assignment this week is due on Friday instead of Sunday. It is shorter than usual to make up for the fact that you have less time.
- To start:
- Create a folder named 'week11'
- Download the starter file hw11.py into that folder.
- Edit hw11.py using Pyzo
- When you are ready, submit hw11.py to Autolab. For this hw, you may submit as many times as you want, but only your last submission counts.
- Attend a TP mini-lecture [20pts]
To help you prepare for the term project, we're holding a series of one-hour lectures conducted by the TAs on interesting CS topics and modules that you may want to use in your project. Attend one of these lectures to receive 20 points on hw11. The current schedule is listed below:
Day Time Room Topic Presenters Resources Mon 4:30pm WEH 5310 Computer Vision Fletcher, Kusha, Olly OpenCV Manual Mon 5:30pm WEH 4709 3D Graphics Chaya, Gabriela Panda 3D Manual Mon 6:30pm WEH 5302 Pygame Brent, Raunak Pygame Manual Tue 4:30pm GHC 4102 Hand Tracking with Leap Motion Jonathan Tue 5:30pm DH 2105 Multiplayer with Sockets Brandon Sockets Manual Tue 7:30pm WEH 5320 User Experience and HCI Sanjna, Madeline (S18 TA) Tue 8:30pm WEH 5310 Chatbots Jacob, Zuhayer Wed 5:30pm DH A302 Body Tracking with Kinect Fletcher Wed 6:30pm WEH 5302 User Interfaces Jenny Wed 8:30pm WEH 5320 Audio Ambika, Ike PyAudio Manual Thu 4:30pm WEH 4709 Creative Computing Jonathan Thu 5:30pm DH 2122 Graph Theory Ike, Katherine Thu 7:00pm WEH 5320 Machine Learning Kyle, Kyra Machine Learning Manual Thu 8:30pm WEH 5310 Game AI Eric, Kyle, Sarah Fri 6:30pm WEH 5320 Data Structures Christina, Eileen, Vishal --- --- --- Additional Manuals --- Python Imaging Library (PIL)
- OOPy Asteroids [80pts]
In this assignment, you will be completing the code for a game of OOPy Asteroids that has already been partially implemented. If you are not familiar with the game Asteroids, you may wish to try it here.
Here is a video demo of the results of completing each of the steps listed below:
Step One: read the code provided in the starter file to understand the initial setup of the game. It is very important that you understand the provided code, as you'll be using it during the next few steps! This starter file includes five classes and the central game code.
- The first three classes represent the enemies in the game, the Asteroids. Asteroids are circles that can move around the screen based on a pre-determined direction. There is a base Asteroid class and two subclasses, ShrinkingAsteroid and SplittingAsteroid. You'll add a bit of code to these classes.
- The fourth class is the Rocket class, which represents the main player. The rocket stays in the middle of the screen, but can be rotated using the left and right arrow keys to point in different directions. The rocket can also fire bullets.
- The fifth class is the Bullet class. Bullets are generated by the rocket, and move in a straight line from their starting location.
- The central game code starts by drawing and allowing interaction with the main rocket object, and by creating and showing a score. You'll add a lot of code here!
Step Two: modify the game code to keep track of bullets fired by the rocket. A bullet is fired every time the user presses the space key. All bullets on the screen move every time timerFired is called, and bullets are removed from the game once they move offscreen. Make sure to read the Bullet class first- much of the work is already done in the class!
Step Three: modify the game code to keep track of asteroids. An asteroid with a random radius, speed, position, direction, and type (Asteroid, Shrinking Asteroid, or Splitting Asteroid) is added to the game every 2 seconds. You'll want to use random.randint() to determine most of these values, though random.choice() will be more useful for determining the direction and asteroid type. For now, asteroids stay on the screen permanently. Again, make sure to read the Asteroid class- much of the work has already been done in there!
Step Four: add the method reactToWallHit(self, screenWidth, screenHeight) to the Asteroid class(es). This method should be called after moving the asteroid to determine whether it has hit a wall and react appropriately. Normal asteroids and splitting asteroids wrap around to the other side of the screen when they hit a wall. Shrinking Asteroids bounce when they hit the wall. Note that screenWidth and screenHeight should be data.width and data.height.
Step Five: add the method reactToBulletHit(self) to the Asteroid class(es). Asteroids react to being hit by a bullet in the following ways:
- Normal Asteroids: when they are hit by a bullet, they are stunned and stop moving permanently. Every ten seconds, the game removes all asteroids that are currently stunned.
- Shrinking Asteroids: when they are hit by a bullet, their radius shrinks by the specified shrinkAmount. When their radius becomes 15 or smaller, they are removed from the game.
- Splitting Asteroids: when they are hit by a bullet, they split into two asteroids, each half the radius of the original asteroid (integer-divided). The first asteroid should be centered in the upper-left corner of the original asteroid's bounding box; the second asteroid should be centered in the lower-right corner. The two new asteroids should be added to the game, while the original asteroid should be removed. Asteroids should not be added when their radius drops to 0.
Finally, add code to timerFired that checks whether any of the bullets collide with any of the asteroids after movement has been completed. If a bullet hits an asteroid, the asteroid reacts as described above, then the bullet is removed from the game. Note that whenever an asteroid is removed from the game, you should add 1 point to the score. Points for normal asteroids should only be added when the asteroid is actually removed (not when it is stunned), and points should be added for each Splitting Asteroid that gets split.
You now have a working Simple Asteroids game! Woohoo!
Note: there is no bonus problem for this week's homework. If you'd like to challenge yourself, try applying the large project design concepts we discussed this week in class to your own term project.