Contents

  1. rectangle.py
  2. tictactoe_clone.py
  3. tictactoe_constant.py
  4. tictactoe.py

rectangle.py 1/4

[
top][prev][next]
# Draw a rectangle using the graphics API, move it, 
# and find out and display its coordinates
# by CSCI111

from graphics import *

win = GraphWin("Rectangle")

# Create an instance of a 50x100 Rectangle
upperLeftPt = Point(50, 50)
lowerRightPt = Point(100, 150)
rectangle = Rectangle(upperLeftPt, lowerRightPt)

# Draw the rectangle
rectangle.draw(win)

#pause so we can see the drawing
win.getMouse()

# Shift the instance of the Rectangle class to the right 10 pixels
rectangle.move(10, 0)

# pause so we can see the moved rectangle
win.getMouse()

# Display (print) the x- and y- coordinates of the upper-left corner of the Rectangle
# in a couple of different ways
newUpperLeftPt = rectangle.getP1()
print("The upperleft point:", newUpperLeftPt)

newXCoord = newUpperLeftPt.getX()
print("The upperleft point's x-coordinate:", newXCoord)
print("The upperleft point's y-coordinate:", newUpperLeftPt.getY())

tictactoe_clone.py 2/4

[
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()
ticTacToeBoard.close()

tictactoe_constant.py 3/4

[
top][prev][next]
# Create full-size tic-tac-toe board,
# using constants for the width and the height
# Compare to the solution for tictactoe_clone.py
# Which is easier to change?
# By CSCI111

from graphics import *

WINDOW_WIDTH=200
WINDOW_HEIGHT=200

# create the window
ticTacToeBoard = GraphWin("Tic-Tac-Toe Board", WINDOW_WIDTH, WINDOW_HEIGHT)

# make horizontal lines
horizPoint1 = Point(0, WINDOW_HEIGHT/3)
horizPoint2 = Point(WINDOW_HEIGHT, WINDOW_HEIGHT/3)
horizLine1 = Line(horizPoint1, horizPoint2)
horizLine1.setWidth(3)
horizLine1.setOutline("purple")
horizLine1.draw(ticTacToeBoard)

horizLine2 = horizLine1.clone()
horizLine2.move(0, WINDOW_HEIGHT/3)
horizLine2.draw(ticTacToeBoard)

# make vertical lines
vertPoint1 = Point(WINDOW_WIDTH/3, 0)
vertPoint2 = Point(WINDOW_WIDTH/3, WINDOW_WIDTH)
vertLine1 = Line(vertPoint1, vertPoint2)
vertLine1.setWidth(3)
vertLine1.setOutline("purple")
vertLine1.draw(ticTacToeBoard)

vertLine2 = vertLine1.clone()
vertLine2.move(WINDOW_WIDTH/3, 0)
vertLine2.draw(ticTacToeBoard)

ticTacToeBoard.getMouse()
ticTacToeBoard.close()

tictactoe.py 4/4

[
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()
win.close()

Generated by GNU Enscript 1.6.6.