Answered by:
Comment about meaning of "beginners" and help with program
Question

I AM a beginner, unlike most of those who seem to post here. I am wondering when Small Basic is actually going to be released to the general public and ACTUAL beginners, i. e. those who have absolutely no or very little programming experience such as myself. I know this forum is a MSDN forum, so it caters to those who are probably experienced and professional programmers. And it seems that is whom primarily posts here.
A reference manual, beyond the getting started guide and the Intellisence guide, that is geared towards actual an BEGINNER:: one that begins something ; especially : an inexperienced person
and that gives definitions and explanations of how all aspects of the language works needs to be published. This should be your priority before creating a reference about APIs and other such things, geared toward highly experienced programmers for them to tweak the language. So far there is no explanation how to use stacks, arrays, and other aspects of the program.
For example, in your FAQ. The first three entries are actually speaking to begginners. Then you start discussing the relationship of Small Basic to other Basics, Small Basics, VB.net, imperitives, types, libraries, KPLs etc. that the begginner has never heard of. Your
FAQ is geared toward the developers not the begginners.
So I ask you. Is this really going to be for actual BEGGINNERS, (or the perjoritive "newbie" if you wish) or just something for you developers to mess around with?
Now that I have that rant off of my chest can I have some help with my first little program? It has the turtle wander randomly around the screen. But I want it to stay within the confines of the visible screen and not wander off screen. Is there some way to make this happen?
GraphicsWindow.Width = 1300
GraphicsWindow.Height = 700
Turtle.X = 675
Turtle.Y = 350
Turtle.Speed = 10
begin:
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Blue"
Turtle.Move(f)
If r >= 90 then
neg()
EndIf
Goto begin
Sub neg
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
r = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Red"
Turtle.Move(r)
EndSub
 Edited by pz scranton Friday, January 30, 2009 1:04 AM
Monday, December 22, 2008 9:06 PM
Answers

pz_scranton,
I'm happy to see that as a beginner you actually have picked up Small Basic and were able to write your first program. That is definitely a great success for us.
You're absolutely right about providing more beginner focused material. What you ask for requires a lot of effort for just one team inside Microsoft to put together. That's why we have such community based resources, where likeminded community members hang out and help others. The point of the blog, forums, wiki, etc. is to generate and capture the kind of data that people like you can use.
One of the benefits of having both beginners and experienced users participate in a forum like this is so that the experienced members would help the beginners. A ton of people have spent countless hours answering questions and keeping a thriving community here.
That said, we will be working on more material as time goes.
Here's a version of your program that brings the turtle back to bounds when it leaves the screen bounds.GraphicsWindow.Width = 1300 GraphicsWindow.Height = 700 Turtle.X = 675 Turtle.Y = 350 Turtle.Speed = 10 begin: r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Blue"
Turtle.Move(f)
If r >= 90 then
neg()
EndIf
BringBackToBounds() Goto begin Sub neg r = Math.GetRandomNumber(180) Turtle.Turn(r) f = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Red" Turtle.Move(f) EndSub Sub BringBackToBounds If Turtle.X < 0 Then Turtle.Angle = 90 Turtle.Move(40) EndIf If Turtle.X > GraphicsWindow.Width Then Turtle.Angle = 90 Turtle.Move(40) EndIf If Turtle.Y < 0 Then Turtle.Angle = 180 Turtle.Move(40) EndIf If Turtle.Y > GraphicsWindow.Height Then Turtle.Angle = 0 Turtle.Move(40) EndIf EndSub Marked as answer by Vijaye Raji Monday, December 22, 2008 10:08 PM
Monday, December 22, 2008 9:35 PM
All replies

