Contents
- animate2.py
- animate.py
- circleShift.py
- fenway.py
- userDraw.py
animate2.py 1/5
[top][prev][next]
# This program is an example of how to create animation in a graphics
# window. The user can indicate a new location for a circle. The
# overall change in x and change in y is calculated. Then the circle
# is moved in very small steps, with a slight pause in between, towards
# the final destination.
from graphics import *
from time import sleep
FRAMES = 300
w = GraphWin("Animate me!", 400, 400)
w.setBackground("orange")
current = Point(60,60)
circ = Circle(current, 50)
circ.setFill("yellow")
circ.setWidth(3)
circ.draw(w)
message = Text(Point(200,380), "Click mouse on new location for circle")
message.setSize(15)
message.draw(w)
for i in xrange(3):
moveTo = w.getMouse()
message.setText("Watch it move...")
currPos = circ.getCenter()
dx = float(moveTo.getX() - currPos.getX())
dy = float(moveTo.getY() - currPos.getY())
for step in xrange(FRAMES):
circ.move(dx/FRAMES, dy/FRAMES)
sleep(0.001)
message.setText("Click mouse on another new location")
message.setText("Click once more to quit")
w.getMouse()
animate.py 2/5
[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 xrange(STEPS):
circ.move(dx, 0)
sleep(.1)
w.getMouse()
circleShift.py 3/5
[top][prev][next]
# This program draws an initial circle in the graphics window.
# Each time the user clicks on a new spot, the circle is
# shifted to that location. This happens four times. On the fifth
# mouse click the program ends.
from graphics import *
win = GraphWin("Moving Circles")
circ = Circle(Point(50,50), 20)
circ.setOutline("magenta")
circ.setFill("magenta")
circ.draw(win)
label = Text(Point(100,180), "Click where the circle goes")
label.draw(win)
for i in xrange(5):
moveTo = win.getMouse()
current = circ.getCenter()
dx = moveTo.getX() - current.getX()
dy = moveTo.getY() - current.getY()
circ.move(dx,dy)
label.setText( str(5-i-1) + " clicks left")
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 a 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]
# Demonstrate getting user input with GraphWin.getMouse()
# CS111
from graphics import *
win = GraphWin("Drawing", 500, 500)
text = Text( Point(250,450), "Click where you want the first point.")
text.draw(win)
# get the user's mouse click
pt1 = win.getMouse()
pt1.draw(win)
text.setText("Click where you want the second point.")
# get the user's second mouse click
pt2 = win.getMouse()
pt2.draw(win)
# draw the line
text.setText("Here is your line!")
line = Line(pt1, pt2)
line.setOutline("purple")
line.setWidth(3)
line.draw(win)
# Pause so user can see the result
win.getMouse()
Generated by GNU enscript 1.6.4.