As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab15 $ cd lab15
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.
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.
Wednesday, we wrote code that would allow an image to bounce back and forth horizontally around the screen. This is the first step to your first real game: pong. However, in pong the ball doesn't just bounce horizontally. The ball has vertical motion as well. Restructure your code so that your ball moves in a diagonal pattern, and bounces off all four walls.
First, copy your bouncing_ball.py file from your previous lab directory:
$ cd ~/cs120/labs/lab15 $ cp ../lab14/bouncing_ball.py .
Alter your code in this program so that the ball moves horizontally and vertically at the same time. Make sure you add collisions for the bottom and top of the screen as well!
$ python3 bouncing_ball.py
You probably had two conditionals on Wednesday, one for each edge of the window. However, you were executing the same code in each condition. We can use the or operator in this case to merge the two conditions together.
You want to flip the sign of the x velocity if the x position of the "ball" is less than 0, or if it is greater than the window width. You can do the same for the y velocity.
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; It's typically the first one you write. Today you are going to create a 1-player version of Pong.
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.
Test your program thoroughly and make sure your code follows the course's code conventions.
$ python3 bouncing_ball.py
Testing if the bottom of the ball hits the top of the paddle requires testing for three different conditions:
To test if all three of these conditions are true, the if statements must be nested.
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.
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.
The game is kind of easy because the velocity 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 give the ball spin, 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.