pz_scranton,
I'm happy to see that as a beginner you actually have picked up Small Basic and were able to write your first program. That is definitely a great success for us.
You're absolutely right about providing more beginner focused material. What you ask for requires a lot of effort for just one team inside Microsoft to put together. That's why we have such community based resources, where likeminded community members hang out and help others. The point of the blog, forums, wiki, etc. is to generate and capture the kind of data that people like you can use.
One of the benefits of having both beginners and experienced users participate in a forum like this is so that the experienced members would help the beginners. A ton of people have spent countless hours answering questions and keeping a thriving community here.
That said, we will be working on more material as time goes.
Here's a version of your program that brings the turtle back to bounds when it leaves the screen bounds.GraphicsWindow.Width = 1300 GraphicsWindow.Height = 700 Turtle.X = 675 Turtle.Y = 350 Turtle.Speed = 10 begin: r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Blue"
Turtle.Move(f)
If r >= 90 then
neg()
EndIf
BringBackToBounds() Goto begin Sub neg r = Math.GetRandomNumber(180) Turtle.Turn(r) f = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Red" Turtle.Move(f) EndSub Sub BringBackToBounds If Turtle.X < 0 Then Turtle.Angle = 90 Turtle.Move(40) EndIf If Turtle.X > GraphicsWindow.Width Then Turtle.Angle = 90 Turtle.Move(40) EndIf If Turtle.Y < 0 Then Turtle.Angle = 180 Turtle.Move(40) EndIf If Turtle.Y > GraphicsWindow.Height Then Turtle.Angle = 0 Turtle.Move(40) EndIf EndSub Marked as answer by Vijaye Raji Monday, December 22, 2008 10:08 PM
Monday, December 22, 2008 9:35 PM 
THANKS for the timely help. I appreciate the work you and your team have put into this. Sorry if my rant was a bit much, I just wanted to make sure that some actual begginners voices were heard concerning the next direction of the development of Small Basic.
Monday, December 22, 2008 10:05 PM 
I gotta say that i'm a beginner too, i'm starting reading about C/C++; i'm reading for second time Small Basic starting guide, and i know this is the fastest way to start programming. I learned (as my first programming language) a language called Karel+ from Karel The Robot, but this isn't functional, Small Basic is.
TextWindow.WriteLine("Rafael Valdés")Tuesday, December 23, 2008 1:09 AM 
I was going to post a couple variations on Vijaye's code, but as I was coding them I thought it would be better to challenge pz_scranton to come up with the code to do the same things:
1) When turtle goes over the edge, have it reappear on the other side  like if it goes off the right side of the graphics window, have it appear on the left side, and the other way around. Include the Y axis too (off bottom edge, appear at top edge).
Hint: Turtle.X and Turtle.Y can return the current position of the turtle, but can also set the turtle's position  "Turtle.X = 0" Sets the turtle's X position to 0 (zero).
2) Probably not as useful to your goal of not going over the edge, but still something that can help you learn  when the turtle goes over the edge, bring turtle to the middle of the screen on the axis it went over. So if turtle goes off left or right edge, put turtle's X location at half of the current screen width.
This is kindof the same as the first challenge but you can use "Or" in the IF/Then's as well as using the (simple) math to figure out the center of the specific axis.
A variation of this task would be to bring the turtle to the complete center of the screen  no matter what edge the turtle goes over, figure out the center of the graphics window for X and for Y and place the turtle there.
3) Virjaye's code allows the turtle to go over the edge and then brings it back. Write code to figure out the when next Turtle.Move will end up with the turtle over the edge of the screen, move the turtle to that edge while keeping track of how many movement points were used (based on the random number generated for f), and then bounce the turtle off the edge using up the remaining movement points.
This 3rd challenge is, obviously, much more complicated and will require use of a couple more variables than what is already in the program (the first two challenges I did not have to add any variable).You may solve the problems above any way you like. You may come up with a completely different solution than I did, and that's great. Different people think differently. They attack, and think about, problems in different ways.
...but in the end the exercises will stretch your understanding of Small Basic, and of programming, just a little.If you want any help, let me know.
If other people want to participate, then hold off on your answers until pz_scranton has posted his solution(s).
If you want to just see my examples and learn that way, that would be fine.
When I was learning BASIC back in the 80's, I looked over hundreds of example codes and many times I would find myself saying; "I didn't know you could do that!"
BTW: Virjaye; I like the way you used GraphicsWindow's Width and Height to figure out the edge on the fly  since the graphics window is resizeable the turtle can expand or contract it's boundaries. Also, maybe you could put out some programming challenges, that can be solved in SB, to generate more interest as well as give people a project to work on for those who may not be able to think of what to do with SB once they have it. Edited by Path_drc Friday, January 30, 2009 4:11 PM
Tuesday, December 23, 2008 7:10 PM 
Path_drc
Thanks for the response and the challenges. I'll see if I am up to them. But with the Holidays coming up my time is becoming limited with family etc. As a matter of fact, I need to be leaving soon to meet up with some of them to do last minute shopping.
Happy Holidays All...
pz
Tuesday, December 23, 2008 7:53 PM 
Hey Path_drc
I did not give up, just now getting back. I solved the first one, pretty easy. I havn't tried the other two just because I forgot about them and thought up another variation on this program. Every time the turtle hits the edge of the graphic window, the window moves in, until there is no more window. Kind of like a cheesy action movie where the heros end up in a room with walls that are moving into crush them. Except the turtle never finds a pipe that might be lieing around to stop the walls. I wanted to figure out how to create a rectangle that would do the same thing while the grapics window edge remained stationary but I could not figure that out, specifically how to get the coordinates from the rectangle to create variables. Anyway, I will work on the other two challanges in the next several days when I get a chance.
Here is the code for challange # 1
GraphicsWindow.Width = 1300
GraphicsWindow.Height = 700
Turtle.X = 675
Turtle.Y = 350
Turtle.Speed = 10
begin:
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Blue"
Turtle.Move(f)
If r >= 90 then
neg()
EndIf
GoToOtherSide()
Goto begin
Sub GoToOtherSide <<<< <<<<<<< New code
If Turtle.X < 0 Then
Turtle.X = GraphicsWindow.Width
EndIf
If Turtle.X > GraphicsWindow.Width Then
Turtle.X = 0
EndIf
If Turtle.Y < 0 Then
Turtle.Y = GraphicsWindow.Height
EndIf
If Turtle.Y > GraphicsWindow.Height Then
Turtle.Y = 0
EndIf
EndSub <<<<<<<<<<<< End new code
Now here is my addition; the "CrushTurtle" sub
GraphicsWindow.Width = 1300
GraphicsWindow.Height = 700
Turtle.X = 675
Turtle.Y = 350
Turtle.Speed = 10
begin:
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Blue"
Turtle.Move(f)
If r >= 90 then
neg()
EndIf
BringBackToBounds()
CrushTurtle()
Goto begin
Sub BringBackToBounds
If Turtle.X < 0 Then
Turtle.Angle = 90
Turtle.Move(40)
EndIf
If Turtle.X > GraphicsWindow.Width Then
Turtle.Angle = 90
Turtle.Move(40)
EndIf
If Turtle.Y < 0 Then
Turtle.Angle = 180
Turtle.Move(40)
EndIf
If Turtle.Y > GraphicsWindow.Height Then
Turtle.Angle = 0
Turtle.Move(40)
EndIf
EndSub
Sub CrushTurtle <<<<<<<<<<<<<<<<< New code
If Turtle.X > GraphicsWindow.Width Then
GraphicsWindow.Width = (GraphicsWindow.Width  30)
EndIf
If Turtle.X < 0 Then
GraphicsWindow.Width = (GraphicsWindow.Width  30)
EndIf
If Turtle.Y > GraphicsWindow.Height Then
GraphicsWindow.Height = (GraphicsWindow.Height  10)
EndIf
If Turtle.Y < 0 Then
GraphicsWindow.Height= (GraphicsWindow.Height  10)
EndIf <<<<<<<<<<<<<<<<<<<< End new code
EndSub
Wednesday, December 31, 2008 2:53 AM 
PZ;
Awesome work, especially with coming up with something completely new to try.
I often find myself going one direction with a program and think "...but what if we do this?" or "I wonder if it is possible to do that?"
The change in direction almost never makes the program any better or make it into the main project, but just figuring out how to do it is really cool. At least for me it is. :)Monday, January 19, 2009 5:20 PM 
Ok, I'm finally back.
Here are my answers to challanges #2 and #3 from Path_drc.
GraphicsWindow.Width = 1300 GraphicsWindow.Height = 700 Turtle.X = 675 Turtle.Y = 350 Turtle.Speed = 10 begin: Turtle.Speed = 10 r = Math.GetRandomNumber(180) Turtle.Turn(r) f = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Blue" Turtle.Move(f) If r >= 90 then neg() EndIf TurtleMiddle() Goto begin Sub neg r = Math.GetRandomNumber(180) Turtle.Turn(r) r = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Red" Turtle.Move(r) EndSub Sub TurtleMiddle If Turtle.X > GraphicsWindow.Width Or Turtle.X < 0 Or Turtle.Y > GraphicsWindow.Height Or Turtle.Y < 0 Then Turtle.X = (GraphicsWindow.Width/2) Turtle.Y = (GraphicsWindow.Height/2) Program.Delay(1000) EndIf EndSub But # 3 ... My solution does not work. It works sometimes. It works, better, if I substitute a static number such as 100 in place of Turtle.Move(f  a). But it still does not work as it should and I don't understand why. If someone could point out the error of my ways I would appreciate it.
GraphicsWindow.Width = 1300 GraphicsWindow.Height = 700 Turtle.X = 675 Turtle.Y = 350 Turtle.Speed = 10 begin: Turtle.Speed = 10 r = Math.GetRandomNumber(180) Turtle.Turn(r) f = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Blue" Turtle.Move(f) If r >= 90 then neg() EndIf Edge3() Goto begin Sub neg r = Math.GetRandomNumber(180) Turtle.Turn(r) r = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Red" Turtle.Move(r) EndSub Sub Edge3 If Turtle.X <= 100 And Turtle.X >= 0 And f > Turtle.X Then a = Turtle.X Turtle.X = 0 Turtle.Angle = 90 GraphicsWindow.PenColor = "Green" Turtle.Move(f  a) EndIf If Turtle.X >= 1200 And Turtle.X <= GraphicsWindow.Width And f > Turtle.X Then a = Turtle.X Turtle.X = GraphicsWindow.Width Turtle.Angle = 90 Turtle.Move(f  a) EndIf If Turtle.Y <= 100 And Turtle.Y >= 0 And f > Turtle.Y Then a = Turtle.Y Turtle.Y = 0 Turtle.Angle = 180 Turtle.Move(f  a) EndIf If Turtle.Y >= 600 And Turtle.Y <= GraphicsWindow.Height And f > Turtle.Y Then a = Turtle.Y Turtle.Y = GraphicsWindow.Height Turtle.Angle = 0 Turtle.Move(f  a) EndIf EndSub Actually, like this, it does not work at all, the turtle gets lost offscreen. As I said, if a static number such as 100 is substituted for (f  a), it works better for some reason.
Wednesday, January 28, 2009 2:22 AM 
Hi pz_scranton.
Here is a solution for #3GraphicsWindow.Width = 1300 GraphicsWindow.Height = 700 Turtle.X = 675 Turtle.Y = 350 Turtle.Speed = 10 begin: r = Math.GetRandomNumber(180) Turtle.Turn(r) f = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Blue" MakeMove() If r >= 90 then neg() EndIf Goto begin Sub neg r = Math.GetRandomNumber(180) Turtle.Turn(r) f = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Red" MakeMove() EndSub Sub MakeMove cos = Math.Cos((90Turtle.Angle)*Math.Pi/180) sin = Math.Sin((90Turtle.Angle)*Math.Pi/180) ' New X and Y if the move is of length f ' Just simple maths nx = Turtle.X + f * cos ny = Turtle.Y  f * sin nf = f ' If the turtle touch a wall I'll make it bounce on it ' I need to compute the distance to the wall ' And the bounce angle If nx > GraphicsWindow.Width Then nf = (GraphicsWindow.WidthTurtle.X)/cos na =  Turtle.Angle EndIf If ny > GraphicsWindow.Height Then nf = (GraphicsWindow.HeightTurtle.Y)/sin na = 180  Turtle.Angle EndIf If nx < 0 Then nf = (0Turtle.X)/cos na =  Turtle.Angle EndIf If ny < 0 Then nf = (0Turtle.Y)/sin na = 180  Turtle.Angle EndIf ' Move to the wall Turtle.Move(nf) If nf < f Then ' Bounce Turtle.Angle = na Turtle.Move(fnf) EndIF EndSub The solution is not difficult, it is just simple math.
Each time we want to make a move a check is make and if the turtle is goind off screen we made it bounce.
The main difficulty is that turtle angle and mathematical ones are not the same ( one is clockwise and vertical = 0 the other not and vertical = Pi/2 ).
Once we have the new position we can check if it is off screen the 'nf' variable is just obtain by solving an equation with one unknow variable.
Ask me if you need more explanations.
Wednesday, January 28, 2009 5:19 PM 
Hi Loic, thanks for providing this answer.
As I have said at the start of this thread I am a beginner. I presume most here are professional programmers, and I appreciate the helpfull attitude many of you show. I am also a college dropout. I had "math  phobia", perhaps a euphemism for saying one is dumb at math. Therefore, it is not simple math(s) to me. Never took triginometry. But it does solves the problem.I think I understand the logic of the code, minus the trig part. I'll have to bite the bullet some day and work on my math, or "maths", as the rest of the world says it.
I may be slow at this and cannot always devote time to it. But I will plod along at my own pace because I have always wanted to learn programming. I am also trying to learn Processing.
Path_drc, if you are still there, could you provide your answer to this? I wonder if there is one that would not involve trig.
Friday, January 30, 2009 1:01 AM 
Hi.
If you want to exactly rebounce on the walls i.e. if "f" is equal to 100 but you will touch the wall after moving from 75 then my code will make you bounce from exactly 25 with the "correct" angle (the one you would have in practice with a ball and a perfect wall, like if your are playing billiards). To do so you have to know "what is my distance to the wall ?" and only trigonometry will give you an exact answer.
But you can use a trick, as often :)
First, do you understand how is computing the variable na ?
Just draw a circle with the angle 0 at the top, 90 at the right, 180 at the bottom and 270 (or 90 it is the same) at the right, draw also your input angle and your output angle whether you want to rebounce on the X or the Y axis.
You will see that for X we need to take the opposit angle and for Y we need to take 180 minus the angle.
Once you have that instead of computing the exact "nf" value just do something like (this is not a correct code it just gives you the structure) :
nf = 0
While "I'm in the screen" And nf < f
Turtle.Move(1)
nf = nf + 1
EndWhile
if nf < f Then
"Compute na...."
Turtle.Angle = na
Turtle.Move(fnf)
EndIfWe move forward as long as we can and then we turn of the correct angle to rebounce.
Visually there should be no difference between the two codes, this one will just be much slower : the Turtle will move 100 times slower
I'll not give the full code immediately so you can try to do it yourself.
But otherwise I think that trigonometry and equation sloving (1st et 2nd degre i.e. of for example x + 2 = 5  x and x*x + 3x  10 = 0) is the kind of maths you use the most often in programming especially when you are doing some graphics. If you have time you should definitely try to learn it, with a good book.
One thing : my last code will actually not work is the Turtle is in a corner i.e. if in a single move she will need to rebound on 2 walls. It wouldn't be much more complicated to deal with this case but the code would be longer, you can try if you want.
Friday, January 30, 2009 6:27 AM 
Sorry PZ, I have been sick for a while (still am) so haven't been online.
You're right. #2 was easy and you did a great job. You used less lines then I did with my solution.
I don't see where your number 3 figures out the angle at which the edge was hit when it went over that edge.
I have tried a few ideas but am falling short for some reason that my halfsick and halfmedicated brain can't understand.I thought I had coded out my soltion to question 3, but I can't find it any where.
My solutions are not trig based, at least I don't think they are, I just try different basic math techniqes until I come up with a formula that consitantly gives me the correct results.
When I am feeling better I will present my answer(s)
THNX!Saturday, January 31, 2009 1:45 AM 
Vijaye,
I have run your code on this question and noticed some odd effects. Two of these are triggered when the turtle goes outside the window and comes back in again. An image of the turtle appears at its start point as well as its current position and a line joins the present turtle position to the top left corner. Both effects are temporary.
Another peculiarity is that the turtle tends to proceed around the edge of the window in an anticlockwise direction. Is this a problem with the random number generator?Sunday, February 1, 2009 9:05 AM 
The break in synchronization between the turtle and the line is a bug. The two animations tend to go out of synch often. I'll probably fix this for v0.4 (based on priority).
I'm not aware of any problems with the random number generator. Can you copy and paste the program that is exhibiting this behavior?Tuesday, February 3, 2009 4:46 PM 
Vijaye,
The code in question is your own that you posted in reply to a question from pz_scranton at the start of this thread (22/12/08) about stopping the turtle moving outside the bounds of the window but i'll include it here to save you looking for it.
GraphicsWindow.Width = 1300
GraphicsWindow.Height = 700
Turtle.X = 675
Turtle.Y = 350
Turtle.Speed = 10begin:
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Blue"
Turtle.Move(f)
If r >= 90 then
neg()
EndIf
BringBackToBounds()
Goto beginSub neg
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Red"
Turtle.Move(f)
EndSub
Sub BringBackToBounds
If Turtle.X < 0 Then
Turtle.Angle = 90
Turtle.Move(40)
EndIf
If Turtle.X > GraphicsWindow.Width Then
Turtle.Angle = 90
Turtle.Move(40)
EndIf
If Turtle.Y < 0 Then
Turtle.Angle = 180
Turtle.Move(40)
EndIf
If Turtle.Y > GraphicsWindow.Height Then
Turtle.Angle = 0
Turtle.Move(40)
EndIf
EndSub
I have just run the program ten times and each time the turtle tended to proceed in an anticlockwise direction when hitting the edge. No bias was observed when the turtle was away from the edge.Tuesday, February 3, 2009 7:19 PM 
Ahh... I did notice the anticlockwise traversal around the edges. Nice catch Stendec.
Here's what I think is happening.
The problem is that the positive turn always executes before the negative turn.
Say, when the turtle goes past the left edge (turtle.x < 0), we reset the angle of the turtle to 90 and send it back in. The turtle is now facing east. Now, we get a random number that's less than 180 and turn the turtle. That would leave the turtle facing downwards at the left most edge of the screen  biasing it anticlockwise.
We can take another edge as an example  say, the bottom edge (y > GraphicsWindow.Height). We reset the angle to 0 and send it back up. Again, the random number generator is going to pick a number less than 180 and turn the turtle. That would leave the turtle facing right  again biasing it anticlockwise.
To fix this bias, we can change the input to random number generator.
r = Math.GetRandomMunber(360)
and then modify the check as:
if r >= 180 then
Hope that helps.Wednesday, February 4, 2009 1:10 AM 
Vijaye,
A simpler solution is to orientate the turtle to face the left with respect to each edge of the window so that the arc of random angles (0  180 degrees) always falls in side the window. I have added Turtle.Angle commands after the Turtle.Move(40) commands in BringBackToBounds to do this.
GraphicsWindow.Width = 1300
GraphicsWindow.Height = 700
Turtle.X = 675
Turtle.Y = 350
Turtle.Speed = 10begin:
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Blue"
Turtle.Move(f)
If r >= 90 then
neg()
EndIf
BringBackToBounds()
Goto beginSub neg
r = Math.GetRandomNumber(180)
Turtle.Turn(r)
f = Math.GetRandomNumber(100)
GraphicsWindow.PenColor = "Red"
Turtle.Move(f)
EndSub
Sub BringBackToBounds
If Turtle.X < 0 Then
Turtle.Angle = 90
Turtle.Move(40)
Turtle.Angle = 0 'New line
EndIf
If Turtle.X > GraphicsWindow.Width Then
Turtle.Angle = 90
Turtle.Move(40)
Turtle.Angle = 180 'New line
EndIf
If Turtle.Y < 0 Then
Turtle.Angle = 180
Turtle.Move(40)
Turtle.Angle = 90 'New line
EndIf
If Turtle.Y > GraphicsWindow.Height Then
Turtle.Angle = 0
Turtle.Move(40)
Turtle.Angle = 90 'New line
EndIf
EndSub
Wednesday, February 4, 2009 1:10 PM 
Good point. Resetting the Turtle after getting into bounds, works great.Wednesday, February 4, 2009 4:52 PM

