CMU 15-112: Fundamentals of Programming and Computer Science
Writing-Session6 Practice
- These exercises will help you prepare for writing-session6, which is on Thu 27-Feb, and which will contain a randomly-chosen subset of exercises from among these.
- Unlike the hw, you may work collaboratively on these practice exercises.
- That said, during the actual writing session on Friday you must work alone, and without any notes or access to the web or other resources.
- To start:
- Create a folder named 'writing_session6_practice'
- Download cmu_112_graphics.py to that folder
- Edit a new file, writing_session6_practice.py, in that folder.
- Do not use recursion.
- Do not hardcode the test cases in your solutions.
Note that Snake will not be on the writing session, but it is fair game for Tuesday's quiz, so you should definitely know it very well by then. The snake questions are included here to help prepare you for the quiz -- again, they will not be on the writing session.
- Examples from the notes
Be sure to download, run, fully understand, and be able to reproduce the examples in this unit's notes on Event-Based Animations, including:- all the ways we move dots (with key presses, mouse presses, and timer, and with bounds and wraparound)
- Adding and Deleting Shapes
- Grids (with modelToView and viewToModel)
- Bouncing Square
- Snake
- Short Answer Practice Problems
1. Fill in each blank with Model, View, or Controller: A) The ________________ responds to keyboard, mouse, timer and other events and updates the model. B) The ________________ draws the app using the values in the model. C) The ________________ contains all the data we need for the animation. D) Controllers can only update the ________________, they cannot update the ________________. E) The view can never call the controller functions, and the view also can never update the ________________. 2. Fill in the blanks with the appropriate values for event.key: User types: event.key value: The enter key (aka return) ________________ The space key (aka the space bar) ________________ The left arrow key ________________ The # sign (shift-3) ________________ 3. True or False: A) Your code never directly calls redrawAll (the view) or event handlers like keyPressed (the controllers) -- these are called for you by the animation framework. B) An MVC Violation occurs if you try to update the model while drawing. C) If the user presses the mouse, the animation framework will first call mousePressed (the controller), and when that call has completely finished, only then will the animation framework call redrawAll (the view). 4. Fill in the blank: A) If we set app.timerDelay to 50, then we will get roughly ________________ calls to timerFired per second. B) In our grid example, getCell is also known as ______________________, because it takes (x,y) values and converts them to row,col values. C) In our grid example, getCellBounds is also known as ______________________, because it takes row,col values and converts them to (x,y) values. D) In our grid example, if we set app.rows and app.cols both to 2 in appStarted, and then we clicked the mouse near the top-right corner of the canvas, then app.selection would be set to ______________________. 5. Very short answers: A) Consider this code excerpt from doStep in our bouncing square example: if app.squareLeft < 0: # if so, reverse! app.squareLeft = 0 app.dx = -app.dx Very briefly, why do we set app.squareLeft = 0? B) In our snake example, why do we use app.waitingForFirstKeyPress? C) In our snake example, in takeStep, we check if ((newRow, newCol) in app.snake)). Very briefly, what situation does this test for? D) In our snake example, placeFood includes a 'while' loop. Very briefly, what is the purpose of this loop? 6. In our snake example, mark each of the following with M, V, or C if it is from the Model, View, or Controller: A) app.foodPosition B) app.rows C) drawSnake() D) app.snake E) app.cols F) drawGameOver() G) app.margin H) drawBoard() I) app.waitingForFirstKeyPress J) timerFired() K) app.direction L) drawFood() M) redrawAll() N) keyPressed() O) takeStep() P) placeFood()