Practice 1
Write a function compute_value(x)
, which uses a single if
statement to determine what value to return. This function should
return the value of x if 0<x<10. Otherwise, your program should return 0.
Practice 2
Write a function omit_values(x)
, which uses a single if
statement to determine what value to return. This function should
return the value of x as long as x is even
or x is negative. Otherwise, your program should return 0.
The bouncing image screen saver you created in the last lab only moved horizontally. A better animation is one that bounces all over the screen. Using logical operators, you can create this animation.
Details
Create a program that animates a bouncing image in a file called bouncing.py. The image should begin in the center of the window and move diagonally. When the image hits an edge of the window, it should bounce. The animation should continue until the end of the animation loop. Your program should only have two conditional statements.
Example
$ python3 bouncing.py
Hint
One of the first successful video games was Pong. Back in the 1970's a game of Pong was so complicated that devices were sold that played only Pong. Today, Pong is the "Hello World" version of a video game; Its typically the first one you write. Today you are going to create a 1-player version of Pong.
Details
Copy the program from the previous activity into a file called pong.py. Modify the program to add a user controlled image. The image should always be at or near the bottom of the window. The image should be able to move only left and right in response to keyboard or mouse input.
Modify the program so that the image bounces off of the player controlled image but does not bounce off of the bottom of the window.
Example
$ python3 pong.py
Hint
Challenge 1
The original Pong game was meant to be like ping-pong and you need two players to play ping-pong. Modify the program so that it is a two player game. Add a second user controlled image to the opposite side of the screen.
Challenge 2
Modify the program so that it keeps score. Every time the ball goes off the window, the player on the other side gets a point and the ball's position resets. Draw each player's score in the window.
Challenge 3
The game is kind of easy because the path of the ball is completely deterministic. Make the game more interesting by speeding up the ball every time it bounces and give players the ability to add spin to the ball. Players can spin the ball, or change its direction, by moving the paddle when it hits the ball. If the paddle is moving in the same direction as the ball, it will make the bounce angle more obtuse. If the paddle is moving in the opposite direction as the ball, it will make the bounce angle more acute. If the paddle is not moving, it should bounce like normal.
Submission
Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.