Difference between revisions of "CSC111 Homework 4 Solutions 2011"

From dftwiki3
Jump to: navigation, search
(Music, Extra-Credit Option)
(Music, Extra-Credit Option)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 14:13, 17 October 2011 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] 14:13, 17 October 2011 (EDT)
 
----
 
----
 +
 +
=Music=
 +
<br />
 +
[[Image:NotesKeys.png|right|200px]]
 +
<source lang="python">
 +
# hw4b.py
 +
# Julie Woods
 +
# 111a-ba
 +
# This is the beginning notes to "Good King Wenceslas" for the cello,
 +
# without the stems for the notes or the clef sign
 +
# Beneath are the 7 keys of the piano.
 +
 +
from graphics import *
 +
def main():
 +
    # open a graphics window
 +
    width = 800
 +
    height = 600
 +
    win = GraphWin( "Hw4b - 111a-ba", width, height )
 +
 +
    # draw 5 lines
 +
    for i in range (1, 6):
 +
        line = Line( Point( 10, 75 + i*35 ), Point( width-10, 75 + i*35 ) )
 +
        line.setFill( "black" )
 +
        line.setWidth( 5 )
 +
        line.draw( win )
 +
 +
    # draw 7 circles placed on separate lines
 +
    radius = 15
 +
    for j in range (1,3):
 +
        c = Circle( Point( 100*j, 127.5 ), radius )
 +
        # make the circle look like a note
 +
        c.setFill( "black" )
 +
        c.draw( win ) 
 +
    for k in range (400, 601, 200):
 +
        c4 = Circle( Point( k, 162.5 ), radius )
 +
        c4.setFill( "black" )
 +
        c4.draw( win )
 +
 +
    c3 = Circle( Point( 300, 180 ), radius )
 +
    c3.setFill( "white" )
 +
    c3.setWidth (7)
 +
    c3.draw( win )
 +
   
 +
    c5 = Circle( Point( 500, 180 ), radius )
 +
    c5.setFill( "black" )
 +
    c5.draw( win )
 +
 +
    c7 = Circle( Point( 700, 145 ), radius )
 +
    c7.setFill( "black" )
 +
    c7.draw( win )
 +
 +
    # Make bar between notes
 +
    bar = Line( Point( 350, 75+ 35 ), Point( 350, 250 ) )
 +
    bar.setFill( "black" )
 +
    bar.setWidth( 5 )
 +
    bar.draw( win )
 +
 +
    # white piano keys
 +
    for i in range (250, 551, 50):
 +
        rect    = Rectangle( Point( i, 300 ), Point( i+50, 500))
 +
        rect.setWidth( 5 ) 
 +
        rect.setFill( "white" )
 +
        rect.draw( win )
 +
       
 +
    # black piano keys
 +
    for j in range (283, 400, 50):
 +
        rect    = Rectangle( Point( j, 300 ), Point( j+34, 410))
 +
        rect.setWidth( 5 ) 
 +
        rect.setFill( "black" )
 +
        rect.draw( win )
 +
 +
    for k in range (483, 550, 50):
 +
        rect    = Rectangle( Point( k, 300 ), Point( k+34, 410))
 +
        rect.setWidth( 5 ) 
 +
        rect.setFill( "black" )
 +
        rect.draw( win )
 +
 +
   
 +
    # Wait for user to click mouse.
 +
    win.getMouse()
 +
    win.close()
 +
 +
main()
 +
</source>
 +
<br />
  
 
=Music, Extra-Credit Option=
 
=Music, Extra-Credit Option=
Line 73: Line 158:
  
 
</source>
 
</source>
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
[[Category:CSC111]][[Category:Python]][[Category:Homework]]

Revision as of 15:04, 17 October 2011

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


Music


NotesKeys.png
# hw4b.py
# Julie Woods
# 111a-ba
# This is the beginning notes to "Good King Wenceslas" for the cello,
# without the stems for the notes or the clef sign
# Beneath are the 7 keys of the piano.

from graphics import *
def main():
    # open a graphics window
    width = 800
    height = 600
    win = GraphWin( "Hw4b - 111a-ba", width, height )
 
    # draw 5 lines
    for i in range (1, 6):
        line = Line( Point( 10, 75 + i*35 ), Point( width-10, 75 + i*35 ) )
        line.setFill( "black" )
        line.setWidth( 5 )
        line.draw( win )

    # draw 7 circles placed on separate lines
    radius = 15 
    for j in range (1,3):
        c = Circle( Point( 100*j, 127.5 ), radius )
        # make the circle look like a note
        c.setFill( "black" )
        c.draw( win )  
    for k in range (400, 601, 200):
        c4 = Circle( Point( k, 162.5 ), radius )
        c4.setFill( "black" )
        c4.draw( win )

    c3 = Circle( Point( 300, 180 ), radius )
    c3.setFill( "white" )
    c3.setWidth (7)
    c3.draw( win )
    
    c5 = Circle( Point( 500, 180 ), radius )
    c5.setFill( "black" )
    c5.draw( win )

    c7 = Circle( Point( 700, 145 ), radius )
    c7.setFill( "black" )
    c7.draw( win )

    # Make bar between notes
    bar = Line( Point( 350, 75+ 35 ), Point( 350, 250 ) )
    bar.setFill( "black" )
    bar.setWidth( 5 )
    bar.draw( win )

    # white piano keys
    for i in range (250, 551, 50):
        rect     = Rectangle( Point( i, 300 ), Point( i+50, 500))
        rect.setWidth( 5 )  
        rect.setFill( "white" )
        rect.draw( win )
        
    # black piano keys
    for j in range (283, 400, 50):
        rect     = Rectangle( Point( j, 300 ), Point( j+34, 410))
        rect.setWidth( 5 )  
        rect.setFill( "black" )
        rect.draw( win )

    for k in range (483, 550, 50):
        rect     = Rectangle( Point( k, 300 ), Point( k+34, 410))
        rect.setWidth( 5 )  
        rect.setFill( "black" )
        rect.draw( win )

    
    # Wait for user to click mouse. 
    win.getMouse()
    win.close()

main()


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()