Homework 5

Due Sunday 24-Sep, at 10:00pm


To start

  1. Create a folder named hw5
  2. Download hw5.py to that folder
  3. Inside that folder download the CMU Graphics.
  4. Edit hw5.py and modify the functions as required
  5. When you have completed and fully tested hw5, submit hw5.py to Gradescope. For this hw, you may submit up to 999 times, but only your last submission counts.

Some important notes

  1. This homework is solo. You may not collaborate or discuss it with anyone outside of the course, and your options for discussing with other students currently taking the course are limited. See the academic honesty policy for more details.
  2. While you may submit to Gradescope as often as you like for this assignment, there is no autograded portion, so you will be responsible for testing your code and making sure it meets the problem requirements.
  3. Remember the course’s academic integrity policy. Solving the homework yourself is your best preparation for exams and quizzes; cheating or short-cutting your learning process in order to improve your homework score will actually hurt your course grade long-term.

Limitations

Do not use sets, dictionaries, try/except, classes, or recursion this week. The autograder (or a manual CA review later) will reject your submission entirely if you do.

A Note About Style Grading

Like in the previous assignment, we will be grading your code based on whether it follows the 15-112 style guide. We may deduct up to 10 points from your overall grade for style errors. We highly recommend that you try to write clean code with good style all along, rather than fixing your style issues at the end. Good style helps you code faster and with fewer bugs. It is totally worth it. In any case, style grading already started, so please use good style from now on!

Problems

  1. Mentor Meeting [0 pts]
    You are required to meet with your mentor every week. Failing to meet with your mentor will result in -5 on the assignment. Remember, a mentor meeting is a quick, in-person checkin before the submission deadline of this assignment. The goal of the mentor meeting is to see how you are doing, get feedback, and provide advice and tips for the class.

  2. DotRace [50 pts] [manually graded]

    Write an app that starts by drawing a cyan dot of radius 20 at the middle of the left edge of the canvas (so cx is 20 and cy is 200).

    The app displays the 'Time Left', which starts at 100, and goes down by 1 on each step.

    The point of the game is to get the cyan dot all the way to the right edge before the timer reaches 0.

    The user moves the dot by alternating pressing 'm' and 'n'. The user can press either 'm' or 'n' to start the dot moving, but then the dot only moves when the other key is pressed (so, for example, pressing 'm' twice in a row will not move the dot twice). The dot moves by 10 pixels each time 'n' is pressed right after 'm', or 'm' is pressed right after 'n'.

    If the dot reaches the right edge (so cx is 380) before the timer reaches 0, the dot turns lightGreen, and the app displays 'You won!' at (200, 100) in size 16. At that time, the app also displays 'Press r to restart' at (200, 120), also in size 16. However, if the timer reaches 0 before the dot reaches the right edge, then the dot turns pink, and the app displays 'You lost!' at (200, 100) in size 16. At that time, the app also displays 'Press r to restart' at (200, 120), also in size 16.

    If the game is over, and the user presses 'r', then the dot moves back to the left edge and the timer resets to 100, and the game plays again.

    Here is a demo of the game:

    Here are some hints:
    • Instead of calling onAppStart to reset your app, we have included a reset function which should be called in both onAppStart and also when you want to reset your app. The starter code also includes following variables in the model (although you can and should add to this):
      • app.dotRadius to store the radius of the dot.
      • app.dotCx to store the x coordinate of the center of the dot.
      • app.ticksLeft to store the amount of time left. This should go down by one on each step.
    • The starter code also includes the function drawGameOverMessage(app) which takes the app object and draws the game over message. You should call this function in your code.
    • We found it useful to set app.prevKey to the previous key that was pressed. At first, this is set to None. This lets us check that the user has not pressed two consecutive m's or n's.
    • Do not move the dot if the user presses any keys except 'm' or 'n'.
    • To check if the dot reached or passed the right edge of the screen, test for something like this: (app.dotCx >= app.width - app.dotRadius)
    • We found that setting app.stepsPerSecond = 20 makes the game fun to play.

    That's it. Have fun!