CPSC150A
Scientific Computing

Activity 20

Review 2

Screen Saver

DVD players often included a screen saver which is a logo that continually bounces around the screen. Today, using the graphics module and conditional statements, you will replicate a very simple form of this screen saver.

Details

Create a Python program that animates a ball bouncing around the screen. The ball should begin in the center of the screen and should move diagonally. Once the reaches and edge of the screen, the ball should "bounce".

Example

  • The program should have a loop where each iteration the image is drawn at a different location. Do not forget the graphics.clear and graphics.wait functions.
  • Create variables to represent the image's x and y coordinates. The variables should be defined before the animation loop.
  • In addition to position, you need to store a variable for the images's x and y velocity.
  • Use the accumulator pattern to update the x location of the image by the image's speed.
  • If the image 'hits' the edge of the graphics window, change the direction of the image by changining its speed.
  • Use a conditional statement comparing the image's x or y coordinate to the dimensions of the window to determine if the image has 'hit' the edge of the screen.

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, you know enough programming to create your own Pong game in an afternoon.

Details

Pong was designed to be like a game of table tennis (get it? Pong, like ping-pong). Create a Python program that uses the graphics module to play a one-player version of Pong, like a person playing ping-pong against a wall. For example:

The movement of the ball is just like the bouncing ball from the screen saver activity. Begin this program by copying that program and modifying it to include the paddle at the bottom of the screen. The paddle should follow the position of the mouse, but only move horizontally.

Once the program has a paddle, finish the program by replacing the code that bounces the ball off the bottom of the window to be code that bounces the ball only if it hits the paddle.

Hint

Paddle Movement
  • Use the mouse.x function to get the location of the mouse. Use the mouse's x coordinate as the x coordinate when drawing the paddle. Use a fixed value, near the bottom of the screen, for the paddle's y coordinate.
Paddle Bounce
  • Part of the power of writing code using functions is that it makes it easy to move it from one program to another. Testing if the ball hits the paddle is simple if you use one of the functions you have previously written. Either the rectangle_overlaps function or the is_point_in_rectangle function could be used to to test if the ball hits the paddle.
  • If the ball does hit the paddle its speed should change just like it hit the bottom of the screen.
  • Note: the ball will bounce inside of the paddle if the paddle hits the side of the ball. To prevent this, only consider the ball hitting the paddle if the ball is also moving downward.

Challenge 1

When the ball goes off the bottom of the screen, the game is over. Inform the user of this, by drawing the text "Game Over" on the screen.

Challenge 2

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 3

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 4

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.