Contents

  1. ./animate.py
  2. ./circleShiftAnim.py
  3. ./circleShift.py
  4. ./pick4.py
  5. ./random_test.py

./animate.py 1/5

[
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/5

[
top][prev][next]
# Animate moving a circle to where the user clicked
# TODO: Allow the user to click and move the circle 5 times
# 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)

# get where the user clicked
newCenter = canvas.getMouse()
newCenter_x = newCenter.getX()
newCenter_y = newCenter.getY()

# find out how far to move the circle
currentCenter = circle.getCenter()
currentCenter_x = currentCenter.getX()
currentCenter_y = currentCenter.getY()

move_x = newCenter_x - currentCenter_x
move_y = newCenter_y - currentCenter_y

# Animate the circle to that location

## Break the distance into some number of steps
steps = 20
step_x = move_x/steps
step_y = move_y/steps

sleep_time = .05

for step in range(steps):
    # move the small chunk
    circle.move(step_x, step_y)
    # pause
    sleep(sleep_time)


# wait for the mouse click to close the window
canvas.getMouse()
canvas.close()

./circleShift.py 3/5

[
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)

# get where the user clicked
newCenter = canvas.getMouse()
newCenter_x = newCenter.getX()
newCenter_y = newCenter.getY()

# find out how far to move the circle
currentCenter = circle.getCenter()
currentCenter_x = currentCenter.getX()
currentCenter_y = currentCenter.getY()

move_x = newCenter_x - currentCenter_x
move_y = newCenter_y - currentCenter_y

# move the circle to that location
circle.move(move_x, move_y)

# wait for the mouse click to close the window
canvas.getMouse()
canvas.close()

./pick4.py 4/5

[
top][prev][next]
# Display the numbers that are selected by the magic 
# ping-pong ball machine for Pick4 VA Lottery, all on one line
# By CS111

# import the random module
from random import *

print("The Pick4 VA Lottery number is ", end="")

# Repeat generating the random number and displaying that number four times
# The displayed number should be all in one line.
for i in range(4):
    randnum = randint(0,9)
    print(randnum, end="")

print(".")

./random_test.py 5/5

[
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.