Lab 3, CSC 101

This lab will introduce you to functions that use boolean logic and pygame.

Download lab3.zip, place it in your cpe101 directory, and unzip the file.

Short practice with a function with If Statements

This part of the lab is intended as a very short practice writing functions that use conditional (if) statements.

Create a directory named conditional and within that directory create a file named conditional.py and an associated file for testing named if_test.py.

min

Write a function, named min, that takes two arguments of type double and returns the smallest of the two values, using conditionals.

Now write at least two test cases for this function.

Introduction to pygame and using If Statements


Setup

This setup step only needs to be done once. If you have already done this in preparation for Assignment 2, then you can skip this step.

Using pygame with Python 3 on the department machines requires a step of setup. To simplify this, a script has been provied. Type the following at the command-line (on, e.g., unix11).

~akeen/public/pygame_setup

Now, the next time that you login you should be able to import the pygame module into Python 3. You can verify this by opening the python3 interpreter and typing import pygame. This should work based on the common setup of user accounts; I say should because this setup has changed over the years. If you have any issues, please let us know.

pygame Introduction

This part of the lab is intended to introduce you to pygame and to using conditionals to control your program.

For your first task, in the ball directory you will be editing the file named L3_base.py but please read the code provided to you first. Make sure you understand what a ball object contains and how you might use the values defined in colors.py. In order to make sure you understand what the fields are in a ball object, please first be sure to fill in the testing in the ball_tests.py to make sure that the ball created is what you expect.

Next, run the code using python L3_base.py to confirm that the code opens a window and draws a red ball that is not moving.

Task 1:
We will start by getting the ball moving. To do this, first fill-in the move function to increment the ball's y value by one. You will also need to add a call to the function move in the main loop (right before the draw call). Note that since this function is actually called repeatedly in a while loop, your ball will appear to move. Test your code by running it and observing that ball should now "drop".

Task 2:
Our next task will be to stop the ball from falling using a conditional statement. Fill in the function Falling which for the function parameter (ball) will returns True if the y value of the ball is less then 560 (the height of the "floor"), False otherwise.

Now in the main function, before you move the ball, be sure to test if the ball should still be falling, using the function you just wrote.
Test your code by running it to make sure the ball stops and remains visible "on the floor"

Task 3:
Now we would like the ball to "bounce" - so let's try an experiment.
After bouncing, the ball will move up. Before we make that happen, consider the behavior of a ball moving up. We know that when the ball reaches the "top" -- (for sanity, lets call the "top" of the bounce a height of 100 - so we don't have to wait for it to bounce to the very top of the screen) -- we want the ball to change direction. We want the y value to start getting smaller. So write a function called movedown to do just that.

Now lets experiment with filling in the testTop function similar to the Falling but with conditional to test if the ball's y has reached the top Be sure to add the function call to main in order to test this condition before moving, but have the ball movedown instead of move.

Given this code, can you explain the program's behavior to the instructor?

Or lets try something different.