120 likes | 254 Views
Introduction to Computer Science – Chapter 8. CSc 2010 Spring 2011 Marco Valero. Overview. Drawing a canvas Objects and constructors Animation Scope Text and images Sound Scales and making music Songs. Drawing a canvas. We’ve seen the canvas before Asking a question
E N D
Introduction to Computer Science – Chapter 8 CSc 2010 Spring 2011 Marco Valero
Overview • Drawing a canvas • Objects and constructors • Animation • Scope • Text and images • Sound • Scales and making music • Songs
Drawing a canvas • We’ve seen the canvas before • Asking a question • Showing a picture • myCanvas = GraphWin() • myCanvas.close() • myCanvas.setBackground(‘white’) • myCanvas = GraphWin(‘My Masterpiece’, 200, 300)
Drawing a canvas • Uses a grid layout of coordinates for position • (0,0) is the top left corner • We can create points and lines • p = Point(100, 50) • p.draw(myCanvas) • l = Line(Point(0,0), Point(100, 200)) • l.draw(myCanvas)
Objects and constructors • We’ve seen this style of functions before • myList.append(‘a’) • myList.sort() • <object>.<function identifier>(<parameters>) • Think of variables as an object that encapsulates some data and functionality • We construct these objects with special functions called constructors • p = Point(100, 50)
Objects and constructors • Objects may have many member functions • p = Point(100, 50) • p.getX() returns 100 • p.getY() returns 50 • l = Line(p, Point(75,200)) • l.getP1().getX() returns? • L.getP2().getX() returns?
Animation • <graphic object>.undraw() • Erases object from the screen • Circles are defined as a point with a radius • c = Circle(Point(100, 50), 30) • c.draw(myCanvas) • By using the undraw and draw methods we can animate our graphics
Scope • Scope is the ability to ‘see’ or ‘know of’ an identifier in python • Scope of an identifier in python is in ‘textual order’, and local to functions • A global defined at the top will be visible to all • A variable within a function will only be visible to that specific function’s body
Text and images • We can create graphic representations of text • myText = Text(<anchor point>, <string>) • mytext = Text(Point(50,50), ‘hello world’) • mytext.draw(myCanvas) • We can also display images • myphoto = Image(<center point>, <file name>)
Sound • We have used the beep command • beep(<seconds>, <frequency>) • We can make our computer beep as well • computer.beep(<seconds>, <frequency>) • Sorry, no wobble bass electro music
Scales and making music • A scale is 12 notes • C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B • Also known as an octave • We can double an octave by doubling the frequency • C3 (130.815) • C4 (261.63) • The lowest scribbler can go is A0(27.5 Hz)
Songs • We can create a string of notes and durations for myro to play as a song • Note1 [Note2] Fraction of a Whole Note • ‘C5 1;’ • readSong(<file name>) • Reads a file and returns a song object • playSong(<song>) • Plays the given song • makeSong(<textual song>) • Returns a song object based on the string of notes given