Last active
August 29, 2015 14:19
-
-
Save wibbia/8cf01b58233a1affeb13 to your computer and use it in GitHub Desktop.
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
import pygame, sys, winsound, time | |
pygame.init() | |
size = width, height = 865, 365 | |
s = pygame.display.set_mode(size) | |
class Notes(pygame.sprite.Sprite): | |
def __init__(self, x, y, colour, sound_control): | |
pygame.sprite.Sprite.__init__(self) | |
self.image = pygame.Surface([50, 200]) | |
self.image.fill(colour) | |
self.rect = self.image.get_rect() | |
self.rect.x = x | |
self.rect.y = y | |
self.colour = colour | |
self.sound_control = sound_control | |
self.pressed = False | |
def set_pressed(self, pressed): | |
self.pressed = pressed | |
if pressed: | |
self.image.fill([130, 130, 130]) | |
else: | |
self.image.fill(self.colour) | |
def frequency(n): | |
return (2 ** ((n - 49)/12.0)) * 440 | |
counter = 0 | |
totaltime = 0 | |
def length(): | |
global counter | |
if mouse_down[-1] == 1: | |
if counter == 0: | |
time1 = time.time() | |
else: | |
time2 = time.time() | |
if mouse_down[-1] == 0: | |
totaltime = time2 - time1 | |
counter += 1 | |
amount_of_notes = 14 | |
bottom_pitch = 39 | |
white_note_pitches = [1, 3, 5, 6, 8, 10, 12] | |
black_note_pitches = [2, 4, None, 7, 9, 11, None] | |
white_note = pygame.sprite.Group() | |
black_note = pygame.sprite.Group() | |
layered_updates = pygame.sprite.LayeredUpdates() | |
count = 0 | |
total_time = 0 | |
start_x = 80 | |
for i in range(0,amount_of_notes): | |
letter = i % 7 | |
if letter == 2 or letter == 6: | |
continue | |
x = start_x + (55 * i) | |
pitch = bottom_pitch + (int(i / 7) * 12) + black_note_pitches[letter] | |
new_note = Notes(x, 45, [0,0,0], pitch) | |
black_note.add(new_note) | |
start_x = 50 | |
for i in range(0,amount_of_notes): | |
letter = i % 7 | |
x = start_x + (55 * i) | |
pitch = bottom_pitch + (int(i / 7) * 12) + white_note_pitches[letter] | |
new_note = Notes(x, 120, [255, 255, 255], pitch) | |
white_note.add(new_note) | |
layered_updates.add(white_note.sprites()) | |
layered_updates.add(black_note.sprites()) | |
s.fill([0,130,255]) | |
mouse_down = [0] | |
Cont = False | |
found = 0 | |
while True: | |
time.sleep(0.01) | |
for event in pygame.event.get(): | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
mouse_down.append(1) | |
elif event.type == pygame.MOUSEBUTTONUP: | |
mouse_down.remove(1) | |
print(mouse_down) | |
pos = x,y = pygame.mouse.get_pos() | |
found = None | |
for i in range(0, len(black_note.sprites())): | |
if black_note.sprites()[i].rect.collidepoint(pos): | |
found = frequency(black_note.sprites()[i].sound_control) | |
break | |
if found == None: | |
for note in white_note.sprites(): | |
if note.rect.collidepoint(pos): | |
if mouse_down[-1] == 1: | |
note.set_pressed(True) | |
found = frequency(note.sound_control) | |
length() | |
Cont = True | |
if Cont == False: | |
continue | |
else: | |
winsound.Beep(int(found), int(totaltime)) | |
break | |
if found == None: | |
continue | |
layered_updates.draw(s) | |
pygame.display.update() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment