Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 110 - Intro. to Computing Lecture 17: Even More Robotran!

Similar presentations


Presentation on theme: "CSC 110 - Intro. to Computing Lecture 17: Even More Robotran!"— Presentation transcript:

1 CSC 110 - Intro. to Computing Lecture 17: Even More Robotran!

2 Announcements Quiz #4 will given out at end of class on Thursday  Covers Palgo and Robotran But not Robocop, since I never saw it.   Homework Solutions available on web page Thursday also marks a return to the book  We will be discussing operating systems

3 Subprograms & Functions & Handlers, Oh My! Often there is code we want to execute repeatedly, but not in a loop  Drawing the sides of a box  Writing each line in the letter “B”  Performing the Open… window  Turning robot around whenever “RUN” button pushed Want to do this without having to rewrite code everywhere.

4 Subprograms Subprograms begin with a valid name  Any letter combination EXCEPT the program name and reserved words are legal  We will use this, so use a meaningful name drawSquare better than pollyWantACracker Robotran subprograms begin and end similarly: define name The subprogram code goes here end

5 Simple Subprogram program example1 for s = 1 to 4 do drawLine end define drawLine pen down go forward 1 inch turn sharp right pen up end

6 More Complex Subprogram What if we want the lines to be different sizes?  Parameters allow us to pass values to a subprogram  Creates new variable(s) at subprogram start and assigns this variable the supplied value  Cannot use the parameters outside the subprogram

7 Working With Parameters program squares do drawSquare(0.7) go forward 0.1 inch do drawSqare(0.4) define drawSquare(parm length) pen down repeat 4 times go forward length inches turn sharp right end pen up end

8 Functions Sometimes subprograms should compute a value  Like Excel’s max(), min(), average(), etc.  Subprograms return 1 value at a time  These subprograms called “functions”  This happens a lot in the real world…  …but I lack a good classroom example. Please bear with me!

9 Function Example program walker var distance var angle while true do let distance = getRand(3) go forward distance inches let angle = getRand(360) turn right angle degrees end define getRand (parm max) returns float var number = random/100.0 * max return number end

10 Event Handlers Some things in life you can predict:  Death  Taxes  Politicians making impossible promises  People upset at politicians breaking promises  Cubs not winning the World Series For everything else there are Event Handlers™

11 Robotran Event Handlers These are subroutines that are run only when you hit a button Event handlers cannot use parameters  Remember, cannot be called like subroutines

12 Button Handler program handler go forward continue when RUN button is pressed var angle = random + 90 turn left angle degrees end when VIEW button is pressed var angle = random + 90 turn right angle degrees end

13 A Toy My Daughter Would Love program Shoshanna global var count = 0 go forward continue when RUN button is pressed let count = count + 1 stop turn left random degrees end when VIEW button is pressed beep end when PRGM button is pressed display count end

14 Your Turn Write program so your robot moves forward until it hits something. When it hits something it should stop and beep. Add event handlers so that when the RUN or PRGM button is hit your robot turns around 180 degrees and resumes going forward. Add a global variable which is set to 1 whenever the RUN button is pressed and set to 2 whenever the PRGM button is pressed. Whenever you hit something, display the number of the player who gets a point (e.g., the last player to hit their button) and then turn around and restart going forward.

15 For Next Lecture Read chapter 10 in the book Study for the Quiz #4


Download ppt "CSC 110 - Intro. to Computing Lecture 17: Even More Robotran!"

Similar presentations


Ads by Google