Contents

  1. rectangle.py
  2. rectangle_solution.py
  3. tictactoe_clone.py
  4. tictactoe.py
  5. tictactoe_solution.py

rectangle.py 1/5

[
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(150, 150)
rectangle = Rectangle(upperLeftPoint, lowerRightPoint)

# draw the rectangle, then wait for user to click the mouse
rectangle.draw(win)
win.getMouse()

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

win.getMouse()

# What are the x- and y- coordinates of the upper-left corner of the Rectangle now?

newUpperLeftPoint = rectangle.getP1()
upperLeftXCoord = newUpperLeftPoint.getX()
upperLeftYCoord = newUpperLeftPoint.getY()

print( upperLeftXCoord, upperLeftYCoord )

rectangle_solution.py 2/5

[
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(150, 150)
rectangle = Rectangle(upperLeftPoint, lowerRightPoint)
# Alternative, not recommended approach because too much in
# one line of code.
# rectangle = Rectangle( Point(50, 50), Point(150, 150) )

# 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 3/5

[
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 4/5

[
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)

# create the first vertical line
v1Point1 = Point(200/3,0)
v1Point2 = Point(200/3, 200)

vertLine1 = Line(v1Point1, v1Point2)

# set the properties of the first vertical line
vertLine1.setWidth(3)
vertLine1.setOutline("purple")

# draw it
vertLine1.draw(win)

# create the second vertical line
v2Point1 = Point(2*200/3,0)
v2Point2 = Point(2*200/3, 200)

vertLine2 = Line(v2Point1, v2Point2)

# set the properties of the first vertical line
#vertLine2.setWidth(3)
#vertLine2.setOutline("purple")

# draw it
vertLine2.draw(win)


win.getMouse()

tictactoe_solution.py 5/5

[
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.