View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Sp2001 Midterm Review: Coding Sensor and Turtle

Try Coding Sensor and Turtle here:

pen point
pen _ Pen new.
[Sensor redButtonPressed] whileFalse: [].

point _ Sensor cursorPoint.
pen up; goto: point; down.
4 timesRepeat:[pen go: 100; turn: -90].
Bharath Hemachandran

Bharath I think you're supposed to draw the box when the mouse is unclicked... I don't seem to be able to figure out how to do this...
Arkady Shraybman

Arkady, look at Bharath's code again. Better yet, try it. Mark Guzdial

when I ran this code the box was drawn as soon as the left(red) button was clicked , however the assignment says quote unquote

"Waits for the user to press down and UP on the left ("red") button..."

so are you telling me that pressing down and then up is the same thing as just pressing down? Or is my squeak behaving differently than Bharath's squeak?

Technically if you press down in one place and then let go of the left mouse button in a different place then going by the requirement the place where you let go of the button is where the box should be drawn.
Arkady Shraybman

You're right, Arkady – the code does need to be tweaked a bit. Mark Guzdial

Thanks for the answer. :-) chok

pen := Pen new.
[Sensor redButtonPressed] whileFalse: [].
[Sensor redButtonPressed] whileTrue: [].

point _ Sensor cursorPoint.
pen up; goto: point; down.
4 timesRepeat:[pen go: 100; turn: -90].

Can the person that posted the correct answer please explain why this works? It would be helpful. Thanks
Arkady Shraybman

Here's my shot at deciphering it... correct me if i'm wrong.

"create a new pen - you need to own a pen before you can use it"
pen := Pen new.
"While the red button is not pressed, go into an infinite loop"
[Sensor redButtonPressed] whileFalse: [].
"after the person presses the redbutton - go into another infinite loop until he releases the button."
[Sensor redButtonPressed] whileTrue: [].
"get the point where our cursor is"
point _ Sensor cursorPoint.
"move the pen over to the location without drawing a line"
pen up; goto: point; down.
"and draw a box"
4 timesRepeat:[pen go: 100; turn: -90].

I didn't post it, but that should give you the idea.
Jaroslav Tyman



Link to this Page