Fall 2018

# Thu Sep 13

Questions about anything so far?

I've looked over the homework submitted - looks good.

Homework for Tuesday is posted.

# Concepts from chap 4 - objects & graphics

• class : Point
• instance : center = Point(50, 50)
• data : center.x
• method (i.e. function attached to object) : center.move(3, -2)
• inheritance (Circle "inherits from" GraphicsObject) ... more on this later.

## two names can refer to the same object

## Incorrect way to create two circles.
leftEye = Circle (Point (80 , 50) , 5)
leftEye.setFill('yellow')
leftEye.setOutline('red')
rightEye = leftEye          # OOPS
rightEye.move (20, 0)


## copy'n'paste

## works but has a "code smell" (google it) : "duplicated code"
leftEye = Circle (Point (80 , 50) , 5)
leftEye.setFill('yellow')
leftEye.setOutline('red')

rightEye = Circle (Point (100 , 50) , 5)
rightEye.setFill('yellow')
rightEye.setOutline('red')


## clone

# A better way.
leftEye = Circle (Point (80 , 50) , 5)
leftEye.setFill('yellow')
leftEye.setOutline('red')

rightEye = leftEye.clone()  # make a copy with same position, color, etc.
rightEye.move(20, 0)


## future value graph

Show code & discuss the several versions in the chapter.

#
# color grid | Jim M | python class demo | Sep 2018
#

from graphics import *

color_min = 0
color_max = 255
pixels_per_color = 3

window_size = (color_max - color_min + 1) * pixels_per_color
window_title = "colors!"
window = GraphWin(window_title, window_size, window_size)

blue = 100

for red in range(color_min, color_max+1):
for green in range(color_min, color_max+1):

left = red * pixels_per_color
right = left + pixels_per_color
top = green * pixels_per_color
bottom = top + pixels_per_color

color = color_rgb(red, green, blue)
box = Rectangle(Point(left, top), Point(right, bottom))
# box.setOutline(color) # if not set, outline will be black
box.setFill(color)
box.draw(window)

done = input("Done?")


## practice

Other things to try :

• drawing text
• setting a coordinate system
• reading text that the user inputs
• getting a mouse click

... ideas ?

https://cs.marlboro.college /cours /fall2018 /python /notes /chap4b
last modified Tue December 5 2023 9:30 am