Anonymous user
Boston Python workshop/Saturday/ColorWall: Difference between revisions
Boston Python workshop/Saturday/ColorWall (view source)
Revision as of 19:36, 8 July 2012
, 11 years agono edit summary
imported>Jesstess No edit summary |
imported>Jesstess No edit summary |
||
(10 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 23 ⟶ 24:
<pre>python run.py -a</pre>
== Project steps ==
Line 38 ⟶ 39:
* 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?
=== 2. Examine <code>Effect</code> and the interface its subclasses provide ===
Line 45 ⟶ 51:
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 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!)
=== 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 to <code>run.py</code>. For example, if you only wanted to run <code>SaturationTest</code>, you could:
<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>.
==Bonus exercises==
===Checkerboard===
Find and change the colors used in the <code>Checkerboards</code> effect, and re-run the effect:
Line 69 ⟶ 145:
re-run the effect, and see what changed.
What other patterns can you create by tweaking the math for this effect?
=== Matrix ===
Find and change the color of the columns in the <code>Matrix</code> effect, and re-run the 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.
=== Write more of your own effects! ===
You have color, time, randomness, letters, and more at your disposal. Go nuts!
|