sep 21
- go over homework
- where we've been :
- reading and writing to terminal and files
- numbers, strings
- variables, values, lists
- various built-in functions and operations
chap 5 : exploring "objects" with a graphics API
This is usually a fun week ...
objects
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.
graphics.py
See the
resources or
Zelle's book page for 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.
Stuff to play around with interactively class:
- create a window
- with given size
- with given background
- create a circle
- give it a color
- draw it
- move it
- animate it
- "from time import sleep"
- "import random; r = Random(); r.randint(low,high)"
- get a mouse click
- draw an image from a .jpeg file
... and so on
Tic Tac Toe, anyone ?