Department of Mathematics, Computer Science, and
                Statistics
St.
                Lawrence University
Zonohedron
 

What is this object?

...

Links
Overview
Schedule
Grading Info
Class Notes

 

CS 140: Introduction to Computer Programming


HOMEWORK #9
due Friday, Nov 30 by 5:00pm

Guidelines for completing and submitting homework
Refer to the first two paragraphs on the Grading Info page for all the details.  Briefly, you may not collaborate with your classmates while writing homework programs.  However, you may certainly consult Spencer on Thursday evenings or Dr. V on Friday afternoons as you create your programs.  (Email your program to me first to check that I'm in the office.)  Other QRC mentors may also be able to help out, depending on their experience with Python.  Once you have completed your program, or made as much progress as possible, email your program as an attachment to svandervelde@stlawu.edu.

Assignment
To begin, create a new document in Komodo and include these comments at the top
# Your full name
# Homework #9, Nov 30, 2012
# Individuals providing assistance, if any

Then save your file using the format LastnameHW9.py.

This week you will write a program that will permit the user to move a circle around the screen by pressing letter keys, and also to make the circle smaller or larger by pressing the minus and plus keys.  Put the program together one step at a time, as outlined below.  Run your program after each step to make sure you are on the right track.
  1. First initialize a 400 by 400 pixel pygame window with a light blue background.  Initialize the variables xpos = 200, ypos = 200, radius = 20 and draw a solid yellow circle with this center and radius on the screen.  Initialize a Pygame clock as well.  (See our multibounce.py program for this code.)

  2. Now set  flag = True  and create a while loop in which the program will run.  Check for whether any keys are pressed, and if the ESCAPE key is pressed then end the while loop by setting  flag = False.  Close the pygame window at the end, outside the while loop.

  3. Within the while loop but before the key check routine include a command to set the frame rate to 20 frames a second.  This will have the effect of pausing for 1/20th of a second each time through the while loop.  Then erase the screen, draw the circle (using its current xpos, ypos, radius), and display the new screen.

  4. Next check whether the j key is pressed, and if so decrease the value of xpos by 10, which will have the effect of moving the circle to the left.  Include similar code blocks to look for key presses of k, i, m and to move the circle to the right, up, or down, respectively, by 10 pixels.

  5. Finally, check whether the key press is equal to K_EQUALS or K_MINUS and if so, either increase or decrease the radius of the circle by 5.  However, don't allow the value of radius to drop below 5.

  6. Now figure out whether the circle has gone off the screen in any direction, and if so cause the game to end.  (This will take a little thought; you will need to take into account the screen size as well as the values of xpos, ypos, radius .)

And you're done!