CS 110 �Graphics
Benjamin Dicken
Announcements
Graphics in Python
Homework: graphics.py
from graphics import graphics
For today, whiteboard
from graphics import graphics��def main():� # What shall we put here?� pass��main()
Making graphics
Left/Right is the X axis
Up/Down is the Y axis
A particular position on the canvas is specified by by an X position and a Y position (coordinates)
This particular program canvas is 600 pixels wide and 400 pixels tall
X=0, Y=0
X=300, Y=0
X=0, Y=200
X=???, Y=???
X=???, Y=???
X=???, Y=???
Activity
X=100, Y=100
X=???, Y=???
X=???, Y=???
X=100, Y=100
X=320, Y=200
X=???, Y=???
X=100, Y=100
X=320, Y=200
X=500, Y=370
Creating a canvas
Use the code below:
gui = graphics(width, height, 'title')
The gui variable is of type graphics.graphics (we can just refer to it as a graphics type). This is a graphics object.
We can call methods (functions) using this object.
Draw the canvas
from graphics import graphics��def main():� gui = graphics(700, 300, 'Three Squares')��main()
Activity
The canvas . . .
from graphics import graphics��def main():� gui = graphics(700, 300, 'Three Squares')��main()
Draw a rectangle
gui.rectangle(x, y, w, h, fill)
What will this display?
from graphics import graphics��def main():� gui = graphics(700, 300, 'Three Squares')� gui.rectangle( 25, 50, 200, 200, 'black')� gui.rectangle(250, 50, 200, 200, 'purple')� gui.rectangle(475, 50, 200, 200, 'orange')��main()
Recall: gui.rectangle(x, y, w, h, fill)
Activity
What will this display?
from graphics import graphics��def main():� gui = graphics(700, 300, 'Three Squares')� gui.rectangle( 25, 50, 200, 200, 'black')� gui.rectangle(250, 50, 200, 200, 'purple')� gui.rectangle(475, 50, 200, 200, 'orange')��main()
What will this display?
from graphics import graphics
def main():
gui = graphics(700, 300, 'Three Squares')
gui.rectangle( 25, 25, 500, 100, 'green')
gui.rectangle(150, 0, 50, 300, 'red')
gui.rectangle(500, 0, 200, 300, 'blue')
gui.rectangle(300, 100, 50, 50, 'orange')
main()
Recall: gui.rectangle(x, y, w, h, fill)
Activity
What will this display?
from graphics import graphics
def main():
gui = graphics(700, 300, 'Three Squares')
gui.rectangle( 25, 25, 500, 100, 'green')
gui.rectangle(150, 0, 50, 300, 'red')
gui.rectangle(500, 0, 200, 300, 'blue')
gui.rectangle(300, 100, 50, 50, 'orange')
main()
What will this display?
def main():� gui = graphics(700, 300, 'Lines')� i = 10� while i < 700:� gui.rectangle(i, 50, 20, 200, 'blue')� i += 70��main()
Recall: gui.rectangle(x, y, w, h, fill)
Activity
What will this display?
def main():� gui = graphics(700, 300, 'Lines')� i = 10� while i < 700:� gui.rectangle(i, 50, 20, 200, 'blue')� i += 70��main()
What else can be displayed?
gui.ellipse(x, y, w, h, fill)
What else can be displayed?
gui.line(x1, y1, x2, y2, fill, width)
What else can be displayed?
gui.triangle(x1, y1, x2, y2, x3, y3, fill)
What else can be displayed?
gui.rectangle(x, y, w, h, fill)
gui.ellipse(x, y, w, h, fill)
gui.line(x1, y1, x2, y2, fill, width)
gui.triangle(x1, y1, x2, y2, x3, y3, fill)
What will this display?
�gui = graphics(500, 500, 'Lines')�i = 100�while i < 450:� if i % 100 == 0:� gui.ellipse(i, i, 75, 75, 'blue')� else:� gui.line(i, 0, i, 500, 'green', 10)� i += 50
Recall:
gui.ellipse(x, y, w, h, fill)
gui.line(x1, y1, x2, y2, fill, width)
Activity
What will this display?
�gui = graphics(500, 500, 'Lines')�i = 100�while i < 450:� if i % 100 == 0:� gui.ellipse(i, i, 75, 75, 'blue')� else:� gui.line(i, 0, i, 500, 'green', 10)� i += 50
Make your program display a canvas like the one shown
Activity
Make your program display a canvas like the one shown
def main():� gui = graphics(700, 300, 'Graphics')� # What goes here?
Activity
Make your program display a canvas like the one shown
def main():
gui = graphics(700, 300, 'Graphics')
i = 0
while i < 700:
offset = i * 50
if i % 2 == 0:
gui.line(offset, 50, offset, 250, 'blue', 25)
else:
gui.line(offset, 50, offset, 250, 'red', 25)
i += 1
What should change to update the canvas?
def main():
gui = graphics(700, 300, 'Graphics')
i = 0
while i < 700:
offset = i * 50
if i % 2 == 0:
gui.line(offset, 50, offset, 250, 'blue', 25)
else:
gui.line(offset, 50, offset, 250, 'red', 25)
i += 1
Activity
def main():
gui = graphics(700, 300, 'Graphics')
i = -2
while i < 700:
offset = i * 50
if i % 2 == 0:
gui.line(offset, 50, offset+100, 250, 'blue', 25)
else:
gui.line(offset, 50, offset+100, 250, 'red', 25)
i += 1
A Face
from graphics import graphics��def main():� gui = graphics(500, 500, 'Graphics')� gui.ellipse(250, 250, 400, 400, 'dodger blue')� gui.ellipse(150, 200, 50, 50, 'green')� gui.ellipse(350, 200, 50, 50, 'green')� gui.rectangle(150, 325, 200, 50, 'pale green')�
�main()
Give the face a nose
from graphics import graphics
def main():� gui = graphics(500, 500, 'Graphics')� gui.ellipse(250, 250, 400, 400, 'dodger blue')� gui.ellipse(150, 200, 50, 50, 'green')� gui.ellipse(350, 200, 50, 50, 'green')� gui.rectangle(150, 325, 200, 50, 'pale green')� # What would go here?
main()
Activity
Draw This
def main():� gui = graphics(500, 500, 'Graphics')� gui.ellipse(250, 250, 400, 400, 'dodger blue')� gui.ellipse(150, 200, 50, 50, 'green')� gui.ellipse(350, 200, 50, 50, 'green')� gui.rectangle(150, 325, 200, 50, 'pale green')
gui.triangle(250, 175, 200, 300, 300, 300, 'wheat3')
Draw This
Activity
Draw This
def main():� gui = graphics(500, 500, 'Face')� gui.ellipse(250, 250, 400, 400, 'dodger blue')� gui.ellipse(150, 200, 50, 50, 'green')� gui.ellipse(350, 200, 50, 50, 'green')� gui.rectangle(150, 325, 200, 50, 'pale green')
gui.triangle(250, 175, 200, 300, 300, 300, 'wheat3')� # What would go here?
Activity
Draw This
def main():� gui = graphics(500, 500, 'Face')� gui.ellipse(250, 250, 400, 400, 'dodger blue')� gui.ellipse(150, 200, 50, 50, 'green')� gui.ellipse(350, 200, 50, 50, 'green')� gui.rectangle(150, 325, 200, 50, 'pale green')
gui.triangle(250, 175, 200, 300, 300, 300, 'wheat3')� i = 100� while i < 400:� gui.line(i, 25, i, 125, 'orange')� i += 10