Activity 3 – Graphics Programming II

Dean Zeller Tuesday, September 11th

CS10051 5 points

Spring, 2007

Objective The student will design a picture to implement, including position and color guides.

Materials Pencil, graph paper

Background: variables and modifiable graphics

The previous activity used hard-coded numbers for coordinates and colors. While simple, this method is tedious and difficult to modify. To create true works of art, programmers need to be able to easily change a picture without having to hand-calculate coordinates. By defining position and colors in variables, it becomes much easier to modify later.

Instructions

Follow the steps below to create several pictures of varying complexity.

  1. On graph paper, create a drawing that only uses ovals, rectangles, lines, and polygons.
  2. For each graphic object drawn, identify and label the guides and colors. You may align multiple objects to a single guide.
  3. Create the Python code to draw the picture, using the guides and colors specified.
  4. Choose starting values for all guides.

Grading

You will be graded on the following criteria:

Accuracy Correctly identifying the guides for a drawing

Extra Credit

·  Implement additional pictures

Example

The Great Ohio Octatoad (octatotatus toadniferous) is a rare but thriving species, inheriting abilities from amphibians and cephalopods. An octatoad has eight eyes and eight legs. The picture above-right is an actual photograph of the very first octatoad in its natural environment, as discovered by Jennifer Sader in an exploration of her back yard in 1997.

The diagram below represents the relevant guides for drawing the octatoad. Note that the legs 1 thru 4 align with the left of the corresponding eyes, and legs 5 thru 8 align with the right


###########################################################################

# #

# Graphics Programming Using Variables #

# by Dean Zeller #

# #

# The following is a program demonstrating the use of variables to #

# store guides and colors for the purposes of drawing graphics. #

# #

###########################################################################

from Tkinter import *

import random

c = Canvas(width=650, height=500, bg='white')

c.pack(expand=YES, fill=BOTH)

raw_input("Octatoad, by Dean Zeller")

# set body variables

bodytop = 100

bodybottom = 300

bodyleft = 100

bodyright = 600

bodycolor = "white"

bodywidth = 5

# set eye variables

# eyes are the same height and evenly spaced vertically

eyetop = 150

eyebottom = 200

eyecolor = "white"

eyewidth = 2

pupilradius = 5

pupilcolor = "black"

eyesize = 35 # vertical width of an eye

eyespace = 15 # spacing between eyes

eye1left = 150

eye1right = eye1left + eyesize

eye2left = eye1right + eyespace

eye2right = eye2left + eyesize

eye3left = eye2right + eyespace

eye3right = eye3left + eyesize

eye4left = eye3right + eyespace

eye4right = eye4left + eyesize

eye5left = eye4right + eyespace

eye5right = eye5left + eyesize

eye6left = eye5right + eyespace

eye6right = eye6left + eyesize

eye7left = eye6right + eyespace

eye7right = eye7left + eyesize

eye8left = eye7right + eyespace

eye8right = eye8left + eyesize

radius = 10 # radius of the iris

innerrad = 2 # radius of the pupil

# pupil location is determined randomly within the eye oval

pupil1x = random.randrange(eye1left+10, eye1right-10)

pupil1y = random.randrange(eyetop+10, eyebottom-10)

pupil1color = "light blue"

pupil2x = random.randrange(eye2left+10, eye2right-10)

pupil2y = random.randrange(eyetop+10, eyebottom-10)

pupil2color = "green"

pupil3x = random.randrange(eye3left+10, eye3right-10)

pupil3y = random.randrange(eyetop+10, eyebottom-10)

pupil3color = "orange1"

pupil4x = random.randrange(eye4left+10, eye4right-10)

pupil4y = random.randrange(eyetop+10, eyebottom-10)

pupil4color = "purple1"

pupil5x = random.randrange(eye5left+10, eye5right-10)

pupil5y = random.randrange(eyetop+10, eyebottom-10)

pupil5color = "orange3"

pupil6x = random.randrange(eye6left+10, eye6right-10)

pupil6y = random.randrange(eyetop+10, eyebottom-10)

pupil6color = "yellow2"

pupil7x = random.randrange(eye7left+10, eye7right-10)

pupil7y = random.randrange(eyetop+10, eyebottom-10)

pupil7color = "brown"

pupil8x = random.randrange(eye8left+10, eye8right-10)

pupil8y = random.randrange(eyetop+10, eyebottom-10)

pupil8color = "pink"

# leg variables

# legs are the same height, but covered by the body

legtop = 250

legbottom = 400

# legs 1 thru 4 are left-aligned with eyes 1 thru 4

leg1 = eye1left

leg2 = eye2left

leg3 = eye3left

leg4 = eye4left

# legs 5 thru 8 are right-aligned with eyes 5 thru 8

