Contents

  1. animate.py
  2. circleShiftAnim.py
  3. circleShift.py
  4. fenway.py
  5. userDraw.py

animate.py 1/5

[
top][prev][next]
# Simple demonstration of animation.
# by Sara Sprenkle

from graphics import *
from time import sleep

STEPS = 100

def main():
    w = GraphWin("Simple Animation", 400, 400)
    w.setBackground("orange")
    
    current = Point(60,60)
    circ = Circle(current, 50)
    circ.setFill("blue")
    circ.draw(w)

    end = w.getWidth()
    dx = (end - current.getX())/STEPS

    for step in xrange(STEPS):
        circ.move(dx, 0)
        sleep(.1)

    w.getMouse()

main()

circleShiftAnim.py 2/5

[
top][prev][next]
# Move a circle to the position clicked by the user 5 times
# by CSCI 111, 02.07.2011

from graphics import *
from time import sleep

SLEEPTIME=.01
STEPS=100

win = GraphWin("Circle Shift", 500, 500)

circ = Circle( Point(250, 250), 10 )
circ.setFill("red")
circ.draw(win)

directions = Text( Point(250, 20), "Click somewhere to move the circle to there.")
directions.draw(win)

for x in xrange(5):
    userPoint = win.getMouse()
    directions.setText("You have " + str(4-x) + " clicks left")
    circleCenterPoint = circ.getCenter()
    dx = userPoint.getX() - circleCenterPoint.getX()
    dy = userPoint.getY() - circleCenterPoint.getY()
    for y in xrange(STEPS):
        circ.move(dx/STEPS, dy/STEPS)
        sleep(SLEEPTIME)
    
win.getMouse()

circleShift.py 3/5

[
top][prev][next]
# Move a circle to the position clicked by the user 5 times
# by CSCI 111, 02.07.2011

from graphics import *

win = GraphWin("Circle Shift", 500, 500)

circ = Circle( Point(250, 250), 10 )
circ.setFill("red")
circ.draw(win)

directions = Text( Point(250, 20), "Click somewhere to move the circle to there.")
directions.draw(win)

for x in xrange(5):
    userPoint = win.getMouse()
    directions.setText("You have " + str(4-x) + " clicks left")
    circleCenterPoint = circ.getCenter()
    dx = userPoint.getX() - circleCenterPoint.getX()
    dy = userPoint.getY() - circleCenterPoint.getY()
    circ.move(dx, dy)
    
win.getMouse()

fenway.py 4/5

[
top][prev][next]
"""
 How hard do you have to hit a baseball to hit it over 
 the Green Monster at Fenway Park? Run this program to test 
 your guesses.

 Author: Andrew Danner, 09.20.2007
"""

from graphics import *
from time import sleep
from math import *

MPH2FPS = 5280.0/3600. #conversion from mph to fps
DEG2RAD = pi/180.0 #conversion from degrees to radians
GRAVITY=32  #gravity in ft/s^2
NUM_STEPS = 70

def main():

  mph = input("Enter the speed off the bat in mph: ")
  angle = input("Enter an angle in degrees: ")

  win=GraphWin("Fenway", 700, 600)
  win.setBackground("lightblue")
  win.setCoords(0, 0, 350, 300)

  greenMonster = Rectangle(Point(304,0), Point(310, 37))
  greenMonster.setFill("darkgreen")
  greenMonster.setOutline("darkgreen")
  greenMonster.draw(win)

  fallTime = timeToFall(angle, mph)
  vx = changeInXVelocity(angle, mph)
  vy = changeInYVelocity(angle, mph)

  for t in xrange(NUM_STEPS):
    tnow = t/fallTime
    #assume initial ball height of 4 feet
    y = 4+vy*tnow - 0.5*GRAVITY*tnow*tnow
    x = vx*tnow
    ball = Circle(Point(x,y),3)
    ball.setFill("white")
    ball.draw(win)
    sleep(fallTime/NUM_STEPS)
  
  win.getMouse()
  win.close()


# Compute the time for the ball to fall, plus a few seconds.
# Input: the angle (in degrees) and the speed the ball was hit at (in
# miles per hour)
# Returns the time in seconds for the ball to fall
def timeToFall(angle, mph):
  theta = angle * DEG2RAD
  v = mph * MPH2FPS
  vy = v*sin(theta)
  vx = v*cos(theta)
  tf = 2*vy/GRAVITY  #time it takes to fall back down, plus a few secs
  return tf

# Compute the change in vertical velocity
def changeInYVelocity(angle, mph):
  theta = angle * DEG2RAD
  v = mph * MPH2FPS
  return v*sin(theta)

# Compute the change in horizontal velocity
def changeInXVelocity(angle, mph):
  theta = angle * DEG2RAD
  v = mph * MPH2FPS
  return v*cos(theta)



main()

userDraw.py 5/5

[
top][prev][next]
# Draw a line where the user tells you with mouse clicks
# by CSCI 111

from graphics import *

print "Click two points to draw a line."

win = GraphWin("Draw a Line", 500, 500)
directions = Text( Point(250, 20), "Click your first point.")
directions.draw(win)

# Get two points
pt1 = win.getMouse()
pt1Circle = Circle(pt1, 3)
pt1Circle.draw(win)

directions.setText("Click your second point.")
pt2 = win.getMouse()

directions.undraw()

# Create the line and draw it
line = Line(pt1, pt2)
line.setWidth(3)
line.setOutline("blue")
line.draw(win)

win.getMouse()


Generated by GNU enscript 1.6.4.