Contents
- rectangle.py
- tictactoe_clone.py
- tictactoe.py
rectangle.py 1/3
[top][prev][next]
# Draw a rectangle using the graphics API
# by CSCI111
from graphics import *
win = GraphWin("Our Rectangle")
upperLeftPoint = Point(50, 50)
lowerRightPoint = Point(100, 100)
rectangle = Rectangle(upperLeftPoint, lowerRightPoint)
# Alternative, not recommended approach:
# rectangle = Rectangle( Point(50, 50), Point(100, 100) )
# draw the rectangle, then wait for user to click the mouse
rectangle.draw(win)
win.getMouse()
# move the rectangle to the right 10 px
rectangle.move(10,0)
# pause
win.getMouse()
# get and display the rectangle's new coordinates
newUpperLeftPoint = rectangle.getP1()
newLowerRightPoint = rectangle.getP2()
print("coordinate of upper left point:", newUpperLeftPoint)
print("coordinate of lower right point:", newLowerRightPoint)
# Move the rectangle such that the upper left point is at the
# lower right point.
# How far do I need to move?
dx = newLowerRightPoint.getX() - newUpperLeftPoint.getX()
dy = newLowerRightPoint.getY() - newUpperLeftPoint.getY()
# move it!
rectangle.move(dx, dy)
# pause, waiting for the user to click
win.getMouse()
tictactoe_clone.py 2/3
[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 *
# create the window
ticTacToeBoard = GraphWin("Tic-Tac-Toe Board", 200, 200)
# make vertical lines
vertPoint1 = Point(200/3, 0)
vertPoint2 = Point(200/3, 200)
vertLine1 = Line(vertPoint1, vertPoint2)
vertLine1.setWidth(3)
vertLine1.setOutline("purple")
vertLine1.draw(ticTacToeBoard)
vertLine2 = vertLine1.clone()
vertLine2.move(200/3, 0)
vertLine2.draw(ticTacToeBoard)
# make horizontal lines
horizPoint1 = Point(0, 200/3)
horizPoint2 = Point(200, 200/3)
horizLine1 = Line(horizPoint1, horizPoint2)
horizLine1.setWidth(3)
horizLine1.setOutline("purple")
horizLine1.draw(ticTacToeBoard)
horizLine2 = horizLine1.clone()
horizLine2.move(0, 200/3)
horizLine2.draw(ticTacToeBoard)
ticTacToeBoard.getMouse()
tictactoe.py 3/3
[top][prev][next]
# Create full-size tic-tac-toe board
# By CSCI111
from graphics import *
# create the window
win = GraphWin("Tic-Tac-Toe Board", 200, 200)
# make vertical lines
vertPoint1 = Point(200/3, 0)
vertPoint2 = Point(200/3, 200)
vertLine1 = Line(vertPoint1, vertPoint2)
vertLine1.setWidth(3)
vertLine1.setOutline("purple")
vertLine1.draw(win)
vertPoint1 = Point(200/3*2, 0)
vertPoint2 = Point(200/3*2, 200)
vertLine2 = Line(vertPoint1, vertPoint2)
vertLine2.setWidth(3)
vertLine2.setOutline("purple")
vertLine2.draw(win)
# make horizontal lines
horizPoint1 = Point(0, 200/3)
horizPoint2 = Point(200, 200/3)
horizLine1 = Line(horizPoint1, horizPoint2)
horizLine1.setWidth(3)
horizLine1.setOutline("purple")
horizLine1.draw(win)
horizPoint1 = Point(0, 2*200/3)
horizPoint2 = Point(200, 2*200/3)
horizLine2 = Line(horizPoint1, horizPoint2)
horizLine2.setWidth(3)
horizLine2.setOutline("purple")
horizLine2.draw(win)
win.getMouse()
Generated by GNU Enscript 1.6.6.