text
stringlengths
0
897
{caption: "Listing 2 (Selecting random letters).", number-from: 28}
```miniscript
totalFreq = freq.values.sum
randomLetter = function()
i = floor(rnd * totalFreq)
for kv in freq
if i < kv.value then return kv.key
i = i - kv.value
end for
end function
```
Run that, and again nothing happens. But let's test the code out as we consider how it works. Line 28 calculates the total of all the frequency values in our table, i.e., the total number of letters in our word set. `freq.values` would give you a list of all the frequencies, and appending `.sum` adds them up. Enter...
The `randomLetter` function picks a random number `i` from 0 to this total frequency count. Then it iterates over the frequency table (using `kv` to remind us that these are key/value pairs), checking whether `i` is less than the frequency value of each letter. This will of course be more likely for numbers with big ...
D> This is an algorithm known as a *weighted random selection*, and it can be used any time you want to pick from a set of things, but with a different probability or "weight" for each thing.
Enter `randomLetter` at the REPL prompt, and it should print out a letter (or in the case of "Qu", two letters). Repeat it a few times (or maybe even use a `for` loop!), and convince yourself that the more common English letters are appearing more often.
## Creating Letter Tiles
Our game is going to involve a grid of letter tiles, much like the playing pieces in Scrabble. These little tiles need to move around, detect mouse clicks, etc., so we'll make a Sprite subclass to represent them.
D> Careful: we're using "tile" here just to describe the shape and usage of the sprites in this game. Don't confuse it with "tile" as in `TileDisplay`, which we'll be learning about in Chapter 25. In this chapter and the next, remember that our "tiles" are just ordinary sprites that happen to look like letter tiles.
In most games, you load sprite images from picture files on disk. But in this case, we're going to draw the tile images in a PixelDisplay, and then pull out that part of the display as an image to use for the sprite. It's a neat trick to consider any time you can work out a way to draw your sprite images with code.
{caption: "Listing 3 (Tile-drawing code, and Tile class).", number-from: 38}
```miniscript
blockPic = file.loadImage("/sys/pics/Block.png")
drawBlock = function(letter, x=100, y=100)
gfx.drawImage blockPic, x, y, 40, 40
if letter.len < 2 then
gfx.print letter, x+8, y+4, color.black, "large"
else
gfx.print letter, x+6, y+8, color.black
end if
end function
Tile = new Sprite
Tile.letter = ""
Tile.scale = 2
Tile.tint = color.yellow
Tile.target = {"x":0, "y":0}
Tile.speed = 100
Tile.localBounds = new Bounds
Tile.localBounds.width = 36
Tile.localBounds.height = 36
Tile.goTo = function(x,y, speed)
self.target.x = x
self.target.y = y
if speed != null then self.speed = speed
end function
Tile.goToGridPos = function(col, row)
self.goTo 300 + col * 90, 135 + row * 90
end function
Tile.update = function(dt = 0.1)
mathUtil.moveTowardsXY(self, self.target, self.speed * dt)
end function
Tile.updateAll = function(dt = 0.1)
updatedAny = false
for s in spriteDisp.sprites
if not s isa Tile then continue
if s.x == s.target.x and s.y == s.target.y then continue
s.update
updatedAny = true
end for
return updatedAny
end function
Tile.selected = function()
return self.tint != Tile.tint
end function
```
Add that to your program, then run it, and let's test it out on the command line.
```terminal
]drawBlock "E"
]drawBlock "Qu", 140, 100
```
This should produce two tiles near the bottom of the screen: one "E", and one with a smaller "Qu".
{width:"50%"}
![](WordGameEQu.png)
Now let's try grabbing the first of these as the image for a Tile instance.
```terminal
]t = new Tile
]t.image = gfx.getImage(100,100,40,40)
]display(4).sprites.push t
[t]