Week 4 - Lists, keyboard input, the basics of modeling motion discussion#

Goals#

  • Learn the basics of lists in Python

  • Model moving objects in Python

  • Recreate the classic arcade game “Pong”

Week 4a - Basics of Lists#

Lists#

  1. list is a sequence type. What could be a sequence type?

  1. Which functions do you know which work on lists?

  1. Which operation can we do on lists but cannot do on strings?

Keyboard Input#

  1. How many events happen if you type hello on your keyboard?

  1. Which handlers exist for keyboard events?

  1. What is chr() good for?

  1. The key code of most letters on our keyboard seem to correspond to the ASCII encoding. But how can we get the keycode for the arrow keys on our keyboard?

  1. You press a key and do not release it for ten seconds. You have a timer which runs every second and prints the status of your key. How many times is the handler for the keydown event is called?

Motion#

  1. Where will the point at (1, 2) will be after two seconds if this point moves with a velocity of (2, 3) and its position is updated ten times per second?

  1. What do we get if we subtract from one point another point?

  1. Scott shows two possible implementations for the motion:

    What is the difference between these implementations, both in the code and the behavior when you run them?

Collisions and Reflections#

  1. How would you calculate the distance between the points (1, 2) and (4, 6)?

  1. Which Python operators would you use to compute the previous result?

  1. Imagine you have a ball with a radius r. How would you check if the ball hit the left wall or not?

  1. What happens with the velocity of a ball if this vector is reflected by the left wall?

Week 4b - Keyboard Control#

Velocity Control#

  1. Joe shows an example in which he has to mash the the arrow keys to move the ball. What can we do to attain a continous motion without mashing a keyboard key?

  1. What is the problem with the control scheme implemented in this example where we change the velocity of the object everytime we press a key? How can we solve this problem?

Visualizing Lists and Mutation#

  1. Which operator can we use to test if two variables are pointing to the same thing?

  1. a = [0, 1] does b = list(a) create a new list?

  1. In the following example, how does the global list variable get modified even we do not declare it as global?

    a = [1, 2, 3]
    
    def modify()
    	   a[1] = 0
    

Programming Tips - 4#

  1. Which datatypes are mutable and which are not?

  1. What is the difference between tuples and lists?

  1. When would you use tuples instead of lists?

Mini-project #4 - Pong#

Code Clinic Tips#

If you do not understand Tip #4, look at this forum thread