Contents
- animate.py
- circleShiftAnim.py
- circleShift.py
- rectangle.py
- tictactoe_clone.py
- tictactoe.py
- userDraw.py
animate.py 1/7
[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/7
[top][prev][next]
# Animate moving a circle to the position clicked by the user 5 times
# by CSCI 111
from graphics import *
from time import sleep
STEPS = 100
NUM_TIMES = 5
win = GraphWin("My Circle", 500, 500)
centerPoint = Point(25, 25)
circle = Circle(centerPoint, 25)
circle.draw(win)
anchorPoint = Point(win.getWidth()/2, 10)
# tell the user that they need to click somewhere --
instruction = Text( anchorPoint, "Click where you want the circle to go")
instruction.draw(win)
for time in range(NUM_TIMES):
centerPoint = circle.getCenter()
# the user picks the new center
destPoint = win.getMouse()
# what is the total distance we need to go,
# in both the x and y direction?
changeInX = destPoint.getX() - centerPoint.getX()
changeInY = destPoint.getY() - centerPoint.getY()
# we'll animate to there.
for step in range(STEPS):
circle.move(changeInX/STEPS, changeInY/STEPS)
sleep(.01)
win.getMouse()
win.close()
circleShift.py 3/7
[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 x in range(5):
# get where the user clicks
newMidPoint=win.getMouse()
newMidPoint.draw(win)
dx = newMidPoint.getX() - midPoint.getX()
dy = newMidPoint.getY() - midPoint.getY()
circle.move(dx, dy)
newMidPoint.undraw()
# reset the midpoint to where we moved to:
midPoint = circle.getCenter()
instruction.setText("Click to close the window")
win.getMouse()
win.close()
rectangle.py 4/7
[top][prev][next]
# Draw a rectangle using the graphics API
# by CSCI111
from graphics import *
win = GraphWin("My Rectangle", 200, 200)
upperLeftPoint = Point(0, 100)
lowerRightPoint = Point(100, 0)
r = Rectangle(upperLeftPoint, lowerRightPoint)
# Alternative construction of the rectangle -- all in one line
# r = Rectangle(Point(0,100), Point(100,0))
r.draw(win)
win.getMouse()
# shift the instance of the Rectangle class 10 pixels to the right
r.move(10, 0)
# find out what the coordinates of the upper-left corner are now
newUpperLeftPoint = r.getP1()
print("the new upper left point is at (", newUpperLeftPoint.getX(), end="")
print(",", newUpperLeftPoint.getY(), ")")
# Alternatively
print(r.getP1().getX())
win.getMouse()
tictactoe_clone.py 5/7
[top][prev][next]
# Create full-size tic-tac-toe board, using clone.
# Compare to the solution for tictactoe.py
# Which is conceptually simpler to understand?
# Which is easier to change?
# By CSCI111
from graphics import *
WINDOW_DIM=200
tictactoe = GraphWin("Tic-Tac-Toe", WINDOW_DIM, WINDOW_DIM)
# create the points for the vertical line
vpoint1 = Point(WINDOW_DIM/3, 0)
vpoint2 = Point(WINDOW_DIM/3, WINDOW_DIM)
# make the line
vline1 = Line(vpoint1, vpoint2)
vline1.setOutline("purple")
vline1.setWidth(3)
vline1.draw(tictactoe)
# create the second vertical line
vline2 = vline1.clone()
vline2.move(WINDOW_DIM/3,0)
vline2.draw(tictactoe)
# create the first horizontal line
hpoint1 = Point(0, WINDOW_DIM/3)
hpoint2 = Point(WINDOW_DIM, WINDOW_DIM/3)
# create the vertical line
hline1 = Line(hpoint1, hpoint2)
hline1.setOutline("purple")
hline1.setWidth(3)
hline1.draw(tictactoe)
# create the second horizontal line
hline2 = hline1.clone()
hline2.move(0, WINDOW_DIM/3)
hline2.draw(tictactoe)
tictactoe.getMouse()
tictactoe.py 6/7
[top][prev][next]
# Create full-size tic-tac-toe board
# By CSCI111
from graphics import *
WINDOW_DIM=200
tictactoe = GraphWin("Tic-Tac-Toe", WINDOW_DIM, WINDOW_DIM)
# create the points for the vertical line
vpoint1 = Point(WINDOW_DIM/3, 0)
vpoint2 = Point(WINDOW_DIM/3, WINDOW_DIM)
# create the vertical line
vline1 = Line(vpoint1, vpoint2)
vline1.setOutline("purple")
vline1.setWidth(3)
vline1.draw(tictactoe)
# create the second vertical line
vpoint1 = Point(2* WINDOW_DIM/3, 0)
vpoint2 = Point(2*WINDOW_DIM/3, WINDOW_DIM)
vline2 = Line(vpoint1, vpoint2)
vline2.setOutline("purple")
vline2.setWidth(3)
vline2.draw(tictactoe)
# create the first horizontal line
hpoint1 = Point(0, WINDOW_DIM/3)
hpoint2 = Point(WINDOW_DIM, WINDOW_DIM/3)
# create the vertical line
hline1 = Line(hpoint1, hpoint2)
hline1.setOutline("purple")
hline1.setWidth(3)
hline1.draw(tictactoe)
# create the second horizontal line
hpoint1 = Point(0, 2*WINDOW_DIM/3)
hpoint2 = Point(WINDOW_DIM, 2*WINDOW_DIM/3)
# create the vertical line
hline2 = Line(hpoint1, hpoint2)
hline2.setOutline("purple")
hline2.setWidth(3)
hline2.draw(tictactoe)
tictactoe.getMouse()
userDraw.py 7/7
[top][prev][next]
# Draw a line where the user tells you with mouse clicks
# by CSCI 111
from graphics import *
win = GraphWin("My Line", 600, 600)
anchorPoint = Point(300, 10)
instruction = Text( anchorPoint, "Click where you want the first point for your line.")
instruction.draw(win)
point1 = win.getMouse()
point1.draw(win)
instruction.setText("Click where you want the second point for your line.")
point2 = win.getMouse()
point2.draw(win)
line1 = Line(point1, point2)
line1.draw(win)
#update the instructions with the final instructions
instruction.setText("Click when you want to close.")
win.getMouse()
win.close()
Generated by GNU Enscript 1.6.6.