First let's go over the homework.
Questions?
My solutions are in code/homework/sep11.
So far we know about
This is usually a fun week ...
Object oriented programming is a popular paradigm these days. It's not the only one, but it's an important one.
An "object" is a abstration chunk of data (a "thing") with actions (called "methods") it can perform.
A "class" is a kind of object. Example: Point, Circle, GraphWin
An "instance" is a specific object: Example: that red circle right there.
In python, creating an instance is done this way :
p = Point(10, 20) # instance_variable = ClassName(argument1, arg2)
And once we have one, we can tell it do things like this:
p.move(2, 3) # change position (x,y)=(10,20) to (12,23)
An "API" (Application Programmer's Interface) is a description of which sorts of objects you can create, and what methods they have.
All this is pretty abstract, so let's see a concrete example with the attached files.
See the resources page or http://mcsp.wartburg.edu/zelle/python/ Zelle's book pagefor both (1) the file you'll need to run this stuff (put in in the same directory), and (2) the API of what you can do with it.
It's possible that your python installation doesn't yet have the right system stuff - namely tkinter - to run Zelle's graphics library. So if this doesn't work for you we'll need to play around a bit.
You'll need the graphics.py module (i.e. file) from Zelle. And it needs to be somewhere where your python can find it, typically in the same folder as the python file you're running.
If you're using IDLE, then you need to get it to the same folder as the graphics.py file. One way to do that is store a .py file of your own in that folder, then open that file.
# You'll need to do this to use Zelle's graphics:
from graphics import *
# If that doesn't work, make sure you have his graphics.py file.
# Then make sure you're running python in the folder with graphics.py.
# You can see what the folder is with :
import os
print "The current directory is ", os.getcwd()
# And you can change the folder with
os.chdir(full_path_to_a_folder)
With that stuff running, we'll explore it interactively class, doing things like this :
... and so on
Tic Tac Toe, anyone ?
"""
wiggle.py
a demo of Zelle's graphic library
Jim Mahoney | cs.marlboro.college | Sep 2012 | MIT License
"""
# A few new python functions that I'm using here :
# from random import randint # get randint() from random library
# randint(low, high) # generate a random between low & high
# from time import sleep # get sleep() from time library
# sleep(2) # do nothing for 2 sec
from graphics import *
from time import sleep
from random import randint
def main():
window = GraphWin("wiggle", 300, 300)
dot = Circle(Point(150,150), 20)
dot.setFill("red")
dot.draw(window)
for i in range(100):
dx = randint(-5, 5) # pixels
dy = randint(-5, 5)
dot.move(dx, dy)
sleep(0.1) # in seconds
wait = window.getMouse()
main()
Discuss the model of color that computers use :
If there's time :