Week 5, Wednesday: Graphics, Using Objects

review

graphics image

What is the algorithm for drawing this picture? How do we create the crescent moon using just Circle objects?

create graphics window
set background to black
for loop to create stars
  pick random x from 0->width
  pick random y from 0->height
  create Point(x,y)
  draw Point, color it white
create and draw a white circle
clone circle, draw it, move it a little, color it black

We did not talk about the clone() method last time. It is very useful for creating lots of similar objects, like eyes for a face. Most graphics objects (e.g., Points, Circles, Rectangles, Text) have a clone method that works like this:

p = Point(100,200)
radius = 50
c = Circle(p, radius)
c.setFill("red")
c2 = c.clone()

So c2 is an exact copy of c, but it is not drawn on the screen yet.

more practice with functions

Given the following main(), write the functions needed to draw the circles, find the biggest one, and color it red.

def main():
  width = 800
  height = 800
  gw = GraphWin("circle animation", width, height)
  gw.setBackground("white")

  circles = makeCircles(gw, 10)
  # bigRed(circles)

  gw.getKey()

Note: the bigRed() function is commented out, so we can just work on makeCircles() for now. This is a good technique to learn: write main(), but comment out any functions you have yet to write. Write and test each function, one at a time.

makeCircles()

This function should create and draw the given number of circles. Each circle should have a random size an location (in the graphics window). The function should create the circles and store them all in a list. At the end of the function, return the list back to main.

Here's how to use the list append() method, with pseudo code for the function:

circles = []      # start with empty list
for loop:
  pick random location (x,y), size (radius)
  create circle: c = Circle(...)
  draw it, change color, etc
  circles.append(c)
# return list of circles to main
return circles

bigRed()

This function gets the list of Circle objects from main, finds the biggest one, and changes its color to "red". To find the biggest circle we can use the Circle method getRadius(), and compare the radii of each circle. Here's some pseudo code for this function:

# set biggest (so far) to be first circle
biggest = circles[0]   
for loop over all circles in list
  get radius of current circle
  compare it to radius of biggest
  if current_circle_radius > biggest_radius:
     set biggest to current circle
# after for loop, biggest should point to largest circle in list
biggest.setFill("red")

Note: this function does not need to return anything. It just changes the color of one circle in the given list.

random colors

Almost any color can be made by mixing various amounts of red, green, and blue light. The color_rgb(r,g,b) function allows you to create colors this way, varying the amounts of each color from none (0) to full-on (255). Here is some code to create random colors:

r = randrange(0,256)
g = randrange(0,256)
b = randrange(0,256)
mycolor = color_rgb(r,g,b)
c = Circle(...)
c.setFill(mycolor)