Vijaye,
Actually, if we wanted to remove bias from the movement the random angle should ber = Math.GetRandomNumber(181)  1
as the random numbers start at 1, but I think that is a good place to leave it.
Wednesday, February 4, 2009 7:41 PM 
Hi Loic and everyone else,
Sorry I've dragged this out. I worked this out last week and just now got around to posting it. Here is a nontrig solution that seems to work. It uses Loic's code suggestions, and Vijaye's "BringBacktoBounsd" subroutine with Stendec's code improvements. Of course it is slow, as Loic said it would be. Also, I am noticing what I am calling the "lighting bolt effect" more often, which I believe may be what Stendec was referring to when he wrote:
"Vijaye,
I have run your code on this question and noticed some odd effects. Two of these are triggered when the turtle goes outside the window and comes back in again. An image of the turtle appears at its start point as well as its current position and a line joins the present turtle position to the top left corner."
However, the effect I have noticed, similar to what Stendec describes, does not seem to occur when the turtle goes off screen, but randomly; a diagonal line connects the turtle, at whatever position it is at on the screen, with the upper left hand corner of the screen, postion 0,0, and instantly disappears.
Loic, if you could, if you even are bothering to read this after all this time, I would appreicate it if you could post your solution (and you too Path_drc, if you ever find it and have the time.)
I am tired of this and am finished with it after this post.GraphicsWindow.Width = 1300 GraphicsWindow.Height = 700 Turtle.X = 675 Turtle.Y = 350 Turtle.Speed = 10 begin: r = Math.GetRandomNumber(180) Turtle.Turn(r) f = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Blue" screenbounce() Turtle.Move(f) If r >= 90 then neg() EndIf BringBackToBounds2() Goto begin Sub neg r = Math.GetRandomNumber(180) Turtle.Turn(r) r = Math.GetRandomNumber(100) GraphicsWindow.PenColor = "Red" screenbounce() Turtle.Move(r) EndSub Sub BringBackToBounds2 If Turtle.X < 0 Then Turtle.Angle = 90 Turtle.Move(40) Turtle.Angle = 0 'New line na =  Turtle.Angle EndIf If Turtle.X > GraphicsWindow.Width Then Turtle.Angle = 90 Turtle.Move(40) Turtle.Angle = 180 'New line na =  Turtle.Angle EndIf If Turtle.Y < 0 Then Turtle.Angle = 180 Turtle.Move(40) Turtle.Angle = 90 'New line na = 180  Turtle.Angle EndIf If Turtle.Y > GraphicsWindow.Height Then Turtle.Angle = 0 Turtle.Move(40) Turtle.Angle = 90 'New line na = 180  Turtle.Angle EndIf EndSub Sub screenbounce nf = 0 While Turtle.X > 0 And Turtle.X < GraphicsWindow.Width And Turtle.Y > 0 And Turtle.Y < GraphicsWindow.Height And nf < f Turtle.Move(1) nf = nf + 1 EndWhile If nf < f Then Turtle.Angle = na Turtle.Move(f  nf) EndIf Endsub
Wednesday, February 11, 2009 3:56 AM 
I meant to make one comment on my code but forgot. Isn't it somewhat clunky in that there is a redundancy in checking for the end of screen twice in two separate subroutines? But it is the best I could come up with now and as I said, I am tired of messing with it.Wednesday, February 11, 2009 4:01 AM

