In this chapter, we begin our overview of objects used to build the Small Basic programs. For each object, we summarize the properties, methods and events. Then, we build several example programs illustrating use of the object. We begin with the Small Basic Program object.
The Program object (or more properly class) helps with program execution. We use it identify what folder your program is saved in, implement delays and stop the program.
Program Properties:
Directory Gets the executing program's directory.
Program Methods:
Delay(milliseconds) Delays program execution by the specified amount of milliseconds.
End() Ends the program.
This chapter is adapted from the book The Developer’s Reference Guide To Microsoft Small Basic by Philip Conrod and Lou Tylee.
To purchase this book in its entirety, please see the Computer Science For Kids web site.
Write a program that displays the directory (folder) your program is stored in.
Small Basic Code:
' Guide to Small Basic, Example 3-1TextWindow.Title = "Example 3-1"TextWindow.WriteLine("Directory: " + Program.Directory)
Saved as Example 3-1 in Guide to Small Basic\Programs\Chapter 3 folder.
Save and Run the program. The program directory will be written in the text window:
Your directory will be different, assuming you saved your program in a folder of a different name.
Write a program that writes a line, delays a second, writes another line, then delays two seconds, before ending.
' Guide to Small Basic, Example 3-2TextWindow.Title = "Example 3-2"TextWindow.WriteLine("Line 1")Program.Delay(1000)TextWindow.WriteLine("Line 2")Program.Delay(2000)
Saved as Example 3-2 in Guide to Small Basic\Programs\Chapter 3 folder.
Save and Run the program. In the text window, you will see Line 1 display, a delay of 1 second (1000 milliseconds), Line 2 display, then a 2 second delay before the program ends:
In the short text window programs we have written, they end with this statement:
Press any key to continue…
This allows us to see the contents of the window before the program closes.
If you end a program with:
Program.End()
The “Press any key to continue…” statement will not be seen and the text window will close.
Repeat Example 3-2, but add an End statement. That is, write a program that writes a line, delays a second, writes another line, then delays two seconds, before ending with an End statement.
' Guide to Small Basic, Example 3-3TextWindow.Title = "Example 3-3"TextWindow.WriteLine("Line 1")Program.Delay(1000)TextWindow.WriteLine("Line 2")Program.Delay(2000)Program.End()
Saved as Example 3-3 in Guide to Small Basic\Programs\Chapter 3 folder.
Save and Run the program. In the text window, you will see Line 1 display, a delay of 1 second (1000 milliseconds), Line 2 display, then a 2 second delay, then the text window will disappear.
After completing this chapter, you should understand:
Next, we look in more detail at the TextWindow object we have been using.
Stuff for text methods
To determine the number of characters in (or length of) a string variable, we use the GetLength method. Using MyString as example:
MyString = "Small Basic is fun!"LenString = Text.GetLength(MyString)
LenString will have a value of 19. Characters in the string variable start at index 1 and end at 19.
MyString = "Small Basic is fun!"SubString = Text.GetSubText(MyString, 2, 6)
The SubString variable is equal to “mall B” Notice you can use this to extract from 1 to as many characters as you wish.
MyString = "Small Basic is fun!"LeftString = Text.GetSubText(MyString, 1, 3)
The LeftString variable is equal to “Sma”
MyString = "Small Basic is fun!"RightString = Text.GetSubTextToEnd(MyString, 13)
The RightString variable is equal to “s fun!”
MyString = "Read About Small Basic in 2010!”A = Text.ConvertToUpperCase(MyString)B = Text.ConvertToLowerCase(MyString)
The first conversion using ConvertToUpperCase will result in:
A = “READ ABOUT SMALL BASIC IN 2010!”
And the second conversion using ConvertToLowerCase will yield:
B = “read about small basic in 2010!”
The coordinate system used by the graphics window is:
The window is Width pixels wide and Height pixels high. We use two values (coordinates) to identify a single pixel in the window. The x (horizontal) coordinate increases from left to right, starting at 0. The y (vertical) coordinate increases from top to bottom, also starting at 0. Points in the region are referred to by the two coordinates enclosed in parentheses, or (x, y).
GraphicsWindow.PenColor = ColorGraphicsWindow.PenWidth = Width
where Color is the color your pen will draw in and Width is the integer width (a value of 1 by default) of the line (in pixels) drawn. This pen will draw a solid line. To specify a color, you use a color name like “Red”, “White” or “Blue”. Appendix I lists the multitude of colors available with Small Basic.
The Small Basic DrawLine method is used to connect two points with a straight-line segment. If we wish to connect the point (x1, y1) with (x2, y2), the statement is:
GraphicsWindow.DrawLine(x1, y1, x2, y2)
The line will draw in the current pen Color and Width. Example that draws a blue line of width 1:
GraphicsWindow.PenColor = "Blue"GraphicsWindow.PenWidth = 1GraphicsWindowGraphicsWindow.DrawLine(20, 50, 380, 280)
GraphicsWindow.DrawRectangle(x, y, width, height)
The rectangle will draw with the current pen. To draw a blue rectangle (pen width 2) with the upper left corner at (20, 50), width 150 and height 100, use this code:
GraphicsWindow.PenColor = "Blue"GraphicsWindow.PenWidth = 2GraphicsWindow.DrawRectangle(20, 50, 150, 100)
The Small Basic DrawEllipse method is used to draw an ellipse. To draw an ellipse, we specify the upper left hand corner’s coordinate (x, y) and the width and height of the ellipse. To draw such an ellipse in the graphics window:
GraphicsWindow.DrawEllipse(x, y, width, height)
The ellipse will draw with the current pen. To draw a green ellipse (pen width 3) with the upper left corner at (20, 50), width 150 and height 100, use this code:
GraphicsWindow.PenColor = "Green"GraphicsWindow.PenWidth = 3GraphicsWindow.DrawEllipse(20, 50, 150, 100)
GraphicsWindow.BrushColor = Color
A brush is ‘solid’ – filling areas completely with the specified color
To add text information to the graphics window, we use the Small Basic DrawText method – yes, text is “drawn” to the window. The DrawText method is:
GraphicsWindow.DrawText(x, y, text)
In this statement, text represents the string to print in the window and the point (x, y) is where the string will be located. The string will draw in the graphics window using the current brush color using the default font. Note this method uses the brush color, not the pen color – text is truly drawn like other graphics objects.
GraphicsWindow.BrushColor = "Blue"GraphicsWindow.DrawText(40, 100, "Isn't Small Basic fun?")
This puts the line “Isn’t Small Basic fun?” at (40, 100) in the graphics window. The text will be blue in color. By setting the (x, y) point, you can left or right justify the text, or center it horizontally and/or vertically by knowing the window dimensions. The font size can be changed by setting the FontSize property and FontBold determines if the font is bold or not.
To create a rectangular shape (MyRectangle) that is W pixels wide and Hpixels high, use the AddRectangle method:
MyRectangle = Shapes.AddRectangle(W, H)
This will create a ‘bordered’ rectangle. The current pen color and pen width establishes the rectangle’s border color, while the current brush color establishes the fill color. By default, it will be put in the upper left corner of the graphics window.
Analogously, to create an elliptical shape (MyEllipse) that is W pixels wide and H pixels high, use the AddEllipse method:
MyEllipse = Shapes.AddEllipse(W, H)
This will create a ‘bordered’ ellipse. The current pen color and pen width establishes the ellipse border color, while the current brush color establishes the fill color. By default, it will be put in the upper left corner of the graphics window.
To create a shape with an image, we need two steps. First, we must load the image, then create the shape. Assume you have jpg image file (a digital photo) named MyImage.jpg. (You can use other image files types too). The file with this image must be in the same folder as your Small Basic program. The image is loaded using the LoadImage method of the ImageList object:
MyImage = ImageList.LoadImage(Program.Directory + "\MyImage.jpg")
Then the image shape (MyImageShape) is created using:
MyImageShape = Shapes.AddImage(MyImage)
The shape will be placed in the upper left corner of the graphics window.
Moving Shapes objects in a graphics window is easy to do. It is a simple two step process: use some rule to determine a new position, then redraw it in this new position using the Shapes object Move method. If you have a shape object named MyShape and you want to move it to (NewX, NewY), the code is:
Shapes.Move(MyShape, NewX, NewY)
This code will ‘erase’ MyShape at its current position, then ‘redraw’ it at the newly specified position. Successive transfers (or moves) gives the impression of motion, or animation.
We will see many examples of using Shapesobjects in the game programs we build. We will provide the image files for any Shapes requiring images.
Small Basic offers a rich assortment of built-in methods that compute or provide various quantities. The general form of a method is:
ReturnedValue = ObjectName.MethodName(Arguments)
where Arguments represents a comma-delimited list of information needed by MethodName to perform its computation. Once the arguments are supplied to the method it returns a value (ReturnedValue) for use in an application.
Some methods do not return a value, but only perform a task. To use these methods, just type:
ObjectName.MethodName(Arguments)
Each of the Small Basic math functions comes from the Math class. All this means is that each method name must be preceded by Math. (say Math-dot) to work properly. The methods and the returned values are:
Examples:
Math.Abs(-5.4) returns the absolute value of –5.4 (returns 5.4)Math.Cos(2.3) returns the cosine of an angle of 2.3 radiansMath.Max(7, 10) returns the larger of the two numbers (returns 10)Math.Power(2, 4) returns 2 raised to the fourth power (16)Math.SquareRoot(4.5) returns the square root of 4.5
Whenever you need a random whole number (integer) value, use this method:
Math.GetRandomNumber(Limit)
This statement generates a random value that is between 1 and Limit. For example, the method:
Math.GetRandomNumber(5)
will generate random numbers from 1 to 5. The possible values will be 1, 2, 3, 4 and 5.
A roll of a die can produce a number from 1 to 6. To use GetRandomNumber to roll a die, we would write:
DieNumber = Math.GetRandomNumber(6)
For a deck of cards, the random integers would range from 1 to 52 since there are 52 cards in a standard playing deck. Code to do this:
CardNumber = Math.GetRandomNumber(52)
If we want a number between -100 and 100, we would use:
YourNumber = 101 -Math.GetRandomNumber(201)
Next Chapter > >
© Copyright 2010 By BibleByte Books. All Rights Reserved. BibleByte Books, the BibleByte Books Logo, Computer Science For Kids, the Computer Science For Kids logo, and related trade dress are trademarks or registered trademarks of BibleByte Books.
Ed Price - MSFT edited Revision 3. Comment: Link; tags