leg5 = eye5right

leg6 = eye6right

leg7 = eye7right

leg8 = eye8right

legwidth = 2

footsize = 25

# mouth variables

mouthtop = 220

mouthbottom = 280

mouthleft = eye2left

mouthright = eye7right

mouthcolor = "white"

mouthwidth = 2

#draw legs

c.create_line(leg1,legtop, leg1,legbottom, leg1+footsize,legbottom, width=legwidth)

c.create_line(leg2,legtop, leg2,legbottom, leg2+footsize,legbottom, width=legwidth)

c.create_line(leg3,legtop, leg3,legbottom, leg3+footsize,legbottom, width=legwidth)

c.create_line(leg4,legtop, leg4,legbottom, leg4+footsize,legbottom, width=legwidth)

c.create_line(leg5,legtop, leg5,legbottom, leg5-footsize,legbottom, width=legwidth)

c.create_line(leg6,legtop, leg6,legbottom, leg6-footsize,legbottom, width=legwidth)

c.create_line(leg7,legtop, leg7,legbottom, leg7-footsize,legbottom, width=legwidth)

c.create_line(leg8,legtop, leg8,legbottom, leg8-footsize,legbottom, width=legwidth)

#draw body and mouth

c.create_oval(bodyleft,bodytop, bodyright,bodybottom, fill=bodycolor, width=bodywidth)

c.create_oval(mouthleft,mouthtop, mouthright, mouthbottom, fill=mouthcolor, width=mouthwidth)

#draw eyes

c.create_oval(eye1left,eyetop, eye1right,eyebottom, width=eyewidth, fill=eyecolor)

c.create_oval(eye2left,eyetop, eye2right,eyebottom, width=eyewidth, fill=eyecolor)

c.create_oval(eye3left,eyetop, eye3right,eyebottom, width=eyewidth, fill=eyecolor)

c.create_oval(eye4left,eyetop, eye4right,eyebottom, width=eyewidth, fill=eyecolor)

c.create_oval(eye5left,eyetop, eye5right,eyebottom, width=eyewidth, fill=eyecolor)

c.create_oval(eye6left,eyetop, eye6right,eyebottom, width=eyewidth, fill=eyecolor)

c.create_oval(eye7left,eyetop, eye7right,eyebottom, width=eyewidth, fill=eyecolor)

c.create_oval(eye8left,eyetop, eye8right,eyebottom, width=eyewidth, fill=eyecolor)

c.create_oval(pupil1x-radius,pupil1y-radius, pupil1x+radius,pupil1y+radius, fill=pupil1color)

c.create_oval(pupil1x-innerrad,pupil1y-innerrad, pupil1x+innerrad,pupil1y+innerrad, fill=pupilcolor)

c.create_oval(pupil2x-radius,pupil2y-radius, pupil2x+radius,pupil2y+radius, fill=pupil2color)

c.create_oval(pupil2x-innerrad,pupil2y-innerrad, pupil2x+innerrad,pupil2y+innerrad, fill=pupilcolor)

c.create_oval(pupil3x-radius,pupil3y-radius, pupil3x+radius,pupil3y+radius, fill=pupil3color)

c.create_oval(pupil3x-innerrad,pupil3y-innerrad, pupil3x+innerrad,pupil3y+innerrad, fill=pupilcolor)

c.create_oval(pupil4x-radius,pupil4y-radius, pupil4x+radius,pupil4y+radius, fill=pupil4color)

c.create_oval(pupil4x-innerrad,pupil4y-innerrad, pupil4x+innerrad,pupil4y+innerrad, fill=pupilcolor)

c.create_oval(pupil5x-radius,pupil5y-radius, pupil5x+radius,pupil5y+radius, fill=pupil5color)

c.create_oval(pupil5x-innerrad,pupil5y-innerrad, pupil5x+innerrad,pupil5y+innerrad, fill=pupilcolor)

c.create_oval(pupil6x-radius,pupil6y-radius, pupil6x+radius,pupil6y+radius, fill=pupil6color)

c.create_oval(pupil6x-innerrad,pupil6y-innerrad, pupil6x+innerrad,pupil6y+innerrad, fill=pupilcolor)

c.create_oval(pupil7x-radius,pupil7y-radius, pupil7x+radius,pupil7y+radius, fill=pupil7color)

c.create_oval(pupil7x-innerrad,pupil7y-innerrad, pupil7x+innerrad,pupil7y+innerrad, fill=pupilcolor)

c.create_oval(pupil8x-radius,pupil8y-radius, pupil8x+radius,pupil8y+radius, fill=pupil8color)

c.create_oval(pupil8x-innerrad,pupil8y-innerrad, pupil8x+innerrad,pupil8y+innerrad, fill=pupilcolor)


Ideas to Get Started