My challenge #3 solution
Program ID:
SZP009
http://smallbasic.com/program/?SZP009
PZ;
Wow. I thought bouncing back the turle would have been a lot easier than it was  that is without using any kind of advanced math.
Sorry I threw such a big challenge. I promise I didn't know how big of a challenge this was.
I knew I had to do two things  get the turtle back to the point it went off the screen and figure out the deflected angle.
Deflected Angle:
I was hitting my head against the wall trying to figure out the angle.
It wasn't until I inserted code to print the different variables that I discovered the Turtle.Angle amount is not limited to 360 dgrees.
If the Turtle is at zero degrees and you Turtle.Turn(90) four times, the Turtle.Angle is then at 360  but if you execute one more Turtle.Turn(90), then the Turtle.Angle will equal 450, not 90.
(the turtle angle will also go into negative numbers , with 45 degrees on the left side of the degrees circle)
Got help in the forum to convert Turtle.Angle into a 360 degrees number
Math.Remainder(Turtle.Angle,360)
I don't know if my deflected angle is realworld accurate, but it just made sense to me. If the turtle goes off the edge at the top of the screen at a 45 degree angle, then it should bounce off the top at a 135 degrees.
If it hits at 1 degree, it should deflect at 179 degrees.
Again, i don't know if that is real world or not.Exit Point:
This was more difficult without using the higher math.
I first decided to turn around the turtle angle and move the amount the turtle was over the edge.
Then I found out you can have the turtle perform a negative move, which makes the turtle move backwards.
Well, I first confused my X/Y axis and width/heigth.
I corrected that, but my negative move amount was not getting the turtle back onto the screen.
I Figure it has to do with the angle. A move of 10 does not mean x + 10 and Y + 10. That would be a 135 degree angle.
X + 0 and Y + 10 would be a 180 degree angle.
There has to be some kind of math that moves the turtle forward at the angles between those two, right?
So I cheated. I call it cheating but it's just a workaround.
I looped a negative 1 movement and tested to see if the X/Y coordinates are back on the screen after each movement.
Also, with each 1 movement, I kept count of how many times the turtle moved. That is the amount of forward movement I used for the deflection.
Truth is that it is clumbsy and slow, and the place the turtle stops when it gets back onto the screen is not exact, but for the most part, it works.Friday, February 20, 2009 3:53 AM 
Path_drc,
It occured to me that a way to bounce the turtle of the walls without using trigonometry would be to use components. This way the x and y movements are handled separately. There is a Turtle.MoveTo() command which can be used to move to an x,y coordinate position with no angles involved.
Consider a turtle position near the right hand wall. Let's say the turtle position is 390,100 (x=390,y=100) and the GraphicsWindow.Width = 400. If the turtle is told to move 30 to the right and 20 up, then it can only move to GraphicsWindow.Width in the x direction. The remainder of the x movement (390+30GraphicsWindow.Width) is reflected back. The y movement is split in the same proportion as the x movement.
I'm too lazy to type a full explanation of how the program works, but you may want to work it out for yourself. If you have any questions about a particular part of it, please ask.
The listing may be imported as RTB151Friday, February 20, 2009 8:51 PM 
OK Stendec, I see what you did.
You are randomizing the X and Y to move to. The turtle movements look close to what PZ Scranton's original program draws, though I don't know if it does exactly what PZ wants to do.
Awesome way to think outside the box.Friday, February 27, 2009 5:44 PM 
Thanks Path_drc,
I didn't pay too much attention to the original program as I was trying to illustrate a principle. It is often easier to analyse problems in two dimensions if the dimensions can be treated independently. (The x movement does not change the y movement and vice versa).
The turtle is reflected at the wall and the remainder of the move is then reanalysed. This allows the code to cope with corners where there may be more than one reflection. This process is repeated until the calculated move is used up. Another position is then calculated to move to.
If you examine the code closely, there is a major difference in calculating the movement of the turtle. The original program calculated a random distance and a random angle. My program calculates a random x and a random y. The difference is that, in the original code a random point is selected within a circle of a maximum radius, whereas in my code a random point is selected within a square of a maximum size. The difference may be regarded as academic, but in order to stick to the original idea, a conversion would be necessary between polar and rectangular coordinates. The simplest way to do this would be to calculate the random distance and random angle as originally and then calculate x as distance * cos(angle) and y as distance * sin(angle), but this would be reintroducing trigonometry which I was trying to avoid! If anyone knows a way of calculating a random point within a circle without using angles or trigonometry then this blemish could be eradicated.
 Edited by Stendec Sunday, March 1, 2009 10:31 AM Spelling corrections
