Contents

  1. graphics_test.py
  2. rectangle.py
  3. tictactoe2.py
  4. tictactoe.py

graphics_test.py 1/4

[
top][prev][next]
from graphics import *

win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
win.getMouse()

rectangle.py 2/4

[
top][prev][next]
# Using the Graphics Library
# CS111

from graphics import *

win = GraphWin()

upperleft = Point(5, 10)
lowerright = Point(50, 60)

rect = Rectangle(upperleft, lowerright)

rect.draw(win)

win.getMouse()

rect.move(10,0)

upperleft = rect.getP1()

print "The upperleft corner of the rectangle is at (",
print upperleft.getX(), ",", upperleft.getY(), ")"

win.getMouse()

tictactoe2.py 3/4

[
top][prev][next]
# Demonstrate drawing a full-canvas tic-tac-toe board,
# using clone() and move() methods
# CS111

from graphics import *

LINE_COLOR="purple"
LINE_WIDTH=3
CANVAS_WIDTH=500
CANVAS_HEIGHT=500

win = GraphWin("TIC-TAC-TOE Board", CANVAS_WIDTH, CANVAS_HEIGHT)

# could use the constants here
# instead, demonstrating method calls
width = win.getWidth()
height = win.getHeight()

# calculate the coordinates for the lines
topPoint = Point(width/3, 0)
bottomPoint = Point(width/3, height)

leftPoint = Point(0, height/3)
rightPoint = Point(width, height/3)

# draw the vertical lines
vline = Line(topPoint, bottomPoint)
vline.setOutline(LINE_COLOR)
vline.setWidth(LINE_WIDTH)
vline.draw(win)

vline2 = vline.clone()
vline2.move(width/3,0)
vline2.draw(win)

# draw the horizontal lines
hline = Line(leftPoint, rightPoint)
hline.setOutline(LINE_COLOR)
hline.setWidth(LINE_WIDTH)
hline.draw(win)

hline2 = hline.clone()
hline2.move(0,height/3)
hline2.draw(win)

# Pause so user can see the result
win.getMouse()


   

tictactoe.py 4/4

[
top][prev][next]
# Demonstrate drawing a full-canvas tic-tac-toe board
# CS111

from graphics import *

LINE_COLOR="purple"
LINE_WIDTH=3
CANVAS_WIDTH=500
CANVAS_HEIGHT=500

win = GraphWin("TIC-TAC-TOE Board", CANVAS_WIDTH, CANVAS_HEIGHT)

# could use the constants here
# instead, demonstrating method calls
width = win.getWidth()
height = win.getHeight()

# calculate the coordinates for the lines
topPoint1 = Point(width/3, 0)
topPoint2 = Point(2*width/3, 0)
bottomPoint1 = Point(width/3, height)
bottomPoint2 = Point(2*width/3, height)

leftPoint1 = Point(0, height/3)
leftPoint2 = Point(0, 2*height/3)
rightPoint1 = Point(width, height/3)
rightPoint2 = Point(width, 2*height/3)

# draw the vertical lines
vline = Line(topPoint1, bottomPoint1)
vline.setOutline(LINE_COLOR)
vline.setWidth(LINE_WIDTH)
vline.draw(win)

vline2 = Line(topPoint2, bottomPoint2)
vline2.setOutline(LINE_COLOR)
vline2.setWidth(LINE_WIDTH)
vline2.draw(win)

# draw the horizontal lines
hline = Line(leftPoint1, rightPoint1)
hline.setOutline(LINE_COLOR)
hline.setWidth(LINE_WIDTH)
hline.draw(win)

hline2 = Line(leftPoint2, rightPoint2)
hline2.setOutline(LINE_COLOR)
hline2.setWidth(LINE_WIDTH)
hline2.draw(win)

# Pause so user can see the result
win.getMouse()


   

Generated by GNU enscript 1.6.4.