Created
March 21, 2017 13:51
-
-
Save jglee72/b1cdf9e7498d18b13da28510ffe47fa2 to your computer and use it in GitHub Desktop.
drum_R01.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
#TODO: Adjust size to screen size (iPhone) | |
#DONE: Adjusted to rect(x * 47, y * 42, 46, 41) | |
# - decr y * val to accomodate new buttons | |
#DONE: Clear : added 16th row: column 1 clears data | |
#DOME: Random: row 16 column 2 uses randint() to generate random data | |
#TODO: have a random for just column 16 pushed (exc clear button) | |
#TODO: play/pause buttons | |
from scene import * | |
from sound import play_effect, set_volume, load_effect | |
from colorsys import hsv_to_rgb | |
import pickle | |
import ui | |
import random | |
class DrumMachine (Scene): | |
def setup(self): | |
self.beat = 0.0 | |
set_volume(5.0) | |
self.sounds = ['drums:Drums_01', 'drums:Drums_02', 'drums:Drums_03', 'drums:Drums_04', | |
'drums:Drums_10', 'drums:Drums_11', 'drums:Drums_16', 'drums:Drums_14'] | |
for effect in self.sounds: | |
load_effect(effect) | |
try: | |
with open('Drums.data', 'r') as f: | |
self.grid = pickle.load(f) | |
except EOFError: | |
self.grid = [[False for col in xrange(17)] for row in xrange(8)] #17 for extra row, file needs to blank, gone, or corrupted | |
def draw(self): | |
background(0,0,0) | |
last_beat = int(self.beat) | |
self.beat += 1.0/7.5 | |
self.beat %= 16 | |
play = int(self.beat) != last_beat | |
for y in xrange(17): # rows 17 here shows 17th row | |
beat_row = int(self.beat) == y | |
for x in xrange(8): #columns | |
if self.grid[x][y]: | |
h = x / 8.0 | |
r, g, b = hsv_to_rgb(h, 1, 1) | |
if play and int(self.beat) == y: | |
play_effect(self.sounds[x]) | |
if beat_row: | |
h = x / 8.0 | |
r, g, b = hsv_to_rgb(h, 1, 1) | |
fill(r, g, b) | |
else: | |
r, g, b = hsv_to_rgb(h, 1, 0.5) | |
fill(r, g, b) | |
elif beat_row: | |
fill(1, 1, 1) | |
else: | |
fill(0.50, 0.50, 0.50) | |
#rect(x * 47, y * 42, 46, 41) | |
rect(x * 47, y * 38 , 46, 37) | |
def touch_began(self, touch): | |
#(x,y)pos of touch | |
x, y = touch.location.as_tuple() | |
#identify grid in ({0,7}{0,16}) | |
x = int(x / 47) | |
y = int(y / 38)#follows rect above | |
#update grid colour for grid touched | |
if y < 17: #17 here allows activate 17 row | |
self.grid[x][y] = not self.grid[x][y] | |
if y==16: #Check command line row | |
if x==0: #clear data locally | |
self.grid = [[False for col in xrange(17)] for row in xrange(8)] | |
else: #randomize column x | |
for col in xrange(17): | |
self.grid[x][col]=random.choice([0, 0 ,1,0]) # Random choice = less full | |
#self.grid[x][col]=random.randint(0,1) | |
def stop(self): | |
#Update drum.data with current data | |
with open('Drums.data', 'w') as f: | |
pickle.dump(self.grid, f) | |
run(DrumMachine(), PORTRAIT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment