Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Moving Rectangle Bounces Left-to-Right

def pong():
  width = 10
  rate = 10
  canvasWidth = 640
  canvasHeight = 480
  turnNumber = canvasWidth/rate
  for frameNumber in range(199):
    canvas = makeEmptyPicture(canvasWidth, canvasHeight)
    x = (frameNumber%turnNumber)*rate
    if isGoingRightAt(frameNumber, turnNumber):
      addRectFilled(canvas, x, getHeight(canvas)/2, width, width, orange)
    else:
      addRectFilled(canvas, canvasWidth-x, getHeight(canvas)/2, width, width, orange)
    writeFrame(canvas, frameNumber)


def isGoingRightAt(frame, turnAt):
  bounces = frame / turnAt # Integer division, so integer quotient
  return (bounces%2==1) # Returns whether bounces is an odd number

def writeFrame(pic, fn):
  numerals = str(fn)
  if fn < 100:
    numerals = "0" + numerals
  if fn < 10:
    numerals = "0" + numerals
  fileName = "pong" + numerals + ".jpg"
  writePictureTo(pic, getMediaPath(fileName))



Links to this Page