Contents

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

circle_move.py 1/6

[
top][prev][next]
# Move a circle to where the user clicked
# By CS111

from graphics import *

canvas = GraphWin("Move the Circle", 500, 500)

# create the circle in the upper left
circle = Circle( Point(50,50), 50)
circle.setFill("fuchsia")
circle.draw(canvas)

# put the directions in the top center of the canvas
directions = Text( Point(250, 20), "Click where you want to move the circle to.")
directions.draw(canvas)

# get the info from the click
new_point = canvas.getMouse()
newX = new_point.getX()
newY = new_point.getY()

# need to move the circle to that point, but move requires a dx and dy...
dx = newX - circle.getCenter().getX()
dy = newY - circle.getCenter().getY()

circle.move(dx, dy)

# wait for the mouse click
canvas.getMouse()
canvas.close()

rectangle.py 2/6

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

from graphics import *

# create the canvas
win = GraphWin("My Rectangle")

# create the points for the rectangle,
# create and draw the rectangle
upper_left_point = Point(50, 50)
lower_right_point = Point(150, 100)
r = Rectangle( upper_left_point, lower_right_point)
r.draw(win)

# wait for the mouse click before moving
win.getMouse()

# move rectangle 10 pixels to the right
r.move(10, 0)

# find out the coordinates of the upper left point
newUpperLeftPoint = r.getP1()
print("The new x coordinate is", newUpperLeftPoint.getX())

# wait for the mouse click before closing
win.getMouse()
win.close()

rectangle_solution.py 3/6

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

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

[
top][prev][next]
# Create full-size tic-tac-toe board (in progress...)
# By CSCI111

from graphics import *

# create the window
win = GraphWin("Tic-Tac-Toe Board", 200, 200)

# draw the first horizontal line
horizLine1 = Line( Point(0, 200/3), Point(200, 200/3))
horizLine1.setOutline("purple")
horizLine1.setWidth(3)
horizLine1.draw(win)


# wait for the mouse click before closing
win.getMouse()
win.close()

tictactoe_solution.py 6/6

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