Contents
- animate.py
- circleShiftAnim.py
- circleShift.py
- fenway.py
- tictactoe.py
- 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")
# creating a blue circle on the left side of the window
current = Point(60,60)
circ = Circle(current, 50)
circ.setFill("blue")
circ.draw(w)
# animate moving the circle to the right side of the window
end = w.getWidth()
dx = (end - current.getX())/STEPS
for step in range(STEPS):
circ.move(dx, 0)
sleep(.1)
w.getMouse()
circleShiftAnim.py 2/6
[top][prev][next]
# Animate moving a circle to the position clicked by the user 5 times
# by CSCI 111, 09.26.2012
from graphics import *
from time import sleep
CIRCLE_RADIUS = 50
STEPS = 50
win = GraphWin("Circle Shift", 500, 500)
# Give instructions to the user
anchorPoint = Point(win.getWidth()/2, 10)
instruction = Text(anchorPoint, "Click where you want your circle to go.")
instruction.draw(win)
# create the initial circle in the center of the window and draw it
midPoint = Point(win.getWidth()/2, win.getHeight()/2)
circle = Circle( midPoint, CIRCLE_RADIUS)
circle.draw(win)
for n in range(5):
# get the user input--the mouse click
point2 = win.getMouse()
# compute the change in X and the change in Y
changeInX = point2.getX() - circle.getCenter().getX()
changeInY = point2.getY() - circle.getCenter().getY()
# figure out the increments of change in x and y
incrementX = changeInX/STEPS
incrementY = changeInY/STEPS
# animate the moving of the circle, in STEPS increments
for s in range(STEPS):
circle.move(incrementX, incrementY)
sleep(.05)
# update the user's instructions
instruction.setText("Click to close")
win.getMouse()
win.close()
circleShift.py 3/6
[top][prev][next]
# Move a circle to the position clicked by the user 5 times
# by CSCI 111
from graphics import *
CIRCLE_RADIUS = 50
win = GraphWin("Circle Shift", 500, 500)
# Give instructions to the user
anchorPoint = Point(win.getWidth()/2, 10)
instruction = Text(anchorPoint, "Click where you want your circle to go.")
instruction.draw(win)
# create the initial circle in the center of the window and draw it
midPoint = Point(win.getWidth()/2, win.getHeight()/2)
circle = Circle( midPoint, CIRCLE_RADIUS)
circle.draw(win)
for n in range(5):
# get the user input--the mouse click
point2 = win.getMouse()
# move the circle to where the user clicked
changeInX = point2.getX() - circle.getCenter().getX()
changeInY = point2.getY() - circle.getCenter().getY()
circle.move(changeInX, changeInY)
# update the user's instructions
instruction.setText("Click to close")
win.getMouse()
win.close()
fenway.py 4/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.py 5/6
[top][prev][next]
# Create full-size tic-tac-toe board
# By CSCI111
#
# NOT YET COMPLETE
#
from graphics import *
WINDOW_DIM=200
tictactoeBoard = GraphWin("Tic Tac Toe Board", WINDOW_DIM, WINDOW_DIM)
width = tictactoeBoard.getWidth()
height = tictactoeBoard.getHeight()
# first vertical line
point = Point(WINDOW_DIM/3, 0)
point2 = Point( WINDOW_DIM/3, WINDOW_DIM )
vline = Line(point, point2)
vline.setOutline("purple")
vline.setWidth(3)
vline.draw(tictactoeBoard)
# second vertical line
point3 = Point(WINDOW_DIM*2/3, 0)
point4 = Point( WINDOW_DIM*2/3, WINDOW_DIM )
vline2 = Line(point3, point4)
vline2.setOutline("purple")
vline2.setWidth(3)
vline2.draw(tictactoeBoard)
# Added to work in IDLE
tictactoeBoard.getMouse()
tictactoeBoard.close()
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)
# getting the user's two clicks for where to put the line
point1 = win.getMouse()
point1.draw(win)
#update the instructions
instruction.setText("Click where you want the second point for your line.")
point2 = win.getMouse()
point2.draw(win)
# creating the line and drawing it
line = Line(point1, point2)
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.4.