Contents
- animate.py
- circleShiftAnim.py
- circleShift.py
- module_example.py
- pick4.py
- random_test.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]
# Move a circle to the position clicked by the user 5 times
# by CSCI 111
from graphics import *
from time import sleep
CIRCLE_RADIUS = 50
STEPS = 20
win = GraphWin("Circle Shift", 500, 500)
# 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)
anchorPoint = Point(win.getWidth()/2, 10)
# Give instructions to the user
instruction = Text(anchorPoint, "Click where you want your circle to go.")
instruction.draw(win)
# repeat the whole process 5 times
for i in range(5):
newCenter = win.getMouse()
# how far the circle needs to travel
differenceInX = newCenter.getX() - circle.getCenter().getX()
differenceInY = newCenter.getY() - circle.getCenter().getY()
# break that distance across multiple STEPS
stepDifferenceInX = differenceInX/STEPS
stepDifferenceInY = differenceInY/STEPS
for step in range(STEPS):
sleep(.1)
circle.move( stepDifferenceInX, stepDifferenceInY )
instruction.setText("Click to close the window")
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)
# 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)
anchorPoint = Point(win.getWidth()/2, 10)
# Give instructions to the user
instruction = Text(anchorPoint, "Click where you want your circle to go.")
instruction.draw(win)
for i in range(5):
newCenter = win.getMouse()
differenceInX = newCenter.getX() - circle.getCenter().getX()
differenceInY = newCenter.getY() - circle.getCenter().getY()
circle.move( differenceInX, differenceInY )
instruction.setText("Click to close the window")
win.getMouse()
win.close()
module_example.py 4/6
[top][prev][next]
# Example of importing a module
# by Sara Sprenkle
# Alternative: could use import math
# Would then need to prepend all constants, functions with math.
from math import *
i = 1j
# The equation e^(i pi) + 1 = 0
# with import math statement
# shouldbezero = math.e ** (i * math.pi) + 1
shouldbezero = e ** (i * pi) + 1
print("e^(i pi) + 1 equals", shouldbezero)
# practice using functions from modules
print("100^(1/2) =", sqrt(100))
pick4.py 5/6
[top][prev][next]
# Display the numbers that are selected by the magic
# ping-pong ball machine for Pick4 VA Lottery, all on one line
from random import *
print("This program generate the winning VA Pick 4 number.")
for i in range(4):
rand=randint(0,9)
print(rand, end="")
print("")
random_test.py 6/6
[top][prev][next]
# Demonstrating random module
# by Sara Sprenkle
import random
NUM_RANDOM = 10
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.6.