Contents
- circleShift.py
- tictactoe2.py
- tictactoe.py
- userDraw.py
circleShift.py 1/4
[top][prev][next]
# Move a circle to the position clicked by the user
# Repeat five times
# By CSCI 111
from graphics import *
# create our canvas
win = GraphWin("Circle Shift", 300, 300)
# draw the circle in the middle of the screen
centerPt = Point(win.getWidth()/2, win.getHeight()/2)
circle = Circle( centerPt, 20 )
circle.setFill("blue")
circle.setWidth(0) # no outline
circle.draw(win)
# give the user some instructions
msg = "Click where you want to move the circle."
text = Text( Point(150, 290), msg)
text.draw(win)
for i in xrange(5):
# Update the instructions
text.setText(msg + "\nOnly " + str( 5-i) + " more to go")
# The user tells us where to click
newCenter = win.getMouse()
# figure out how far the circle needs to move
dx = newCenter.getX() - centerPt.getX()
dy = newCenter.getY() - centerPt.getY()
# move the circle
circle.move( dx, dy )
# get the circle's new center point
centerPt = circle.getCenter()
text.setText("Click to exit")
win.getMouse()
tictactoe2.py 2/4
[top][prev][next]
# Draw a Tic-Tac-Toe Board
# By CSCI 111
from graphics import *
win = GraphWin("Tic-Tac-Toe Board")
WIDTH = win.getWidth()
HEIGHT = win.getHeight()
# Make 2 horizontal lines
leftPoint1 = Point( 0, HEIGHT/3.0 )
rightPoint1 = Point( WIDTH, HEIGHT/3.0 )
hline1 = Line( leftPoint1, rightPoint1 )
hline1.setWidth(3)
hline1.setOutline("purple")
# clone the horizontal line so we don't need to set the
# "other" attributes of the line, like color and width
hline2 = hline1.clone()
# need to shift the second line down, 1/3 of the way
hline2.move(0, HEIGHT/3.0)
# Make 2 vertical lines
topPoint1 = Point(WIDTH/3.0, 0)
bottomPoint1 = Point(WIDTH/3.0, HEIGHT)
vline1 = Line(topPoint1, bottomPoint1)
vline1.setWidth(3)
vline1.setOutline("purple")
# similarly, clone the vertical line ...
vline2 = vline1.clone()
vline2.move(WIDTH/3.0, 0)
# draw the lines
hline1.draw(win)
hline2.draw(win)
vline1.draw(win)
vline2.draw(win)
win.getMouse()
tictactoe.py 3/4
[top][prev][next]
# Draw a Tic-Tac-Toe Board
# By CSCI111
from graphics import *
win = GraphWin("Tic-Tac-Toe Board")
WIDTH = win.getWidth()
HEIGHT = win.getHeight()
# Make 2 horizontal lines
leftPoint1 = Point( 0, HEIGHT/3.0 )
rightPoint1 = Point( WIDTH, HEIGHT/3.0 )
hline1 = Line( leftPoint1, rightPoint1 )
hline1.setWidth(3)
hline1.setOutline("purple")
leftPoint2 = Point( 0, 2*HEIGHT/3.0 )
rightPoint2 = Point( WIDTH, 2*HEIGHT/3.0 )
hline2 = Line( leftPoint2, rightPoint2 )
hline2.setWidth(3)
hline2.setOutline("purple")
# Make 2 vertical lines
topPoint1 = Point(WIDTH/3.0, 0)
bottomPoint1 = Point(WIDTH/3.0, HEIGHT)
vline1 = Line(topPoint1, bottomPoint1)
vline1.setWidth(3)
vline1.setOutline("purple")
topPoint2 = Point(2*WIDTH/3.0, 0)
bottomPoint2 = Point(2*WIDTH/3.0, HEIGHT)
vline2 = Line(topPoint2, bottomPoint2)
vline2.setWidth(3)
vline2.setOutline("purple")
# draw the lines
hline1.draw(win)
hline2.draw(win)
vline1.draw(win)
vline2.draw(win)
# pause so we can see our drawing ...
win.getMouse()
userDraw.py 4/4
[top][prev][next]
# Draw a line where the user clicks
# by CSCI 111
from graphics import *
win = GraphWin("User Draw", 300, 300)
msg = "Click where you want the first point for your line."
msg2 = "Click where you want the second point for your line."
# put the text field near the bottom
text = Text( Point(150, 270), msg)
text.setTextColor("blue")
text.draw(win)
# get user's input for the first point
pt1 = win.getMouse()
# update the screen: draw the first point, change message
pt1.draw(win)
text.setText(msg2)
# get user's input for the second point
pt2 = win.getMouse()
# draw the line
line = Line(pt1, pt2)
line.setWidth(2)
line.setOutline("green")
line.draw(win)
text.setText("Click to exit")
win.getMouse()
Generated by GNU enscript 1.6.4.