CPSC120A
Fundamentals of Computer Science I

Lab 15

Logical Operators

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 Return of the Bouncing Image

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

Look back at your bouncing ball program from the last lab. This program will have the same overall structure. Some things that are different:

  • The image should have two variables controlling its speed. One for the x speed and one for the y speed. These should be used in conjunction with an accumulator to change the position of the image every iteration of the loop.
  • Making the image bounce is a matter of changing the image's direction. The x speed and y speed variables control the image's direction.
  • Only one speed variable needs to change when the image hits an edge. For example when the ball hits the bottom edge:

    Only the y speed changes, and it changes sign, not magnitude.

  • When the image hits the top and the bottom of the window, only the y speed changes and it changes in the same way, by changing sign. If it is positive it becomes negative; if it is negative it becomes positive. The x speed changes in the same way, when it hits the right or left sides. This means that you only need two if statements to get the image to bounce, one for the top and bottom and one for the left and right. Use a logical operator to create each of these conditional statements.
Pong

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

Testing if the bottom of the ball hits the top of the paddle requires testing for three different conditions:

  1. The bottom of the ball must be below the top of the rectangle.
  2. The center of the ball must be to the right of the rectangle's left side.
  3. The center of the ball must be to the left of the rectangle's right side.

Use logical operators to test if all three of these conditions are true..

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.