[MWForum]RE: random numbers with setpos

Wendy Petti mwforum@lists.mathcats.com
Mon, 9 Feb 2004 11:33:01 -0500


I'd like to add on to Daniel's information about using setpos with random
numbers.  This had confused me too for a long time too, until I got some
help beyond the MW help menus, because for years I had only seen setpos used
with brackets.  But in fact all of these alternatives work:

* * *

Example 1:
setpos random 200 random 200

(This command would only place the turtle in the upper right quadrant.)

* * *

Example 2:
setpos -250 + random 500 setpos -150 + random 300

(This command would place the turtle anywhere on a project of size 500 x
300.)

Example 3:
setpos (se -250 + random 500 -150 + random 300)

(This command does *exactly* the same thing as Example 2, using se
(sentence) instead of list.  Note that when when more than 2 inputs are
being combined using SE, then the SE command and its inputs need to be
enclosed in parentheses.)

* * *

Example 4:
Below is a complex example using a cluster of turtles to follow a turtle
named brad and another cluster of turtles to follow a turtle named kyle.
(The two procedures below were part of a complex project in which two
basketball players were being moved by two players using key commands.
These turtle clusters were hidden; their purpose was to guide a visible
basketball moving up and down so that when Kyle or Brad was facing to the
left, the basketball would be dribbling up and down on the left side of the
player, and when Kyle or Brad was facing to the right, the basketball would
dribble on the right side of the player; meanwhile the player shape was
changing to move the arms and legs in a dribbling fashion.  Other procedures
not posted below were involved in steering the players with the keys, moving
the basketball up and down, etc.)

I include this example because it demonstrates how the SETPOS and LIST
commands can be combined with other commands involving brackets and
parentheses... the RANDOM command is not involved in this example, but it
could be incorporated after the + and - signs if that served a useful
purpose for you.

to followbrad
t1, setpos list (ask "brad [xcor]) + 35 (ask "brad [ycor]) + 10
t2, setpos list (ask "brad [xcor]) + 35 (ask "brad [ycor]) - 60
t3, setpos list (ask "brad [xcor]) - 35 (ask "brad [ycor]) + 10
t4, setpos list (ask "brad [xcor]) - 35 (ask "brad [ycor]) - 60
end

to followkyle
k1, setpos list (ask "kyle [xcor]) + 35 (ask "kyle [ycor]) + 10
k2, setpos list (ask "kyle [xcor]) + 35 (ask "kyle [ycor]) - 60
k3, setpos list (ask "kyle [xcor]) - 35 (ask "kyle [ycor]) + 10
k4, setpos list (ask "kyle [xcor]) - 35 (ask "kyle [ycor]) - 60
end

* * *

Wendy Petti
MicroWorlds in Action
http://mia.openworldlearning.org


> -----Original Message-----
> From: mwforum-admin@lists.mathcats.com
> [mailto:mwforum-admin@lists.mathcats.com]On Behalf Of Daniel Ajoy
> Sent: Sunday, February 08, 2004 4:44 PM
> To: mwforum@lists.mathcats.com
> Subject: Re: [MWForum]random numbers, filling
>
>
> On 8 Feb 2004 at 11:23, Karen Randall wrote:
>
> > Greetings,
>
> Hi Karen,
>
>
> > Would one of you be kind enough to comment on our class conclusions
> > about a couple small programming problems?  Is there a more elegant
> > way around them?
> >
> > 1. The kids want to have random number inputs to setting turtle
> > coordinate locations.  We've been able to do this using setx and sety
> > separately, but not setpos followed by something such as [random 20
> > random 20].  Is this correct?  Can you help us understand why one
> > works but not the other?
>
> That is because [random 20 random 20] is a list with four items: one
> word (random), one number (20), one word and one number.
>
> show first [random 20 random 20]
> random
>
> show item 1 [random 20 random 20]
> random
>
> show item 2 [random 20 random 20]
> 20
>
> show item 3 [random 20 random 20]
> random
>
> show item 4 [random 20 random 20]
> 20
>
> What you need is a list of two numbers, like:
>
> show list random 20 random 20
> 1 17
>
> show list random 20 random 20
> 3 19
>
> show list random 20 random 20
> 18 14
>
> "random 20" is a number
> "random 20" is a another number
> "list" takes both numbers and makes a list with them
>
>
> > 2.  When trying to fill shapes, we're adding the extra commands to
> > get the turtle inside the shape and off the line it drew getting
> > there so the fill command works, and then sometimes there is still
> > something not cooperating.  Lifting the pen before moving the turtle
> > inside the shape helps a little, but is there an easier way to get
> > solid color shapes (via the turtle and not the graphics tools)?
>
> Here is a general procedure to draw a filled square,
> everything cooperates:
>
> to square :side :color
> setc :color
> repeat 4 [fd :side rt 90]
> pu rt 45 fd :side fill bk :side rt 45 pd
> end
>
>
> Daniel
> OpenWorld Learning
>
> _______________________________________________
> MWForum mailing list
> MWForum@lists.mathcats.com
> http://lists.mathcats.com/mailman/listinfo/mwforum
> Attachments archived at:
> http://www.mathcats.com/mwforum/attachments.html
> To unsubscribe or for administrative questions contact
> mailto:mwforum-admin@lists.mathcats.com
>