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

pool table

# Simulate a pool ball bouncing off the cushions.

def pool():
  w = 640    # Width of table
  h = 480    # Height of table
  xRate = 20 # Speed ball moves from left to right (pixels per frame) 
  yRate = 24 # Speed ball moves from top to bottom (pixels per frame)
  size = 30  # Dimensions of square "ball"

  # Ball bounces when its trailing edge gets to edge
  right = w-size  
  bottom = h-size

  # You can change the ball's color from run to run, so that you can just view the
  # thumbnails of the frames as they are generated. Old thumbnails from previous
  # runs of the program will have different colored ball, so can be easily distinguished.
  color = red

  # Generate 99 frames with ball in slightly different position.
  for frameNumber in range(1,100):

    # Create a pool table canvas & let's color it green
    table = makeEmptyPicture(w, h)
    addRectFilled(table, 0, 0, w, h, green) 

    # x,y location is displaced depending on how far into the movie we are.
    # The part after the % was not shown in class and will be explained on Friday
    x = (frameNumber*xRate)%(2*right)
    y = (frameNumber*yRate)%(2*bottom)

    # Keep the ball on the table
    if x > right: # Too far right
      x = x - 2*(x - right)  # Reflect left ("bounce off the right cushion")
    if y > bottom: # Too far down
      y = y - 2*(y - bottom) # Reflect up ("bounce off the bottom cushion")
    if x < 0: # Too far left
      x = abs(x) # Reflect down ("bounce off top cushion")
    if y < 0: # Too far up
      y = abs(y) # Reflect up ("bounce off lower cushion")

    # Now draw the ball in the calculated position
    addRectFilled(table, x, y, size, size, color)

    # And write this frame of the movie
    frameString = str(frameNumber)
    if frameNumber < 10:
      frameString = "0" + frameString
    writePictureTo(table, getMediaPath("pool" + frameString + ".jpg"))


Link to this Page