Bill Manaris : Fall 2007 / CSCI 220 Homework 4

Assigned Date: Wednesday, Oct. 24, 2007
Due Dates: Wednesday, Nov. 7, 2007
Due Time: 11:55pm

Last modified on November 05, 2007, at 09:58 AM (see updates)

Learning Objective


You have been hired by a major computer-game software company to help write TargetShooter!, a video game for the iPhone platform.

You are one of several programmers working on this project. Your task is to write some of the functions used in this program.

It is essential that you follow precisely the specifications given, otherwise your functions will not work with the code written by other programmers.

Functions to Implement

Write the following functions:

def drawTarget(win, winSize, targetSize):
   """It draws a red circle and places it in random x, y coordinates within the window.   

      win        -- the window to draw the target
      winSize    -- the side of the window (assumed square)
      targetSize -- the radius of the circle     

      returns the target object
def getCoordinates(target):
    """Returns the x and y coordinates of the tagret's center."""
def isAtLeftWall(target, minX):
    """Returns true if we have hit or gone past the left wall; false otherwise."""
def isAtRightWall(target, maxX):
    """Returns true if we have hit or gone past the right wall; false otherwise."""
def isAtTopWall(target, minY):
    """Returns true if we have hit or gone past the top wall; false otherwise."""
def isAtBottomWall(target, maxY):
    """Returns true if we have hit or gone past the bottom wall; false otherwise."""
def isTargetHit(target, mouseX, mouseY):
   """It returns true if mouseX and mouseY coordinates are within the target; false otherwise."""
def reportScore(win, winSize, hits, shots):
   """It displays the score.

      win     -- the display where to show the score
      winSize -- the side of the display (assume a square display)
      hits    -- how many times the target was hit
      shots   -- how many shots were fired

You should fully document and test your code.

Functions Written by Other Programmers

Below are functions already written by other people on your team. Feel free to use the code below to test your functions.

However, do not change the provided code in any way. If you have any concerns about this code, contact your project manager (ahem, ... your instructor). He will advise you on how to proceed.

from graphics22 import *
from math import *
from random import *
from time import sleep

def targetShoot():
   """It displays a moving target on a square display and gives the user a number of shots.
      It keeps track of how many hits the user gets and displays them at the end of the game.

   winSize = 600    # side of the square display
   targetSize = 60  # radius of the moving target
   numTries = 5     # how many shots they get

   # create the display
   win = GraphWin("Target shoot!", winSize, winSize)

   # create the target and draw it on the display
   target = drawTarget(win, winSize, targetSize)

   # give them 'numTries' shots and get final score
   hits = playGame(win, winSize, target, numTries)

   # display the final score
   reportScore(win, winSize, hits, numTries)

   # Wait for another click to exit
   Text(Point(winSize/2, winSize-10), "Click anywhere to quit").draw(win)
def playGame(win, winSize, target, numTries):
   """It moves the target randomly and gives the user a number of chances to shoot at the target (click the mouse).

      win      -- where the target is displayed and the mouse is clicked
      winSize  -- the side of the window (assume a square window)
      target   -- the moving circle
      numTries -- how many chances the user gets to shoot at the target

      returns the final score

   shots = 0     # no shots fired yet
   score = 0     # no hits yet

   currentClickX = win.mouseX   # get initial mouse coordinates
   currentClickY = win.mouseY

   dx = randomValue(-10, 10)    # get random initial dx and dy
   dy = randomValue(-10, 10)

   # move the target and process shots (until they use up all their shots)
   while shots < numTries:

      # left wall or right wall?
      if isAtLeftWall(target, 0) or isAtRightWall(target, winSize):
         dx = -dx

      # top wall or bottom wall?
      if isAtTopWall(target, 0) or isAtBottomWall(target, winSize):
         dy = -dy

      # move the target randomly
      target.move(dx, dy)


      # has the mouse been clicked?
      if currentClickX != win.mouseX or currentClickY != win.mouseY:

         # they fired one more shot
         shots = shots + 1

         # remember the new mouse click coordinates
         currentClickX = win.mouseX
         currentClickY = win.mouseY

         # did they hit the target?
         if isTargetHit(target, currentClickX, currentClickY):

            # they hit the target, so update score
            score = score + 1

            # let them know that the target was hit by changing direction and speed
            dx = randomValue(-10, 10)
            dy = randomValue(-10, 10)

   return score
def randomValue(minValue, maxValue):
   """Returns a random value between minValue and minValue."""

   return random() * abs(maxValue - minValue) + minValue

Finally, at the bottom of the program include the following code. This code will facilitate unit testing of your functions.

# play the game
if __name__ == '__main__':


The submission instructions have changed:

WebCT should be used to submit your program,

Submit the entire program (i.e., include the functions given above), so that it can be graded easily.

It is fine to submit a partial solution for partial credit. However, you need to state in your top documentation what does not work yet.


The following policies are in effect for this assignment:


All identifiers should be meaningful.

Include your design (pseudocode) as comments in your program.

The following comments should appear in your program as the first lines in the file. Items in angle brackets are either to be removed or replaced with what is specified within the brackets:

# Name: <your name goes here first and last minimum>
# <ProgramName>.py
# Problem: <Brief, one or two sentence description of the
#           problem that this program solves, in your own
#           words.>
# Certification of Authenticity: 
#   <include one of the following>
#   I certify that this lab is entirely my own work.
#   I certify that this lab is my own work, but I
#   discussed it with: <Name(s)>
(Printable View of