1 / 28

Lab 8

Lab 8. Exercise 1:. Follow the text’s suggestion and Google “color names list” Collect from the list a set of 25 colors or so and in a new Python program put them in a list data structure.

simeon
Download Presentation

Lab 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 8 Intro to Robots

  2. Exercise 1: • Follow the text’s suggestion and Google “color names list” • Collect from the list a set of 25 colors or so and in a new Python program put them in a list data structure. • Write code that creates a graphics window and then loops through the elements of the colors list, changing the color of the graphics window background. colors = [‘DarkCyan’, ‘DarkGoldenRod’, …] Intro to Robots

  3. Exercise 2: • Follow up on Exercise 1 by doing the following • Add a point on the graphics window each time you change the background color. • Use some kind of systematic method of assigning the point coordinates like (i,i) and increment i each time you change the background color. • The outcome will be something like: Intro to Robots

  4. Exercise 3: • In class we saw the code that draws the following picture. • Extend this code to draw the following picture. • Hint 1: Figure out what the four corner point coordinates will be. • Hint 2: Use 4 loops Intro to Robots

  5. Exercise 4: • Add to the program in Exercise 3 by including the codeto the loops that control the drawings of the lines. • What behaviour do you get? wait(0.3)L.undraw() Intro to Robots

  6. Exercise 5: • Consider the Bouncing Circle program # Moving circle; Animate a circle... from myro import * from random import * def main(): # create and draw the graphics window winWidth = winHeight = 500 w = GraphWin("Bouncing Circle", winWidth, winHeight) w.setBackground("white") # Create a red circle radius = 25 c = Circle(Point(53, 250), radius) c.setFill("red") c.draw(w) # Animate it dx = dy = 3 main() while timeRemaining(15): # move the circle c.move(dx, dy) # make sure it is within bounds center = c.getCenter() cx, cy = center.getX(), center.getY() if (cx+radius >= winWidth) or (cx-radius <= 0): dx = -dx if (cy+radius >= winHeight) or (cy-radius <= 0): dy = -dy wait(0.01) Intro to Robots

  7. Part 1: • Modify the previous program so that each time the Circle is moved it leaves a Point at its previous center. Intro to Robots

  8. Exercise 5, Part 2: • After you draw the Circle, draw a rectangle in the middle of the Bouncing Circle window and color it yellow: • Use the coordinates(200,100) and (300,400) • Now rerun the program and notice that the ballmoves behind the rectangle. R = (Rectangle(Point1,Point2)) # Point1 is the top left-hand corner # Point2 is the bottom right-hand corner (200,100) (300,400) Intro to Robots

  9. Exercise 5 (cont): • Modify the program so that the ball will bounce off the rectangle just like it bounces off the edge of the graphics window. • Initially there are four situations to consider: Intro to Robots

  10. Case 1: • If the ball approaches the rectangle side whose coordinates are given below then the test for the program variables is: (200,100) (300,100) cx + radius >= 200 and cx – radius <= 300 andcy between 100 and 400 program code: ( (cx + radius >= 200) and (cx – radius <= 300) and ( cy >= 100) and (cy <= 400)) (200,400) (300,400) Intro to Robots

  11. Cases 2, 3 and 4: • Evaluate the same expressions for the following three cases: • Put this code into the original program and watch the circle move around the window. Intro to Robots

  12. (200,100) (200,400) Exercise 5 (cont): • If you implement this code you will see that everything works well except at the corners. • In this case we can see that the test conditions fails. What part of it fails? ( (cx + radius >= 200) and (cx – radius <= 300) and ( cy >= 100) and (cy <= 400)) fails: Intro to Robots

  13. Exercise 5 (cont): In order for the circle to bounceoff the yellow rectangle it must be inside the outer rectangle in one of the areas labeled A, B, C, D or 1 … 8. This is not enough however. In theareas labeled 4 and 5 the circle caninside the outer rectangle but still not be touching the inner rectangle. Inside B, C, D condition: ? Inside 1 condition: inside outer rectangle and not in A, B, C or D and cx <= 200 and cy <= 100 and 200 – cx <= 100 – cy Inside 2 .. 8 condition? Inside 1 and touching rectanglecondition: inside 1 and (200 – cx)2 + (cy – 100)2 < radius2 Variables: (cx,cy) - center of circle radius – radius of circle Inside outer rectangle condition: cx + radius >= 200 and cx – radius <= 300 and cy + radius >= 100 and cy – radius <= 400Inside A condition: inside rectangle and cx >= 200 and cx <= 300 and cy <= 100 Intro to Robots

  14. Exercise 5 (cont): • Modify your program to take these new conditions into account. The pseudocode is: if inside outer rectangle: if inside A: flip dy elif inside B: flip dx elif inside C: flip dy elif inside D: flip dx elif inside 1: if touching inner rectangle: flip dy elif inside 2: if touching inner rectangle: flip dx . . . ( 6 more cases) Added clear rectangle tosee where Circle center iswhen Circle touches rectangle Intro to Robots

  15. Exercise 5 (cont): entered area 2 are exited correctly • A bug: Circle starts here back and forth, back andforth until it exits area 2 passed through area 8 ok entered area 8 and got “stuck” Intro to Robots

  16. Exercise 5, Bug Analysis: • Each time we move the Circle we move it 3 pixels on the x-axis and 3 pixels on the y-axis. • When we cross the line into the outer rectangle we flip either the x- or y- increment and the next move typically takes us back outside the rectangle. • However this is not necessarily true in the corners . . . . . . . . . bouncing back weare still inside and so flip again; this keepsus inside until the end of the line. Intro to Robots

  17. Exercise 5, Bug Solution: • When you enter the outer rectangle and flip either the x-increment or the y-increment you must not flip again until you have at least left the outer rectangle. • Our problem here is that once too far inside the outer rectangle we stay there by flipping back and forth. • Code Solution: Add a boolean flag to your program that gets turned on whenever you flip and only turned off once you leave the rectangle.Then don’t ever allow a flip to happen if the flag is turned on. Intro to Robots

  18. Exercise 5, Bug Solution Pseudocode • Exercise: Implement this pseudocode # outside while-loop turn flag off if inside outer rectangle: if flag turned on: continue # skips to top of while-loop if inside A: flip dy turn flag on elif inside B, C, D: . . . elif inside 1: if touching inner rectangle: # inside circular arc flip dy turn flag on elif inside 2, … , 8: . . . ( 6 more cases) else: # outside outer rectangle turn flag off Intro to Robots

  19. Exercise 5 (one last time) • Make the robot beep each time you flip the x- or y-increment. • Make it beep differently if it is hitting the outside will than if it is hitting the rectangle. Intro to Robots

  20. Exercise 6 (Sound): • The human ear can not distinguish between sounds which are very close together. • Execute the beep() command over a range of frequency values to determine your audible range. • Execute the beep() command over a range of close-by frequency pairs to see how far apart the pairs need to be for you to distinguish the sounds beep(1,300), beep(1,400), beep(1,1000), beep(1,2000), … beep(1, 300), beep(1, 305), … beep(1, 1000), beep(1, 1010), beep(1, 440), beep(1, 441) … Intro to Robots

  21. Exercise 7 (siren): • A siren is two sounds repeated constantly. • Find a good pair for sounds for a siren. Intro to Robots

  22. Exercise 8: Musical Scale Data Structure • In the past we have used the dictionary data structure to hold data that comes in pairs. • Remember the englishToSpanish dictionary in Chap 1? • Create a dictionary called notes with notes from 9 different octaves (from A0 to C8). These are the notes Scribbler is able to make. • Method 1: Key in by hand. • Method 2:: freqs = [‘C’, ‘C#’, D, ‘D#’, ‘E’, ‘F’, ‘F#’, ‘G’, ‘G#’, ‘A’, ‘A#’, ‘B’] notes = {} notes[‘A0’] = 27.5; notes[‘A#0’] = 29.14; notes[‘B0’] = 30.87; notes[‘C8’] = 4186.0 for i in range(7): j = 1 for f in freqs: notes[f+str(i+1)] = (notes[‘B0’])*2**(i+j/12.0) j = j+1 Intro to Robots

  23. Musical Notes: • Google ‘musical note frequencies’ and compare what you find there with the numbers you have created with the code on the previous page. Intro to Robots

  24. Exercise 9: • Pick a simple song you remember and make your robot play your song. • Remember that the robot can only play chords with two notes. Intro to Robots

  25. Exercise 10: • Working with another team in the lab, play a duet – one robot plays the treble clef and the other the bass clef notes. http://en.wikipedia.org/wiki/Clef Intro to Robots

  26. Exercise 11: • Since our robot may at times need to express emotion you might find some short tunes that express • Determination • Sadness • Victory • Curiosity • Plodding along • Full steam ahead • Any other emotions that seem appropriate … Intro to Robots

  27. Exercise 12: • Write a function that will draw a maple leaf.Now cover a graphics window with maple leaves of different sizes (small, medium, large) and colors (red, yellow, orange). def mapleLeaf(anchor point, size, color, window): . . . Intro to Robots

  28. Exercise 13: • Draw the following robot using the function: • Now use this function to draw the following picture. def drawRobot(anchor point, window): . . . def drawPyramid(window): . . . Intro to Robots

More Related