Boston Python workshop/Saturday/ColorWall: Difference between revisions

no edit summary
imported>Jesstess
imported>Jesstess
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 8:
* Practice using functions and classes.
* Get experience with graphics programming using the Tkinter GUI toolkit.
* Practice reading other people's code.
 
== Project setup ==
Line 44 ⟶ 45:
 
<b>Check your understanding</b>: what saturation and value would you guess firetruck red have?
 
 
=== 2. Examine <code>Effect</code> and the interface its subclasses provide ===
Line 64 ⟶ 66:
<code>effects.py</code> exports and <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!)
 
 
Line 74 ⟶ 76:
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 to <code>run.py</code>. For example, if you only wanted to run <code>SaturationTest</code>, you could:
==Suggested exercises==
 
<pre>python run.py 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 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 Twinkle
</pre>
 
<b>Challenge</b>: make <code>Twinkle</code> twinkle with shades of red.
 
 
=== 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>.
 
==SuggestedBonus exercises==
 
===Checkerboard===
 
<ul>
<li>
Find and change the colors used in the <code>Checkerboards</code> effect, and re-run the effect:
 
Line 99 ⟶ 145:
 
re-run the effect, and see what changed.
</li>
<li>
Find and change the color of the columns in the <code>Matrix</code> effect, and re-run the effect:
 
What other patterns can you create by tweaking the math for this effect?
<pre>
python run.py 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.
</li>
<li>
Walk through <code>Twinkle</code>. Find explanations of the <code>random.randint</code> and <code>random.uniform</code> functions in the online documentation at
 
=== Matrix ===
http://docs.python.org/library/random.html.
 
Find and change the color of the columns in the <code>Matrix</code> effect, and re-run the effect:
Experiment with these functions at a Python prompt:
 
<pre>
python run.py Matrix
import random
random.randint(0, 1)
random.randint(0, 5)
random.uniform(-1, 1)
</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.
Then experiment with the numbers that make up the hue and re-run the effect:
 
<pre>
python run.py Twinkle
</pre>
</li>
<li>
Write your own effects!
</li>
</ul>
 
==Some Useful Links==
 
=== Write more of your own effects! ===
If you choose to use HSV colors you define instead of the dictionary, you may find some of the following useful:
* [http://www.yafla.com/yaflaColor/ColorRGBHSL.aspx A graphical HSV color picking website]
* [http://en.wikipedia.org/wiki/HSL_and_HSV HSV on Wikipedia]
* [http://docs.python.org/library/random.html Python random library]
* [http://docs.python.org/library/time.html Python time library]
 
You have color, time, randomness, letters, and more at your disposal. Go nuts!
[[Boston Python workshop 2/Saturday projects|&laquo; Back to the Saturday project page]]
Anonymous user