Due Sunday 24-Sep, at 10:00pm
hw5
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.
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!
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.
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.drawGameOverMessage(app)
which takes the app object and draws the game over message. You should call this function in your code.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.(app.dotCx >= app.width - app.dotRadius)
app.stepsPerSecond = 20
makes the game fun to play.That's it. Have fun!