Jump to content

ColorWall: Difference between revisions

5,522 bytes added ,  11 years ago
no edit summary
imported>Jesstess
 
imported>Jesstess
No edit summary
Line 1:
== Project ==
#REDIRECT [[Boston Python workshop/Saturday/ColorWall]]
 
Program graphical effects for a ColorWall using the Tkinter GUI toolkit.
 
== Goals ==
 
* Have fun experiment 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.
 
== Project setup ==
 
=== Download and un-archive the ColorWall project skeleton code ===
 
* http://web.mit.edu/jesstess/www/BostonPythonWorkshop6/ColorWall.zip
 
Un-archiving will produce a <code>ColorWall</code> folder containing several Python files, including: run.py, effects.py, and advanced_effects.py.
 
=== Test your setup ===
 
From a command prompt, navigate to the <code>ColorWall</code> directory and run
 
<pre>python run.py -a</pre>
 
You should see a window pop up and start cycling through colorful effects. If you don't, let a staff member know so you can debug this together.
 
== Project steps ==
 
=== 1. Learn about HSV values ===
 
Run the ColorWall effects again with
 
<pre>python run.py -a</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?
 
 
=== 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 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:
 
<pre>
python run.py 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?
 
 
=== 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!
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.