Friday, February 27, 2009 6:20 PM 
To have written the Small Basic compiler you MUST have de4fined the syntax.
Please publish it, perhaps as a separate file, so we can see what the commands are. The Introduction is almost as useless as a funnel on a submarine.
Among the things which I want to do, but cannt, are to suppress a line feed when I want to print out a mixed line of text and values, for example:
m "metres is "f "feet and "i"Inches"
requiring both extracting the whole number of feet and printing on one line.
There have been several such instances as I have tried to port legacy GWBasic and QBasic programs to Small Basic.Sunday, March 1, 2009 1:22 PM 
To answer your question about suppressing line feeds
TextWindow.Show()
TextWindow.Write("Please enter distance in metres ")
m = TextWindow.ReadNumber()
f = Math.Floor(m * 100 / 2.54 / 12)
i = Math.Remainder( m * 100 / 2.54, 12) * 100
i = Math.Floor(i) / 100 ' inches limited here to 2 decimal places.
'In one line
TextWindow.WriteLine(m + " metres is " + f + " feet " + i + " inches")'Or you could use
TextWindow.Write(m)
TextWindow.Write(" metres is ")
TextWindow.Write(f)
TextWindow.Write(" feet ")
TextWindow.Write(i)
TextWindow.WriteLine(" inches")'Or somewhere in between the two extremes.
As for the general question about the syntax, see Loyola IT's post in the thread "What goes next" from which I reproduce the relevant part here:
" You need to download the Getting Started Guide and go to the wiki and download the API at http://smallbasic.com/smallbasic.com/wiki/Downloads.ashx. Read them."
The Getting Started Guide should be available from the same place you downloaded Small Basic. Proposed as answer by Stendec Sunday, March 1, 2009 2:12 PM
Sunday, March 1, 2009 2:11 PM 
Stendec,
Thank you for your reply. It gave me some constructs that I did not know and also thank you for pointing me at the download.
I did, in fact, know how to "Design" a metric to imperial program but I used that as an example of a multiple output. You might see my books Superprogrammer and Practical Programming...
As a side comment, <TextWindow.WriteLine> takes me back to the old days of Fortran.
Though now longretired, there are some things that I think a program does better than, eg a spreadsheet, and when I upgraded to XP I found that my GWBasic programs would not open in a full screen so then I swapped to QBasic. When I went to Vista I found that even this would not go full screen.
I looked at Visual Basic and, while it may be excellent for what it is designed for, it did not suit me for plain "input  calculate  output" programs. When I saw the reference to Small Basic I had to investigate.
Once I have got a good grasp of the syntax I may well port some of my legacy programs. I have, among others, a Morse Trainer, a Mastermind [the DOS version was sometimes called Bulls & Cows] a magic squares generator and a dice program.
Thank you again.Tuesday, March 3, 2009 10:06 AM