Accessible scripts
This guide is about writing .hex scripts that work for everyone who uses Heydrun: people who look at the screen, people who listen to it with a screen reader, people who can't hear it, and people with the sound off. It covers how to show things, how to get someone's attention, how to draw a screen that reads well and looks clear, and how to test. It ends with complete examples to copy and change.
The language itself is described in Scripting. Everything used here is in it.
The short version
- Show it. Put what a script has to say on screen, with
echo,toastor a view. A screen reader already reads what's on screen, so showing it is how it reaches a screen reader user too. - Get attention in more than one way. A flash can't be heard, and a sound can't be seen. When something needs attention, use two or three of
notify,flash,vibrateandsound. - Speak only what isn't shown.
speakis for a spoken cue that has no place on screen, or that someone would otherwise have to go looking for. Most scripts never need it. - Respect the person's settings. Do not disturb, Remove animations and the sound switches are there for a reason. Heydrun follows them for you, as far as it can.
Showing things
| Way | What happens | Use it for |
|---|---|---|
echo <buffer> <text> | A line appears in the chat and stays there. | A record: results, errors, help text. Something the person can come back to. |
toast <text> | A short notice at the bottom of the screen, gone in a few seconds. TalkBack usually reads it. | A quick "done" or "saved" after a command or a button. |
view { … } | The script's own screen, with text, buttons, cards and pictures. | A game, a form, anything with buttons. |
text "…" live in a view | Shown, and read out by a screen reader whenever it changes, while the view is on screen. | What just happened: the score, whose turn it is, a result. |
When in doubt, echo. A line in the chat can be found again with a swipe, /last, /find or Ctrl+Shift+1; a toast is gone in a few seconds.
When an echo is read aloud
An echo is a local line, like Heydrun's own command results. An echo into the buffer on screen within a few seconds of a command you typed is read aloud as that command's result, as Heydrun's own are: even with reading aloud off, and during Do not disturb. So a script's answer to its own command, such as /roll, is heard at once.
Any other echo has no sender, so Heydrun treats it as an event line, and reads it aloud only when Read messages aloud and Also read joins, parts, nick changes and other events are both on, as for a join or a topic change. Otherwise a screen reader user finds the line the way they find any other: by swiping to it, or with /last or Ctrl+Shift+1.
So for the answer to something the person just did, such as a command or a button, either:
- echo it, when it's worth keeping: after a command, it's read at once, or
- toast it, when it's a quick confirmation, so it's shown to everyone and TalkBack usually reads it out, or
- in a view, put it in a
livetext.
Pick one. Echoing and toasting the same thing means the person may hear it twice.
Getting attention
Different people notice different things. A flash is no use to someone who can't see the screen; a sound is no use to someone who can't hear it, or has the phone on silent. These commands each reach someone, and together they reach almost everyone.
| Command | What happens | Who it reaches |
|---|---|---|
notify <text> | A notification in Heydrun's Scripts category, titled with the buffer the script ran in. Tapping it opens that buffer. | Everyone, whether Heydrun is open or not. Android and the screen reader handle it like any other notification. |
flash | The edge of the screen flashes once, as it does for a mention. | People who can see the screen: deaf and hard-of-hearing people, and anyone in a noisy place or with the sound off. |
vibrate | One of Heydrun's vibrations: vibrate mention, vibrate private, or just vibrate for a short knock. | Anyone holding the phone, including in a quiet room or a loud one. |
sound <name> | One of Heydrun's own sounds, such as sound mention or sound join, with its caption when Captions for sounds is on. | People who can hear it, and, through the caption, people who can't. |
speak <text> | Said through the screen reader or Heydrun's voice. | People listening. See Speaking. |
The names sound knows are: message, action, notice, private, mention, highlight, join, part, quit, nick, topic, typing, invite, kick, dcc, online, offline, connected and disconnected. A name it doesn't know is ignored.
Each one follows the person's own settings:
notifyneeds Show notifications on. Its sound, vibration and importance are the Scripts category's, which the person can change or turn off in Android's notification settings for Heydrun. It's posted while Heydrun is open too. Its title, and the buffer tapping it opens, is where the script ran, such as the channel a message came from or where its command was typed; a script running with no buffer, such as from a timer, uses the buffer on screen. Colour codes are taken out, and only the first 1000 characters are kept.flash,vibrateandsoundonly work while Heydrun is open. When it isn't, usenotify.flashworks whether or not Flash for mentions is on, and with Remove animations on it shows and goes without fading.vibrateneeds In-app vibration on, and follows Vibration type, Vibration strength and Vibrate even on silent.soundfollows In-app sounds and that sound's own switch, and vibrates as that sound does when its vibration is on.- Each of them works at most five times in five seconds. Past that they're dropped, not saved for later.
None of these make an error when they're held back, so a script carries on as usual.
Choosing
- Something the person asked for, and will want even if they're away, such as a reminder:
notify. It waits in the notification shade. - Something happening now, while they're in Heydrun:
flashandvibrate, and asoundif it suits. Together they reach the eye, the hand and the ear. - Something that matters but isn't urgent: an echo is enough. People check the chat.
- Don't add to what Heydrun already does. A mention or a private message already notifies, sounds, flashes and vibrates as the person has chosen. A script that alerts for the same line again makes it twice as loud.
Speaking, for what isn't on screen
speak Ten seconds left
speak -i Your turn
stfu
speak <text>says the text through Speak with: the screen reader, or Heydrun's own voice with its Voice speed and Voice pitch. With Speak with set to Screen reader and no screen reader running, nothing is said.speak -i <text>stops whatever is being said first, the screen reader included, then says the text.stfustops speaking, as the/stfucommand does.
A screen reader already reads what's on screen. So speak isn't for a script's ordinary output; echo, toast or a view does that, for everyone. speak is for the few things a listener needs said that aren't shown, or shouldn't be:
- A spoken cue that would get in the way on screen. In a game, a countdown that's redrawn every second never lets anything else be heard. One spoken "ten seconds left", with a
flashfor those who can't hear it, says it once and leaves the view, and the screen reader's place in it, as they were. - Something time-critical that would otherwise need finding. A game bot that says "Red to play" doesn't say it's you. A sighted player sees the line and knows their colour; a screen reader user would have to stop, find the line and work it out. "Your turn", said at once, is the part that matters. The turn cue example does this.
If what you'd speak is also shown on screen, in a toast, a live text or an echo that's read aloud, don't speak it too: the person hears it twice.
A script's speech is only said when all of these are true:
- Scripts can speak is on, in Settings, Accessibility, under Spoken alerts. It's on by default.
- Heydrun is open.
- Heydrun's Do not disturb is off.
- The script hasn't spoken five times in the last five seconds. Lines past that are dropped.
Colour and bold codes are taken out, and only the first 500 characters are said.
When you do speak:
- Use
-ionly for what can't wait. It cuts off whatever was being said, which may be the very message that caused it. "Your turn" is worth it. "Bob joined" isn't. - Say the important word first. "Your turn in #uno" beats "In #uno, it is now your turn", because people often stop listening, or interrupt, after the first few words.
- Keep it short. One sentence. Detail belongs on screen.
- Don't count on being heard. Speech can be off, rate limited or held. Anything that matters should also be on screen, or felt.
Respecting settings and Do not disturb
Three read-only values say how the person is using their phone. They're read when the script asks, so they're always current.
| Field | Value |
|---|---|
$screen.reader | 1 while a screen reader using explore by touch, such as TalkBack, is on; otherwise 0. |
$screen.dnd | 1 while Heydrun's Do not disturb is on; otherwise 0. |
$screen.motion | 0 when the phone's Remove animations is on; otherwise 1. |
Use $screen.reader to change how something is worded, not whether it's shown. For example, a script that decorates its output with emoji might leave them out for a screen reader, where each one is read by name:
if ($screen.reader == 1) { echo $chan Score: %score }
else { echo $chan 🏆 Score: %score 🏆 }
Do not disturb
While Heydrun's Do not disturb is on, a script's speak, flash, vibrate and sound are held, as Heydrun's own are. A held sound still shows its caption, with Captions for sounds on. connected and disconnected sound anyway, as Heydrun's own connection alerts do, and private sounds when Let private messages through is on. notify is still posted, without sound, so it's there afterwards.
Heydrun holds those for you. What it can't know about is anything else a script does to get attention, such as sending a message that makes someone else's phone sound. Check $screen.dnd first.
A script can turn Do not disturb on and off itself:
dnd on ; on until it's turned off
dnd 25 ; on for 25 minutes (1 to 10080, a week)
dnd off
It works as /dnd does, setting away too when Set away while on is on. A value it doesn't understand is ignored. This alias starts a 25 minute focus session:
alias focus {
dnd 25
toast Do not disturb for 25 minutes
}
on DND runs when Do not disturb turns on or off, however it was done: the switch, /dnd, a script, or its time running out. $text is on or off, so on DND:off runs only as it ends. The what I missed example uses it.
Motion
Heydrun follows the phone's Remove animations: with it on, cards in a view appear without dropping in, and the flash shows without fading. A game can keep its animations; see Games. A script that moves things itself, such as by redrawing a view on a timer, should check $screen.motion and do less, or nothing, when it's 0.
Changing what's read aloud: the SPEAK event
on SPEAK runs just before Heydrun reads a line from the buffer on screen aloud: a new message when Read messages aloud, or the buffer's own Read aloud choice, reads it; an event, when events are read; the result of a command you typed; a connection alert. A handler can:
rewrite <text>to say something else instead,haltto say nothing at all,- or return, to leave it as it was.
The line in the chat is never changed, only what's said. SPEAK changes Heydrun's reading aloud only; when the person swipes to the line with their screen reader, they hear the line as it is.
In a SPEAK handler:
$textis the whole line as it would be spoken, worded by the settings in How messages read: the encryption note, such as [not encrypted], where there is one, the nick, the time if it's read, and the message. So match it loosely, withisin,iswmor a filter such ason SPEAK:*http*, rather than comparing it whole.$nickis who sent it, and is empty for events, alerts and command results.$ismeistruefor your own line, when your own messages are read aloud.$chan,$network,$isprivateand$mework as in other events.
SPEAK doesn't run for mentions announced from other buffers, for /speak, /last or /catchup, or for a script's own speak. Lines from an encrypted channel or query only reach it while Scripts can read encrypted messages is on, in Settings, Privacy.
A SPEAK handler runs for every line read aloud, so keep it quick.
To stop reading a nick or a channel altogether, there's no need for a script: /speechignore nick does it. SPEAK is for the cleverer cases, such as a bot that should only be read when it mentions you. The examples below have three.
Drawing views that work for everyone
A view is read the way it's written: top to bottom, and left to right within a row. A screen reader user meets it one item at a time, without the layout to help, so each item has to make sense on its own. A sighted user sees it all at once, so what matters has to show, not only be said.
Name the screen with a heading
text "Higher or lower" heading bold
The first heading names the screen, and is said when it opens. Headings also let people jump between parts of a long view. Give each part a heading, but not every line. bold and a larger textsize make it look like a heading too.
Give every button a name it can say
A button is read as its label. A label of symbols reads badly, if at all: "▲" may be read as "black up-pointing triangle". Give it alt:
button "▲" hl_guess higher alt "Higher"
button "♠" pick_suit s alt "Spades"
The label still shows on screen; the alt is what's said. Write the name of what the button does, not what it looks like. Quote an alt of more than one word.
Say, and show, what state a button is in
selectedmarks a button as chosen, such as a held card, a picked seat or your current answer. It's read as selected, and drawn with an outline in the theme's primary colour. A word somewhere on screen as well, such as "You said higher", helps anyone who misses the outline.disabledmarks a button that can't be used right now. It's drawn faded, read as disabled, and a tap does nothing. It stays where it is and can still be reached, so the person learns it exists.
Prefer disabled to leaving a button out. A button that comes and goes moves everything after it, and the person loses their place.
Because a view's body is filled in before it's read, a variable can hold a state:
set %hl_off disabled
...
button "▲" hl_guess higher alt "Higher" %hl_off
While %hl_off is empty, the button is usable; set it to disabled, draw the view again, and it isn't. A variable can hold more than one prop, such as selected bg #1565c0.
Don't use colour alone
Some people can't tell red from green, and a screen reader doesn't say colours at all. Whatever a colour means, say it in words or a symbol as well:
- A right or wrong answer: "Right!" or "Wrong", not only a green or red text.
- Whose turn it is: the name in a live text, not only a coloured border round their seat.
- A chosen button:
selected, and a word on screen, not only abg.
Cards are already done for you: a red card shows its suit symbol as well as its colour, and is read by name.
Keep text readable against its background. White on a dark blue, such as #1565c0, is fine; light grey on white isn't.
Group what belongs together
merge on a container reads everything in it as one item:
row merge gap 6 {
text "Score"
text "%score"
}
That's read as "Score, 3", in one swipe, instead of "Score" and then a lone "3". Use it for a label and its value, or a player's name, chips and cards.
alt on a merged container names the whole group instead of reading what's in it. Quote a name of more than one word: column merge alt "Seat 1, 500 chips".
Use live text for what changes
text "%status" live
A live text is shown like any other, and read by a screen reader whenever it changes, without the person having to find it. Use one, near the top, for what just happened. Two live texts that change together are read one after the other, and a live text that changes every second, such as a timer, never lets anything else be heard.
When a tap changes several things, put them in one live sentence: "It's a queen. Right! Score 3." rather than a live card, a live result and a live score.
Pictures, cards and rings
- An
imageis skipped unless it hasalt "…"saying what it shows. Leavealtoff for decoration. - A
cardis read as one name, such as "Queen of hearts", "Face-down card" or "Empty", not as its rank and suit symbols. - A
ringreads its children in the order they're written, child 0 first, whatever their place around the table. Write seats in the order people take turns.
Don't chatter
- Draw the view again when something changes, not on a timer.
- Say each thing once: in a live text, or a toast, not both.
- Keep labels short. "Fold", not "Click here to fold your hand".
Games
Three things mark a script as a game:
- A
; @gamecomment among the first comment lines of the file, before any code, puts a Game badge beside it on the Scripts screen. gameon the outermost container of its view keeps the view's animations when the phone's Remove animations is on, as Android games do. Other views follow that setting: cards appear without dropping in.gameonly works on the outermost container, so the view needs a single one, such as acolumn, holding everything else.$screen.motiontells the script whether Remove animations is on, should it want to do less itself.
Whose turn it is matters most in a game. Put it in a live text while the game is on screen. When the person may be elsewhere in Heydrun, flash and vibrate reach those who are looking or holding the phone, and a short speak -i reaches those who are listening, when nothing on screen would say it.
Testing
Turn your script on in Settings, IRC, Scripts, and reload it after each change.
With TalkBack
- Open it and listen. The first thing said should be the screen's name, from its first heading.
- Swipe right through everything, from the top. Is the order the order things happen in? Does each item make sense on its own? Is any button read as a symbol, or as just "button"?
- Move by heading. In recent versions of TalkBack, swipe up then down to choose Headings, then swipe down to move. Each part should have one.
- Double-tap each button, and listen. You should hear what happened, from a live text or a toast, once. Note where focus is afterwards.
- Check states. A chosen button should say selected; one that can't be used, disabled. Double-tap a disabled one: nothing should happen.
- Explore by touch, dragging a finger round the screen, to find anything a swipe skipped, or anything read twice.
- Try the settings that change speech: Speak with set to Screen reader and to Text-to-speech, Scripts can speak off, and events read aloud on and off. With events read aloud off, can you still find every echo?
With the sound off
- Turn the sound off, or the phone to silent, and use the script by eye. Is everything that matters shown? Does anything that alerts by sound also flash, vibrate or notify?
- Turn on Captions for sounds, and check each
soundshows what it's for. - Look at the colours. Does anything rely on colour alone? Is all the text easy to read?
With the person's settings
- Turn Do not disturb on. The script should stay quiet: no speech, flashes, buzzes or sounds, and notifications without sound.
- Turn on Remove animations, in Android's accessibility settings. Only a
gameview should still move. - Try it fast. Tap a button many times quickly. Past five in five seconds, speech, flashes, vibrations, sounds and notifications are dropped; make sure nothing important was in them.
If something isn't working, log <text> writes to the script log, and an error there is often why a view or a line is missing.
Examples
Each example is a complete script. Paste one into a new script on the Scripts screen, save, and turn it on. Globals start with a short prefix, such as %hl_, because all scripts share them.
Dice roller
/roll rolls a six-sided die; /roll 2d6 rolls two; /roll d20 one twenty-sided die. The roll goes to the channel as an action, so everyone sees it, you included.
; dice.hex: /roll, /roll 2d6 or /roll d20
alias roll {
set -l %count 1
set -l %sides 6
if ($1 iswm *d*) {
set -l %parts $split($lower($1), d)
set -l %count $get(%parts, 0)
set -l %sides $get(%parts, 1)
}
if ($len(%count) == 0) { set -l %count 1 }
set -l %count $clamp($int(%count), 1, 10)
set -l %sides $clamp($int(%sides), 2, 100)
set -l %rolls $list()
set -l %i 0
while (%i < %count) {
push %rolls $rand(1, %sides)
inc %i
}
set -l %total $sum(%rolls)
me rolls %count d%sides: $join(%rolls, ", "). Total %total.
}
alias rollmakes the/rollcommand. What you type after it is$1.- The two
set -llines start with one six-sided die.-lkeeps these values to this command, so they can't clash with another script's. if ($1 iswm *d*)checks for something like2d6.$splitcuts it at thed, and$gettakes the two halves: 0 is the first, 1 the second.d20has nothing before thed, so the next line makes an empty count 1.$clamp($int(…), 1, 10)turns the count into a whole number from 1 to 10, so/roll 500d6or/roll xd6can't flood the channel. The sides go from 2 to 100.- The
whileloop rolls once per die.$rand(1, %sides)picks a number from 1 to the number of sides, andpushadds it to the list. $sumadds them up.mesends the roll to the channel as an action, such as "rolls 2 d6: 4, 6. Total 10." It shows in the chat like any line you send, and is read aloud if you have your own messages read.
Reminder
/remind 10 the tea is ready puts a line in the chat now, and a reminder in the chat and the notification shade in ten minutes.
; remind.hex: /remind <minutes> <what>
alias remind {
if ($0 < 2) { echo $chan Usage: /remind <minutes> <what> | return }
set -l %mins $int($1)
if (%mins < 1) { echo $chan Give the minutes as a number, such as /remind 10 tea | return }
timer $calc(%mins * 60000) remind_due $2-
echo $chan I'll remind you in %mins minutes: $2-
}
on SIGNAL:remind_due {
echo $chan Reminder: $1-
notify Reminder: $1-
flash
}
$0is how many words followed/remind. With fewer than two, it shows how to use it and stops;|puts two statements on one line.$intmakes the minutes a whole number; anything that isn't a number becomes 0, which the next line turns away.timerwaits that many milliseconds (60000 to a minute), then runsSIGNAL:remind_due, handing it everything after the minutes,$2-.- The echo confirms it, in the chat, where you can check it later.
- When the time comes,
on SIGNAL:remind_dueputs the reminder in the same buffer you set it from, and posts it as a notification. The notification is what alerts you, with the sound and vibration of the Scripts category, whether Heydrun is open or not, and it waits in the shade until you've seen it. flashis for when you're looking at Heydrun with the sound off. With Heydrun closed, it does nothing.
During Do not disturb, the notification comes without sound and the flash is held, so the reminder waits for you quietly. A reminder is lost if scripts are reloaded or Android closes Heydrun.
Turn cue
For a game bot that announces turns by colour, such as "Red to play", rather than by nick. /colour red tells the script which colour you are. When it's your turn, the screen flashes, the phone vibrates, and "Your turn" is said.
; turncue.hex: /colour <your colour>, then a cue when it's your turn.
alias colour {
set %tc_colour $1
toast You're playing %tc_colour
}
on TEXT:*to play* {
if ($len(%tc_colour) == 0) { return }
if (!($text iswm %tc_colour to play*)) { return }
flash
vibrate mention
speak -i Your turn
}
alias colourremembers your colour, and confirms it with a toast, which is shown and usually read by TalkBack.on TEXT:*to play*runs for incoming lines with "to play" in them. The filter ignores case.- The first
ifdoes nothing until you've chosen a colour. The second checks the line starts with your colour;iswmignores case too, so "RED to play" counts. flashandvibrate mentionare for everyone looking at the screen or holding the phone.speak -iis the one place in these examples where speaking is right. The bot's line is already on screen, so an echo or a toast would only repeat "Red to play". What it doesn't say is that red is you, and someone listening would have to stop, find the line and work that out while the game moves on. "Your turn", said at once, cutting in, is the part that matters, and it isn't anywhere on screen.
Because the bot's line doesn't contain your nick, Heydrun doesn't treat it as a mention, so none of this doubles up with its own alerts. None of it happens while Heydrun is closed, or during Do not disturb.
What I missed
While Do not disturb is on, keeps every mention and private message. When it ends, lists them in the chat.
; missed.hex: list the mentions and private messages from Do not disturb.
on LOAD { set %ms_list $list() }
on HIGHLIGHT {
if ($screen.dnd != 1) { return }
push %ms_list $time $chan <$nick> $text
}
on DND:off {
if ($len(%ms_list) == 0) { return }
toast $len(%ms_list) messages while Do not disturb was on
foreach %m %ms_list { echo -a %m }
set %ms_list $list()
}
on HIGHLIGHTruns for a live line from someone else that mentions you, or a private message, in any buffer, whether Heydrun is open or not.- While Do not disturb is on,
pushadds the time, the buffer, the nick and the message to a list. on DND:offruns as Do not disturb ends. With nothing kept, it stops.toastsays how many, on screen, where TalkBack usually reads it too.foreachechoes each one into the buffer you're in,-a, where they stay to be read at your own pace. Then the list is emptied for next time.
The messages are still in their own buffers as well; this just gathers them in one place. The list is lost if scripts are reloaded.
Away notes during Do not disturb
While Do not disturb is on, tells anyone who sends you a private message that you'll read it later, once an hour per person at most.
; dndnote.hex: during Do not disturb, say you'll answer later.
on LOAD { set %dn_told $map() }
on TEXT {
if ($isprivate != true) { return }
if ($isme == true) { return }
if ($screen.dnd != 1) { return }
set -l %who $lower($nick)
set -l %last $get(%dn_told, %who)
if ($len(%last) > 0) {
if ($calc($ctime - %last) < 3600) { return }
}
setat %dn_told %who $ctime
notice $nick I'm not reading IRC just now. I'll see your message later.
}
on LOADmakes an empty map, to remember when each person was last told.on TEXTruns for every incoming message. The first three lines let through only private messages, from someone else, while Do not disturb is on.%whois the nick in lower case, so Alice and alice count as one person.$getlooks up when they were last told. Nothing there means never.- If it was less than an hour ago (3600 seconds), it stops, so a chatty friend gets one note, not one per line.
setatremembers now,$ctime, andnoticesends the note. A notice is used because, by IRC custom, nothing answers a notice automatically, so two away scripts can't answer each other for ever.
People let through Do not disturb get the note too. To leave them out, check their nick before sending.
Shorter joins and quits
When events are read aloud, says "alice joined #rust" instead of "alice, tilde alice at some long host, has joined #rust".
; shorter.hex: joins, parts and quits in fewer words, when they're read aloud.
on SPEAK {
if ($len($nick) > 0) { return }
set -l %t $re_replace($text, " \(\S+\) has", " has")
set -l %t $replace(%t, has joined, joined)
set -l %t $replace(%t, has left, left)
set -l %t $replace(%t, has quit, quit)
rewrite %t
}
on SPEAKruns just before a line is read aloud.- A message has a sender in
$nick; events don't. The first line leaves messages alone. $re_replacetakes out the user@host in brackets before "has". The pattern is in quotes so its brackets and spaces are kept as written.- The three
$replacelines shorten the wording. They only match Heydrun's English wording; in another language, change the words to match what you hear. rewritesays the shorter version. The chat still shows the full line.
Brief wording and Hide hostnames in join/part/quit messages also take out the user@host, without a script; this one shortens the rest.
A quiet bot
Doesn't read a busy bot aloud, unless it mentions you.
; quietbot.hex: don't read FactBot aloud, unless it mentions you.
on SPEAK {
if ($lower($nick) != factbot) { return }
if ($me isin $text) { return }
halt
}
$lower($nick)makes the comparison ignore case, as==and!=don't. Write the bot's nick in lower case.- Any line from someone else is left alone.
- A line from the bot that contains your nick is read as usual.
haltsays nothing for the rest. They still show in the chat.
To silence the bot completely, /speechignore FactBot does it without a script.
Say "link" for web addresses
; links.hex: say "link" instead of reading out a web address.
on SPEAK:*http* {
rewrite $re_replace($text, "https?://\S+", "link")
}
on SPEAK:*http*runs only for lines containing "http".- The pattern matches
http://orhttps://and everything up to the next space, andrewritesays "link" in its place. The link itself is still in the message, and in its Open action.
Higher or lower
A small card game with a view that reads well and shows clearly. Open it from the script launchers, under Higher or lower.
; @game
; higher.hex: guess whether the next card is higher or lower. Aces are low.
on LOAD {
set %hl_faces $list(A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K)
set %hl_names $list(an ace, a 2, a 3, a 4, a 5, a 6, a 7, an 8, a 9, a 10, a jack, a queen, a king)
set %hl_suits $list(s, h, d, c)
sidebar add higher hl_new Higher or lower
}
alias hl_new {
set %hl_score 0
set %hl_off
set %hl_hi
set %hl_lo
hl_deal
set %hl_msg You have $get(%hl_names, %hl_card). Higher or lower?
hl_draw
}
alias hl_deal {
set %hl_card $rand(13)
set %hl_face $get(%hl_faces, %hl_card)$pick(%hl_suits)
}
alias hl_guess {
if (%hl_off == disabled) { return }
set -l %was %hl_card
hl_deal
set -l %name $get(%hl_names, %hl_card)
if ($1 == higher) {
set %hl_hi selected bg #1565c0
set %hl_lo
}
else {
set %hl_lo selected bg #1565c0
set %hl_hi
}
if (%hl_card == %was) {
set %hl_msg It's %name, the same. You said $1. That one's free.
}
elseif ((%hl_card > %was && $1 == higher) || (%hl_card < %was && $1 == lower)) {
inc %hl_score
set %hl_msg It's %name. You said $1: right! Score %hl_score.
}
else {
set %hl_off disabled
set %hl_msg It's %name. You said $1: wrong. You scored %hl_score.
}
hl_draw
}
alias hl_draw {
view {
column game gap 12 pad 16 {
text "Higher or lower" heading bold textsize 20
card %hl_face width 72
text "%hl_msg" live
row merge gap 6 {
text "Score"
text "%hl_score"
}
row gap 8 {
button "▲" hl_guess higher alt "Higher" %hl_hi %hl_off
button "▼" hl_guess lower alt "Lower" %hl_lo %hl_off
}
button "New game" hl_new
}
}
}
The script:
; @game, as the first line, gives the script a Game badge on the Scripts screen.on LOADkeeps three lists: the card faces the renderer draws (T is ten), the same cards as words, and the four suits.sidebar addputs Higher or lower among the script launchers.hl_newstarts a game: score 0, both buttons usable and neither chosen.setwith no value empties a variable.hl_dealpicks a card: a number from 0 to 12 for its rank, which is also its place in both lists, and a face such asQhfor the queen of hearts.hl_guessruns when a button is tapped, withhigherorloweras$1. After a wrong guess it does nothing, as the buttons are disabled.- It remembers the old card in
%was, deals a new one, and marks the button you tapped as selected, which outlines it, with a blue background too, and the other not. - Then it compares the ranks. The same card is free, a right guess adds a point, and a wrong one ends the game, disabling both buttons.
- Every outcome becomes one sentence in
%hl_msg, starting with the new card, so it's the first thing heard and seen. It says what you guessed and whether it was right in words, so neither the blue button nor a colour has to.
The view:
column gameholds everything, sogameis on the outermost container: the card drops in even with Remove animations on.- The heading names the screen, "Higher or lower", when it opens, and is large and bold on screen.
- The card is drawn with its rank and suit, and read as its name, such as "Queen of hearts".
- The live text shows the outcome, and is read after each tap, such as "It's a queen. You said higher: right! Score 3." Nothing else in the game speaks.
row mergereads the score as one item, "Score, 3".- The arrow buttons show arrows and are read as "Higher" and "Lower", from
alt.%hl_hi,%hl_loand%hl_offfill inselected, its background anddisabledwhen they're set, and nothing when they're empty. A disabled button is drawn faded. - New game is always there, and starts again.