12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

152 Chapter 15. Classesand objectsYoushouldseeagreenrectanglewithablackoutline. ThefirstlinecreatesaCanvas,whichappearsin the window as a white square. The Canvas object provides methods like rectangle for drawingvarious shapes.bboxisalistofliststhatrepresentsthe“boundingbox”oftherectangle. Thefirstpairofcoordinatesisthelower-left corner of therectangle; thesecond pair istheupper-right corner.You can draw a circlelikethis:canvas.circle([-25,0], 70, outline=None, fill='red')The first parameter is the coordinate pair for the center of the circle; the second parameter is theradius.If you add this line to the program, the result should resemble the national flag of Bangladesh (seewikipedia.org/wiki/Gallery_of_sovereign-state_flags).1. Write a function called draw_rectangle that takes a Canvas and a Rectangle as argumentsand draws arepresentation of theRectangle onthe Canvas.2. AddanattributenamedcolortoyourRectangleobjectsandmodifydraw_rectanglesothatit usesthe color attributeas the fillcolor.3. Write a function called draw_point that takes a Canvas and a Point as arguments and drawsa representation of thePoint on theCanvas.4. DefineanewclasscalledCirclewithappropriateattributesandinstantiateafewCircleobjects.Writeafunction calleddraw_circlethat draws circleson the canvas.5. Write a program that draws the national flag of the Czech Republic. Hint: you can draw apolygon likethis:points = [[-150,-100], [150, 100], [150, -100]]canvas.polygon(points, fill='blue')I have written a small program that lists the available colors; you can download it fromthinkpython.com/code/color_list.py.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!