text
stringlengths
0
897
```miniscript
stampy.update = function()
super.update // do regular update first
if self.overlaps(stampy2) then
self.tint = color.red
else
self.tint = color.white
end if
end function
```
With those changes, run the program again, and guide your stamper around the screen. You should see that whenever it touches the second stamper, it turns red. Note that the argument to `overlaps` can be either a `Bounds` object, or a `Sprite` object; either will work (but passing a `Sprite` is more common).
## Sprites in the Wild
Now that you know how to create, animate, hit-test, and collision-check sprites in Mini Micro, be on the lookout for sprites in the apps you use every day. Any little image that moves, rotates, reacts to clicks or taps, and animates by switching among some series of predefined frames is likely a sprite. You'll see th...
Sprites are one of the most powerful, general, and widely useful concepts in real-time computer graphics. Now that you have this tool in your toolbelt, you will be using it a lot. Keep it sharp!
A> **Chapter Review**
A> - You learned about sprites, and how to create them in Mini Micro.
A> - You animated sprites by changing their position, rotation, or image.
A> - You discovered the `Bounds` class, and used it to test whether a sprite is touching a point or another sprite.
{chapterHead: "Day 23: Fun with Words", startingPageNum:279}
{width: "50%"}
![](Chapter23.svg)
Q> Sometimes it's better to leave something alone, to pause, and that's very true of programming.
Q>— Joyce Wheeler (Executive Director of Technology, LinkedIn)
A> **Chapter Objectives**
A> - Discover Mini Micro's built-in English word list.
A> - Write some code to analyze data and generate random strings of letters.
A> - Lay the foundation for a word game (to be completed tomorrow).
{i:"`/sys/data`"}
Today we're going to do something a bit different. Mini Micro comes with a list of over 64,000 common English words, found in the file `/sys/data/englishWords.txt`. We'll use this to explore how code can be used to answer questions about data that would be very hard to answer in any other way. Then, we'll make a sol...
## Exploring the Words File
Let's begin by using the `view` command in Mini Micro to peek at the file:
```terminal
]view "/sys/data/englishWords.txt"
```
You should see a pageful of words in alphabetical order: *a*, *aah*, *aardvark*, and so on, followed by something like "64637 more". You can press Return a few times to see more pages, but chances are good you'll lose interest before even getting out of the A's. When that happens, press Q or the Escape key to exit `v...
You can see that the structure of this file is quite simple: one word per line. So we can read all the words into a list by doing:
```miniscript
words = file.readLines("/sys/data/englishWords.txt")
```
I wouldn't suggest trying to print `words` at this point (but hey, go ahead and try if you want — it's not going to hurt anything, though it may take a while). But print `words.len` to see how many words there are.
This list of words is a data set, so put on your "data scientist" hat and let's ask some questions about it. We've already seen the first few items, alphabetically. What are the *last* few words? Print out `words[-10:]` to see.
We've also already seen one of the shortest words in the English language, "a". Are there any other 1-letter words? Let's use a simple `for` loop to find out. It's a loop so short that, if you like, you can do it as a one-liner:
```miniscript
for w in words; if w.len==1 then print w; end for
```
In addition to "a", this also prints out "I" (the pronoun) and "O" (as in "O say can you see?"). It also prints out "x", which surprised me when I did this. But finding surprises in data is part of the fun! (And yes, if you look up "x" in many dictionaries, you will find it defined as a word meaning to cross out or ...
Now let's find the longest word in the set. This is slightly bigger `for` loop.
```miniscript
biggest = ""
for w in words
if w.len > biggest.len then biggest = w
end for
biggest
```
This loops over all words, comparing each one to the biggest word found so far. Whenever it finds a new biggest word, it stores that in `biggest`, which we print out at the end. What word did you find? And, how many letters does it have? (No fair counting them by hand — make MiniScript do that for you!)
Next up is a question I actually needed an answer to. I was pondering making a word-search game, where the user would try to make words out of randomly selected letters in a grid, and I wondered what to do with "Q". Usually "Q" in English is followed by a "u", but is that always the case? Maybe there are a bunch of ...
{i:"`/sys/lib`,`stringUtil`"}
```miniscript
import "stringUtil"
for w in words
if w.contains("q") and not w.contains("qu") then print w
end for
```
Notice that we had to import "stringUtil" before doing this `for` loop. That's because `contains` is not a standard MiniScript intrinsic function; it is something added by the `stringUtil` module. (The standard MiniScript solution would be to check whether `w.indexOf("q") != null`, but `contains` is a lot clearer.)
So this loop prints out every word that contains a "q", but does not contain "qu". Exactly three words popped out: "burqa", "burqas", and "qwerty". Out of more than 64 thousand words, that's not too many!
If we also import the "listUtil" module, we'll get some additional handy functions, like the `any` method that returns a random element of a list.
```miniscript
import "listUtil"
for i in range(1,10); print words.any; end for