Homework 5 - due Wednesday, May 6, before class
1) Reversing a list
Write a function my_reverse that takes a list as parameter and reverses it. For example, the call myreverse([1,2,3]) should return [3,2,1]. (Do not use the built in method reverse.)
A palindrome is a word that is the same when read from left to right or from right to left.
Write a function is_palindrome which takes a list as parameter and checks whether this list is a palindrome.
For example, the function you write should behave like this:
>>> is_palindrome([1,2,3,2,1]) True >>> is_palindrome([1,2,3,1]) False
3) Steering a character
The goal for this problem is to make the character move using the keyboard. More specifically, the character should move up as long as the "w" key is pressed, down as long as the "s" key is pressed, right as long as the "d" key is pressed, and left as long as the "a" key is pressed.
Catching key presses. Now that that we have talked about for-loops, you can understand what is going on in this part of the code, which is part of the game loop.
for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = FalseThe function pygame.event.get() returns a list of all events that happened (since the last time we checked). The for-loops goes through that list and looks at each event individually. At the moment, we ignore all events except for events of type pygame.QUIT.
Step 1: Add code so that each time an event of type pygame.KEYDOWN happens, a message gets printed. And do the same for events of type pygame.KEYUP.
Try it out. Each time you press a key down, something should get printed and each time you release a key, something should get printed.
Step 2: Each event that is of either type pygame.KEYDOWN or pygame.KEYUP is associated with a name for the specific key that was pressed. Let's say that the variable event refers to an event of those types, then you can retrieve the name for the key using the following function
pygame.key.name(event.key)This returns a string. For example, if the key that was pressed or released was the "w" key, this function will return the string "w".
For both the KEYDOWN and the KEYUP events, add some code that checks which key was pressed/released and prints out a message indicating which key was pressed/released.
Step 3: If key "w" gets pressed we want to have the character moving up - that means, we want the y-speed of the character to become something negative. If the "w" key gets released again, we want the character to stop again - that means, the y-speed should become 0 again.
Add code that changes the y-speed as described in the previous paragraph, whenever the "w" key gets pressed/released.
Step 4: Add code to deal with the other three keys "a", "s", "d".
Step 5: (Optional) The code uses the move_and_bounce function we have seen before that reverses the direction of the speed when the character hits the edge of the screen. Change that function into a move_and_block function that simply blocks the character from moving any further when it tries to leave the screen.
Upload all of your python programs and answers to the questions to Blackboard.