Contents
- ./animate.py
- ./circleShiftAnim.py
- ./circleShift.py
- ./random_test.py
./animate.py 1/4
[top][prev][next]
# Simple demonstration of animation.
# by Sara Sprenkle
from graphics import *
from time import sleep
STEPS = 100
window = GraphWin("Simple Animation", 400, 400)
window.setBackground("orange")
current = Point(60,60)
circ = Circle(current, 50)
circ.setFill("blue")
circ.draw(window)
end = window.getWidth()
dx = (end - current.getX())/STEPS
for step in range(STEPS):
circ.move(dx, 0)
sleep(.1)
window.getMouse()
window.close()
./circleShiftAnim.py 2/4
[top][prev][next]
# Animates moving a circle to where the user clicked
# By CS111
from graphics import *
from time import sleep
DIMENSION = 500
canvas = GraphWin("Move the Circle", DIMENSION, DIMENSION)
# create the circle in the upper left
circle = Circle( Point(100,50), 50)
circle.setFill("fuchsia")
circle.draw(canvas)
# show instructions in the middle of the screen
instructions = Text( Point(DIMENSION/2, DIMENSION/2), "Click where you want to move the circle.")
instructions.draw(canvas)
# user clicks where the circle should move to
userClickedPoint = canvas.getMouse()
#center of circle
circleCenterPoint = circle.getCenter()
changeInX = userClickedPoint.getX() - circleCenterPoint.getX()
changeInY = userClickedPoint.getY() - circleCenterPoint.getY()
# adjust these two variables to adjust how fast the animation occurs
secondsToSleep = .01
STEPS=50
for step in range(STEPS):
circle.move(changeInX/STEPS, changeInY/STEPS)
sleep(secondsToSleep)
# wait for the mouse click to close the window
canvas.getMouse()
canvas.close()
./circleShift.py 3/4
[top][prev][next]
# Move a circle to where the user clicked
# By CS111
from graphics import *
DIMENSION = 500
canvas = GraphWin("Move the Circle", DIMENSION, DIMENSION)
# create the circle in the upper left
circle = Circle( Point(100,50), 50)
circle.setFill("fuchsia")
circle.draw(canvas)
# show instructions in the middle of the screen
instructions = Text( Point(DIMENSION/2, DIMENSION/2), "Click where you want to move the circle.")
instructions.draw(canvas)
# user clicks where the circle should move to
userClickedPoint = canvas.getMouse()
#center of circle
circleCenterPoint = circle.getCenter()
# determine how far the circle needs to move to get to where the user clicked
changeInX = userClickedPoint.getX() - circleCenterPoint.getX()
changeInY = userClickedPoint.getY() - circleCenterPoint.getY()
# move the circle to where the user clicked
circle.move(changeInX, changeInY)
# wait for the mouse click to close the window
canvas.getMouse()
canvas.close()
./random_test.py 4/4
[top][prev][next]
# Demonstrating random module
# by Sara Sprenkle
import random
NUM_RANDOM = 8
print("This program generates", NUM_RANDOM, "random numbers.")
# Demonstrates that it's a pseudo-random number generator
# If using the same seed, the program generates the same list of
# "random" numbers.
# The following function call sets the seed.
#random.seed(1)
for x in range(NUM_RANDOM):
#print(random.random())
print(random.randint(0, 10))
Generated by GNU Enscript 1.6.5.90.