CPSC120A
Fundamentals of Computer Science I

Lab 12

Graphics

Use the command line to create a new directory called lab12 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Keyboard Control

Last lab we were able to use conditionals to create a simple text based game. Today we will use conditionals and the graphics module to create a simple graphical game. But first we are going to create an interactive animation.

Details

Download the bouncing_ball.py Python program that we created in class and the graphics.py module that it uses. Modify the program so that it also draws a keyboard controlled rectangle at the bottom of the screen. When the user holds down the left arrow key, the rectangle should move left. When the user holds down the right arrow key, the rectangle should move right. It should not be possible to move the rectangle any other direction. If the ball and the rectangle intersect, nothing should happen. You can either search for your own images onlin, or you can use the following images:

Test your program thoroughly and make sure your code follows the course's code conventions.

Example

$ python3 bouncing_ball.py

Hint

  • Create a variables to represent the rectangle's x and y coordinates. The variables should be defined before the animation for loop. Use the variables to draw the rectangle after the circle is drawn.
  • The graphics module has multiple functions for getting keyboard input from the user. The key_pressed function returns a bool depending on whether a key is currently being pressed.
  • The key_pressed function has one parameter, a string, that represents a keyboard key. The strings for the left and right arrow keys are 'Left' and 'Right'.
  • Move the rectangle by adding conditional statements, before the graphics.clear function, that change the rectangle's x coordinate variable if an arrow key is being pressed.

Challenge

The game isn't very fun if you can lose track of what you are controlling on the screen. Modify the program so that the rectangle can not move off 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, 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.

Details

Modify the bouncing_ball.py program from the last activity to make the ball interact with the rectangle. When the bottom of the ball hits the top of the rectangle the ball should bounce like it hit the bottom of the window. The ball should no longer bounce if it hits the bottom of the window.

Test your program thoroughly and make sure your code follows the course's code conventions.

Example

$ python3 bouncing_ball.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.

To test if all three of these conditions are true, the if statements must be nested.

Challenge

Modify the program so that it keeps score. Every time the ball hits the rectangle the score should go up by one. If the ball goes off the bottom of the screen the score should be set to 0 and the ball's location and velocity should be reset to their initial values. Use the graphics module to display the score in the window.

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.