Contents

  1. ./animate.py
  2. ./circleShiftAnim.py
  3. ./circleShift.py
  4. ./print_examples.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]
# Animate moving a circle to where the user clicked
# TODO: modify the code so that the user can click
# multiple times to move the circle.
# By CS111

from graphics import *
from time import sleep

# break up the movement into multiple steps
STEPS=100
# time in seconds to pause
SLEEP_TIME=.05

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 the current position of the circle
center_point = circle.getCenter()
current_x = center_point.getX()
current_y = center_point.getY()

# checking position ... commented out when saw working
# print(current_x, current_y)

# get where the user clicked the mouse
click_point = canvas.getMouse()

# find the change in the position (in both x and y)
# from the circle's current position to the mouse click
clicked_x = click_point.getX()
clicked_y = click_point.getY()

change_in_x = clicked_x-current_x
change_in_y = clicked_y-current_y

#### Animate moving the circle that distance ####
# Break the distance into smaller steps.
# Then, move that smaller step and sleep

step_x = change_in_x/STEPS
step_y = change_in_y/STEPS

# move the circle that amount, STEPS times
for i in range(STEPS):
    circle.move(step_x, step_y)
    sleep(SLEEP_TIME)

# 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
# TODO: modify the code so that the user can click
# multiple times to move the circle.
# 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)

# YOUR SOLUTION HERE ....

# get the current position of the circle
center_point = circle.getCenter()
current_x = center_point.getX()
current_y = center_point.getY()

# checking position ... commented out when saw working
# print(current_x, current_y)

# get where the user clicked the mouse
click_point = canvas.getMouse()

# find the change in the position (in both x and y)
# from the circle's current position to the mouse click
clicked_x = click_point.getX()
clicked_y = click_point.getY()

change_in_x = clicked_x-current_x
change_in_y = clicked_y-current_y

# move the circle that amount
circle.move(change_in_x, change_in_y)

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

./print_examples.py 4/4

[
top][prev][next]
# Examples calling the print function
# Sara Sprenkle for CSCI111

# By default end is "\n" --> called "the new line character"
# means, put the next displayed text on the next line.

print("This", end=' and ')
print("That")

print("I", "am", "Spartacus", sep='!')

# With a ! at the end
print("I", "am", "Spartacus","", sep='!')
print("I", "am", "Spartacus!", sep='!')
print("I", "am", "Spartacus", sep='!', end="!")
print()

print("3","2","1", sep=". ", end='...\n...')
print("Liftoff")



Generated by GNU Enscript 1.6.5.90.