Contents

  1. animate.py
  2. circleShiftAnim.py
  3. fenway.py
  4. tictactoe_clone.py
  5. tictactoe.py
  6. userDraw.py

animate.py 1/6

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

from graphics import *
from time import sleep

STEPS = 100

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 range(STEPS):
    circ.move(dx, 0)
    sleep(.1)

w.getMouse()
w.close()


circleShiftAnim.py 2/6

[
top][prev][next]
# Animate moving a circle to the position clicked by the user 5 times
# by CSCI 111

from graphics import *
from time import sleep

STEPS = 100
NUM_TIMES = 5

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

# construct and draw the circle
centerPoint = Point(25, 25)
circle = Circle(centerPoint, 25)
circle.draw(win)

# tell the user that they need to click somewhere
anchorPoint = Point(win.getWidth()/2, 10)
instruction = Text( anchorPoint, "Click where you want the circle to go")
instruction.draw(win)

# animate the circle moving to where the user clicked multiple times
for time in range(NUM_TIMES):
    # why do I need to get this information from the circle?
    centerPoint = circle.getCenter()
    
    # get where the user clicked
    destPoint = win.getMouse()

    # determine the total distance the circle must move
    changeInX = destPoint.getX() - centerPoint.getX()
    changeInY = destPoint.getY() - centerPoint.getY()
    
    # animate the movement, moving in small chunks 
    for step in range(STEPS):
        # alternate moving and sleeping
        circle.move(changeInX/STEPS, changeInY/STEPS)
        sleep(.01)


win.getMouse()
win.close()

fenway.py 3/6

[
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():
  print("How hard do you have to hit a baseball to hit it over")
  print("the Green Monster at Fenway Park?")
  print("Run this program to test your guesses.")
  print()
  mph = eval(input("Enter the speed off the bat in mph: "))
  angle = eval(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 range(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()

tictactoe_clone.py 4/6

[
top][prev][next]
# Create full-size tic-tac-toe board, using clone
# By CSCI111

from graphics import *

WINDOW_DIM=200
LINE_COLOR = "purple"
LINE_WIDTH = 3

window = GraphWin("Tic-Tac-Toe Board", WINDOW_DIM, WINDOW_DIM)

# draw first, purple horizontal line
hline1_pt1 = Point(0, WINDOW_DIM/3)
hline1_pt2 = Point(WINDOW_DIM, WINDOW_DIM/3)
hline1 = Line(hline1_pt1, hline1_pt2)
hline1.setFill(LINE_COLOR)
hline1.setWidth(LINE_WIDTH)
hline1.draw(window)

# draw second, purple horizontal line
hline2 = hline1.clone()
hline2.move(0, WINDOW_DIM/3)
hline2.draw(window)

# draw first, purple vertical line
vline1_pt1 = Point(WINDOW_DIM/3, 0)
vline1_pt2 = Point(WINDOW_DIM/3, WINDOW_DIM)
vline1 = Line(vline1_pt1, vline1_pt2)
vline1.setFill(LINE_COLOR)
vline1.setWidth(LINE_WIDTH)
vline1.draw(window)

# draw second, purple vertical line
vline2 = vline1.clone()
vline2.move(WINDOW_DIM/3, 0)
vline2.draw(window)

window.getMouse()
window.close()

tictactoe.py 5/6

[
top][prev][next]
# Create full-size tic-tac-toe board
# Lines are purple with line width 3
# By CSCI111

from graphics import *

WINDOW_DIM=200
LINE_COLOR = "purple"
LINE_WIDTH = 3

# create the window
window = GraphWin("Tic-Tac-Toe Board", WINDOW_DIM, WINDOW_DIM)

# draw first, purple horizontal line
hline1_pt1 = Point(0, WINDOW_DIM/3)
hline1_pt2 = Point(WINDOW_DIM, WINDOW_DIM/3)
hline1 = Line(hline1_pt1, hline1_pt2)
hline1.setFill(LINE_COLOR)
hline1.setWidth(LINE_WIDTH)
hline1.draw(window)

# draw second, purple horizontal line
hline2_pt1 = Point(0, 2 * WINDOW_DIM/3)
hline2_pt2 = Point(WINDOW_DIM, 2 * WINDOW_DIM/3)
hline2 = Line(hline2_pt1, hline2_pt2)
hline2.setFill(LINE_COLOR)
hline2.setWidth(LINE_WIDTH)
hline2.draw(window)

# draw first, purple vertical line
vline1_pt1 = Point(WINDOW_DIM/3, 0)
vline1_pt2 = Point(WINDOW_DIM/3, WINDOW_DIM)
vline1 = Line(vline1_pt1, vline1_pt2)
vline1.setFill(LINE_COLOR)
vline1.setWidth(LINE_WIDTH)
vline1.draw(window)

# draw second, purple vertical line
vline2_pt1 = Point(2*WINDOW_DIM/3, 0)
vline2_pt2 = Point(2*WINDOW_DIM/3, WINDOW_DIM)
vline2 = Line(vline2_pt1, vline2_pt2)
vline2.setFill(LINE_COLOR)
vline2.setWidth(LINE_WIDTH)
vline2.draw(window)

window.getMouse()

userDraw.py 6/6

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

from graphics import *

win = GraphWin("My Line", 600, 600)

anchorPoint = Point(300, 10)
instruction = Text( anchorPoint, "Click where you want the first point for your line.")
instruction.draw(win)

# get where the user clicked
point1 = win.getMouse()
point1.draw(win)

instruction.setText("Click where you want your second point to be.")

# get the second point where the user clicked
point2 = win.getMouse()
point2.draw(win)

# construct and draw the line
line = Line(point1, point2)
line.setWidth(3)
line.draw(win)

#update the instructions with the final instructions
instruction.setText("Click when you want to close.")

win.getMouse()
win.close()

Generated by GNU Enscript 1.6.6.