Homework 5

Due Tuesday 6-Oct, at 9:00pm


To start

  1. Create a folder named hw5
  2. Create hw5_dot_race.py (We are not providing starter files this week.)
  3. Edit hw5_dot_race.py and build the Dot Race game specified below.
  4. Create hw5_tetris.py (We are not providing starter files this week.)
  5. Edit hw5_tetris.py and build Tetris as described below.
  6. Solve the list problem on CS Academy.
  7. When you have completed and fully tested hw5, submit hw5_tetris.py hw5_dot_race.pyto the appropriate locations on Gradescope.
  8. If you did the Tetris bonus, then fill out the Tetris bonus form.

Some important notes

  1. This homework is for your individual learning, but you may collaborate with other members of the class. You are expected to write your own code and submit work that reflects your own understanding. See the academic honesty policy for more details.
  2. 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!

A Note About Testing

For animations, as stated in the style guide, you do not have to write test cases for interactive, random, graphics, data initialization or event functions. Instead, you should test by visually inspecting your code’s behavior as you complete steps of each problem, where reasonably possible. This will make debugging your code much easier.

Problems

  1. isBoxy [25 pts]
    Complete the problem isBoxy(board) from CS Academy.

    You must solve the problem directly on the website, doing all of your testing there. Do not write the solution in Thonny (or a different IDE) and copy/paste it into the website.


  2. DotRace [25 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' in seconds, which starts at 0 and goes up on each step. The time should be displayed with two digits after the decimal point (for example, 'Time: 3.45').

    The point of the game is to get the cyan dot all the way to the right edge as fast as you can.

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

    Once the dot reaches the right edge (so cx is 380), the game is over: the timer stops, the dot turns lightGreen, and the app displays 'You won with a time of X.XX!' at (200, 100) in size 16, where X.XX is the final time in seconds, shown with two digits after the decimal point. The app also displays 'Press r to restart' at (200, 120), also in size 16. When the game is over, the title, the instructions, and the running time are no longer displayed.

    When the user presses 'r', the dot moves back to the left edge and the timer resets to 0, and the game plays again.

    Here is a demo of the game:



    Here are some hints:

    • Instead of calling onAppStart to reset your app, write a reset function which should be called in both onAppStart and also when you want to reset your app.
    • 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 display a number with exactly two digits after the decimal point, you can use an f-string, like this: f'{x:.2f}'

    That's it. Have fun!


  3. Tetris [50 pts] [manually graded]
    Write Tetris according to the design given in this step-by-step tutorial. You may not use a different design, even if you think there's a better way to do it (there probably is, but you still have to do it this way). This may seem limiting, but sometimes you have to write code according to a specific algorithm, rather than writing code to solve a specific problem.

    To get full credit, you'll need to complete the basic implementation according to the design spec. If you are interested, you may add additional features and improvement for a few bonus points. What sort of features? Whatever you think improves the game! Bonus features are not required, but we do heartily encourage them (not for the few points you might earn, but rather for the joy of learning and creating).

    If you decide to add bonus features, then…

    • Have the code run normally, without any bonus features, so we can easily grade your submission against the spec.
    • But if the user presses 'b', then the game switches to bonus mode, and the bonus features are activated.
    • When the user presses 'b' to activate the bonus features, a description of all the bonus features is printed to the console. Be sure to print enough of an explanation that we can understand how to use and grade your bonus features.
    Have fun!