12.2 Introduction

So far in this book we have relied on our trusty turtle for any graphics for our programs. However, most computer graphics programs do not use a turtle. Common drawing programs with which you may be familiar, including Microsoft’s Visio or The Omni Group’s OmniGraffle, allow users to select the shapes they want in their drawing from a palette of available shapes. For simple drawing programs, this palette may include lines, circles, squares, triangles, and other simple shapes. For more advanced drawing programs, such as architecture programs or computer-aided design (CAD) programs, the palette may include sophisticated shapes such as cabinets, bathroom fixtures, or even specific parts for a car. These kinds of drawing programs are typically called object-oriented drawing programs.

Object-oriented drawing programs are easy to use. If your drawing calls for a square, you drag a square from the palette of shapes and drop it into your drawing in the appropriate place. If your drawing calls for a circle, or any other shape, you drag the circle or other shape into position. Once a shape is placed in the drawing, you can change attributes of the shape, such as its size, color, outline, or position.

Our goal in this chapter is to design an object-oriented graphics module for Python that will allow you to create graphics drawings by creating simple graphical shapes and placing them on a drawing canvas. The shapes will be like the shapes for a drawing program such as the ones described previously, except that we will not write the code to let a user drag the objects from a palette.

To be clear about where we are going in this chapter, consider the simple Python function drawHouse, shown in LISTING 12.1. The code draws a blue-filled rectangle as the main part of a house, along with a brown door and a peaked roof. In addition, it draws a yellow sun. The picture drawn by the code in Listing 12.1 is shown in FIGURE 12.1.

Image

LISTING 12.1 Using geometric objects to draw a house

A Python Turtle graphics window shows a simple home drawing with sun representation at a certain distance.

FIGURE 12.1 Using object-oriented graphics to draw a simple scene.

© 2001–2019 Python Software Foundation. All Rights Reserved.

In the rest of this chapter, we will design and implement a set of classes that enable us to write the function in Listing 12.1. Along the way we will learn about a powerful programming concept called inheritance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.153.31