CSC111 Homework 4 Solutions 2011

From dftwiki3
Revision as of 13:15, 17 October 2011 by Thiebaut (talk | contribs) (Music, Extra-Credit Option)
Jump to: navigation, search

--D. Thiebaut 14:13, 17 October 2011 (EDT)


Music, Extra-Credit Option


NotesKeysExtraCredits.png
# hw4x.py
# 111a-bq
# Valerie Cook
# Generate 7 notes on a score not all on the same line with a loop
# and the keys of a piano underneath using a single loop

from graphics import *
import random

def main():
    # define the width and height of the window
    width = 800
    height = 600

    # open a graphics window
    win = GraphWin( "Hw4 - 111a-bq", width, height )

    # draw lines to make the score
    for i in range (5):
        line = Line( Point( 250, (60+20*i) ), Point( 550, (60+20*i)) )
        line.setFill( "black" )
        line.draw( win )
 
    # draw 7 circles placed on the score with a radius of 5
    radius = 10 
    for i in range( 7 ):
        c = Circle( Point( 280+40*i, 60+10*i), radius )

        # make the circle white
        c.setFill( "white" )
        c.setWidth(3)
        c.draw( win )

    # define key points and color of the keys
    #                    X    Y    height color
    whitekey1 = [ 275, 225, 500,   "white" ]
    whitekey2 = [ 325, 275, 500,   "white" ]
    whitekey3 = [ 375, 325, 500,   "white" ]
    whitekey4 = [ 425, 375, 500,   "white" ]
    whitekey5 = [ 475, 425, 500,   "white" ]
    whitekey6 = [ 525, 475, 500,   "white" ]
    whitekey7 = [ 575, 525, 500,   "white" ]
    blackkey1 = [ 290, 260, 390,   "black" ]
    blackkey2 = [ 340, 310, 390,   "black" ]
    blackkey3 = [ 390, 360, 390,   "black" ]
    blackkey4 = [ 490, 460, 390,   "black" ]
    blackkey5 = [ 540, 510, 390,   "black" ]

    # create list of the keys in the order they should be drawn
    keys = [ whitekey1,whitekey2, whitekey3, whitekey4, whitekey5, whitekey6,
             whitekey7, blackkey1, blackkey2, blackkey3, blackkey4, blackkey5 ]
        
    # draw the keys
    for ptA, ptB, ptC, color in keys:
        key = Rectangle(Point(ptA, 250), Point(ptB, ptC))
        key.setFill(color)
        key.setWidth(5)
        key.draw(win)

              
    # wait for the user to click the mouse on the window
    win.getMouse()
    win.close()

main()