ColorWall: Difference between revisions

6,208 bytes added ,  8 years ago
imported>Jesstess
 
 
(18 intermediate revisions by 8 users not shown)
Line 1:
[[File:Colorwall_matrix.png|right|300px]]
#REDIRECT [[Boston Python workshop/Saturday/ColorWall]]
 
== Project ==
 
Program graphical effects for a ColorWall.
 
== Goals ==
 
* Have fun experimenting with and creating graphical effects.
* Practice using functions and classes.
* Get experience with graphics programming using the Tkinter GUI toolkit.
* Practice reading other people's code.
 
== Download source code ==
<ul>
<li>[https://openhatch.org/wiki/PyCon_intro_to_open_source#Goal_.232:_install_git Install git] if you have not already done so. </li>
<li> Clone source </li>
<pre> git clone git@github.com:jesstess/ColorWall.git </pre>
</ul>
 
== Project steps ==
 
=== 1. Learn about HSV values ===
 
Run the ColorWall effects again with
 
<pre>python run.py</pre>
 
The names of the effects are printed to the terminal as they are run. Pay particular attention to the first 4 effects:
* SolidColorTest
* HueTest
* SaturationTest
* ValueTest
 
In all of these effects, a tuple <code>hsv</code> containing the hue, saturation, and value describing a color are passed to <code>self.wall.set_pixel</code> to change the color of a single pixel on the wall.
 
What are the differences between these tests? Given these difference and how they are expressed visually, how does varying hue, saturation, or value change a color?
 
<b>Check your understanding</b>: what saturation and value would you guess firetruck red have?
 
<b>Step 1 resources</b>:
<ul>
<li>
Using tuples: http://www.afterhoursprogramming.com/tutorial/Python/Tuples/
</li>
<li>
Using the <code>range</code> function to produce a sequence of numbers: http://docs.python.org/tutorial/controlflow.html#the-range-function
</li>
<li>
Using the <code>time</code> module to sleep (do nothing for a bit) inside your program: http://docs.python.org/library/time.html
</li>
</ul>
 
=== 2. Examine <code>Effect</code> and the interface its subclasses provide ===
 
All of the effects inherit from the <code>Effect</code> class. Examine this class and its <code>__init__</code> and <code>run</code> methods.
 
What is the purpose of the <code>__init__</code> method?
 
What is the purpose of the <code>run</code> method?
 
Open up <code>run.py</code> and look at this chunk of code at the bottom of the file:
 
<pre>
for effect in effects_to_run:
new_effect = effect(wall)
print new_effect.__class__.__name__
new_effect.run()
</pre>
 
<code>effects.py</code> exports an <code>Effects</code> list at the bottom of the file. <code>run.py</code> goes through every effect in that list, creates a new instance of the effect, and invokes its <code>run</code> method.
 
<b>Check your understanding</b>: what would happen if you added an effect to the <code>Effects</code> list that didn't implement a <code>run</code> method? (Try it!)
 
<b>Step 2 resources</b>:
<ul>
<li>
Creating and using Python functions: http://www.sthurlow.com/python/lesson05/
</li>
<li>
Creating and using Python classes: http://www.sthurlow.com/python/lesson08/
<li>
A discussion on <code>__init__</code> and <code>self</code>: http://stackoverflow.com/questions/625083/python-init-and-self-what-do-they-do
</li>
</ul>
 
=== 3. Examine the nested <code>for</code> loop in <code>SolidColorTest</code> ===
 
<pre>for x in range(self.wall.width):
for y in range(self.wall.height):
self.wall.set_pixel(x, y, hsv)</pre>
 
This code loops over every pixel in the ColorWall, setting the pixel to a particular <code>hsv</code> value. After that <code>for</code> loop is over, <code>self.wall.draw()</code> updates the display.
 
<b>Check your understanding</b>: what would happen if you moved the <code>self.wall.draw()</code> to inside the inner <code>for</code> loop, just under <code>self.wall.set_pixel(x, y, hsv)</code> in <code>SaturationTest</code>? (Try it!)
 
<b>Tip</b>: you can run individual tests by passing their names as command line arguments (argument -e or --effects) to <code>run.py</code>. For example, if you only wanted to run <code>SaturationTest</code>, you could:
 
<pre>python run.py -e SaturationTest</pre>
 
=== 4. Implement a new effect called <code>RainbowTest</code> ===
 
It should run for 5 seconds, cycling through the colors in the rainbow, pausing for a moment at each color.
 
Remember to add your effect to the <code>Effect</code> list at the bottom of <code>effects.py</code>!
 
Test your new effect with
 
<pre>python run.py -e RainbowTest</pre>
 
=== 5. Play with the randomness in <code>Twinkle</code> ===
 
Walk through <code>Twinkle</code>. Find explanations of the <code>random.randint</code> and <code>random.uniform</code> functions in the online documentation at http://docs.python.org/library/random.html.
 
Experiment with these functions at a Python prompt:
 
<pre>
import random
random.randint(0, 1)
random.randint(0, 5)
random.uniform(-1, 1)
</pre>
 
Then experiment with the numbers that make up the hue and re-run the effect:
 
<pre>
python run.py -e Twinkle
</pre>
 
<b>Challenge</b>: make <code>Twinkle</code> twinkle with shades of blue.
 
=== 6. Implement a new effect that involves randomness! ===
 
Remember to add your effect to the <code>Effect</code> list at the bottom of <code>effects.py</code>.
 
==Bonus exercises==
 
===1. Checkerboard===
 
Find and change the colors used in the <code>Checkerboards</code> effect, and re-run the effect:
 
<pre>
python run.py -e Checkerboards
</pre>
 
Then change the line
 
<pre>
if (x + y + i) % 2 == 0:
</pre>
 
to
 
<pre>
if (x + y + i) % 3 == 0:
</pre>
 
re-run the effect, and see what changed.
 
What other patterns can you create by tweaking the math for this effect?
 
===2. Matrix ===
 
Find and change the color of the columns in the <code>Matrix</code> effect, and re-run the effect:
 
<pre>
python run.py -e Matrix
</pre>
 
Each column that we see on the wall corresponds to a <code>Column</code> object. Add some randomness to the color used by each column (the variable whose value you changed above) using the <code>random.random</code> function, re-run the effect, and see what happens.
 
===3. Write more of your own effects! ===
 
You have color, time, randomness, letters, and more at your disposal. Go nuts!
 
 
===Congratulations!===
 
You've read, modified, and added code to a software project that makes art out of pixels. Keep practicing!
 
[[File:Fireworks.png|150px]]
[[File:Balloons.png|150px]]