First let's go over the homework.
Questions?
My solutions are in code/homework/sep17.
(You can put ?html at the end of a url to see color coded code.)
So far we know about
Next : starting to understand the notion of an "object".
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 check out the web page for Zelle's textbook 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.
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
#
# new python functions:
#
# from random import randint
# randint(low, high) low <= random num <= high
#
# from time import sleep
# sleep(2) do nothing for 2 sec
#
# Jim M | Sep 2012 | MIT License
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 :
We wrote a short program to generate a grid of colors.
"""
colorgrid !!!
"""
from graphics import *
def main():
window_width = 500
cell_width = 10
red = 100
window = GraphWin("colorgrid", window_width, window_width)
for x in range(0, window_width, cell_width):
for y in range(0, window_width, cell_width):
rect = Rectangle(Point(x, y), Point(x + cell_width, y + cell_width))
green = int(255*(x/window_width))
blue = int(255*(y/window_width))
color = color_rgb(red, green, blue)
rect.setFill(color)
# TODO: try setting the outline to the same color with setOutline
rect.draw(window)
wait = window.getMouse()
main()
last modified | size | ||
screenshot.png | Tue Dec 03 2024 05:39 pm | 226K |