text stringlengths 0 897 |
|---|
At some point, you'll want to create your own modules of handy code that you like to use in multiple programs. You can simply create your library directory with `file.makedir "/usr/lib"`, and put your own scripts there. These will be found by `import` just like the ones in `/sys/lib`. |
## Exploring the Demos |
{i:"`/sys/demo`,`scribble`"} |
Going back to `/sys/demo`, you'll find one called `scribble.ms` that is just a slightly expanded version of the mouse-drawing program you made above. That one is worth studying a bit to see how the mouse is used. |
{i:"`/sys/demo`,`forestFire`"} |
For an example of display layers, you might look at the `forestFire` demo. This sets up layers 1 and 2 both in text mode, and then switches back and forth between them on each step of the simulation. You could also look at the `wumpusTrap` game, which uses several display layers, though two of these are tile and spri... |
{i:"`/sys/demo`,`theMatrix`;`/sys/demo`,`turtleDemo`"} |
If it's `import` you're looking for, you can find that in many of the demos. The `balloons` demo imports both `mathUtil` and `pathUtil` to help move the balloons around the track. The `chars` module we introduced first in this chapter is used in `theMatrix` demo. And of course `turtleDemo`, which you first saw in th... |
A> **Chapter Review** |
A> - You gained insight into Mini Micro's 8 display layers, and how to control them. |
A> - You learned how to use the `mouse` module to read the mouse position and buttons. |
A> - You came to grips with `import`, and explored the library of modules available to you in `/sys/lib/`. |
A> - You have begun to think about what sort of code you might want to put into your own import modules, to make your future programs shorter and simpler. |
{chapterHead: "Day 2: Simple Math and Functions", startingPageNum:15} |
{width: "50%"} |
 |
Q> There’s so many exciting things going on in the computer industry, that if you have an idea, a dream, something that you want to do, then just go for it. |
Q>— Stephanie Shirley (IT entrepreneur and philanthropist) |
A> **Chapter Objectives** |
A> - Learn how to add, subtract, multiply, and divide numbers and strings. |
A> - Learn what *functions* are and how to use them. |
A> - Explore a few of MiniScript's most common built-in functions. |
A> - Appreciate that coding is an open-book exercise; you don't need to remember everything! |
## Doing Math |
In Day 1, you learned how to use `print` to make MiniScript display a number or string to the output area. Except for some minor changes in formatting, what you put in was exactly what you got out -- MiniScript wasn't really doing any work for you. |
Today we'll learn how to put that computer to work! Some people are good at doing math in their heads, but nobody is as good at arithmetic as a computer. How many minutes are in a year? Let's use MiniScript to find out! |
{caption: "Minutes in a year"} |
```miniscript |
print 365 * 24 * 60 |
``` |
The `*` character in MiniScript (and most other programming languages) means multiplication. So the program above multiplies 365 days in a year, times 24 hours in a day, times 60 minutes in an hour. The answer is printed to the output box. |
The multiplication symbol `*` is just one of several *operators* that can be applied to numbers in MiniScript. |
operator |
: a symbol or word used to indicate an operation (such as addition, division, etc.) |
Everyone learns the four basic arithmetic operators in school. MiniScript numbers support the same four, plus two more: power (raise the first number to the second), and modulo (remainder after division). We'll expand on these in a moment. For now, focus on how to write addition, subtraction, multiplication, and div... |
{i:"numbers, operators;operators, numeric"} |
| `+` | addition | |
| `-` | subtraction | |
| `*` | multiplication | |
| `/` | division | |
| `%` | modulo | |
| `^` | power | |
These operators go between the numbers they operate on, just like you always did in school. So, `39 + 3` adds three to 39, just as you would expect. The order of operation is the same as standard arithmetic, too; power is always done first, then multiplication and division (and modulo), and finally addition and subtr... |
{caption: "Order of operations"} |
```miniscript |
print 2 + 3 * 4 |
print (2 + 3) * 4 |
``` |
Line 1 in the above example first multiplies 3 and 4 to get 12, then adds 2 to get 14. This is because multiplication is always done before addition, unless you force it otherwise with parentheses. The second line above does exactly that: first it adds 2 and 3 to get 5, and then multiplies that by 4 to get 20. |
D> Hang in there! We'll be making creating beautiful art and engrossing games before you know it. |
## String Operators |
Yesterday you learned about two types of data: numbers and strings. The mathy stuff above was all about numbers. What about strings? |
It turns out that you can use most of the same operators on strings, too, but they have different effects. This is a type of math you probably did *not* learn in school, but fortunately it's very simple. When you add two strings together, it simply joins them in order into one big string. This operation is technical... |
{i: "string, concatenation"} |
concatenation |
: forming one larger string by joining two smaller strings together |
For example: |
{caption: "String concatenation"} |
```miniscript |
print "How" + "dy" |
``` |
Can you guess what output this produces? Try it and see if you were right! |
You can also subtract two strings. This lops the second string off the end of the first one, if indeed it is found at the end; otherwise it returns the first string as-is. Some examples will help: |
{caption: "String subtraction"} |
```miniscript |
print "Howdy" - "dy" |
print "Alien.png" - ".png" |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.