Mobile apps with App Inventor
Dice Roller
Build a dice app: tap Roll for a random number, and the app cheers when you roll a six.
No more lost dice under the sofa. Today's app rolls a dice every time you tap a button, with a huge number you can read from across the table, and it cheers when you roll a six.
You will learn
- Style components with colours, sizes and shapes
- Set a label's text with a block
- Use random numbers
- Make a decision with an if then else block
You will need
- A computer with a web browser
- A phone or tablet with the MIT AI2 Companion app
- Day 1, for the Designer and the Blocks editor
Words to know
- random
- Unpredictable: any of the numbers could come up.
- setter
- A dark green block that changes a property, such as set Label2.Text.
- if then else
- Runs one set of blocks if a test is true, and another if it is false.
- mutator
- The gear on some blocks, which adds parts such as else.
Step by step
Step 1: A new project
Start a new project called
DiceRoller. Choose Screen1 and set its Title toDice Roller, AlignHorizontal to Center and BackgroundColor to Blue.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceButton?CheckBox?DatePicker?Image?Label?ListPicker?ListView?Notifier?PasswordTextBox?Slider?Spinner?Switch?TextBox?TimePicker?WebViewer?LayoutMediaDrawing and AnimationMapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41Dice RollerComponentsScreen1RenameDeletePropertiesScreen1 (Screen)AboutScreenAccentColorDefaultAlignHorizontalCenter : 3AlignVerticalTop : 1AppNameDiceRollerBackgroundColorBlueIconNone...PrimaryColorDefaultScreenOrientationUnspecifiedSizingResponsiveThemeDevice DefaultTitleDice RollerTitleVisibleScreen1, set up, with a blue background. Step 2: Two labels
Drag two Labels onto the phone. The first is the message: set its Text to
Tap the button to roll, its FontSize to22and its TextColor to White. The second will show the number, so make it huge: Text?, FontSize120, tick FontBold and set TextColor to White too.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceButton?CheckBox?DatePicker?Image?Label?ListPicker?ListView?Notifier?PasswordTextBox?Slider?Spinner?Switch?TextBox?TimePicker?WebViewer?LayoutMediaDrawing and AnimationMapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41Dice RollerTap the button to roll?ComponentsScreen1Label1Label2RenameDeletePropertiesLabel2 (Label)BackgroundColorNoneFontBoldFontItalicFontSize120FontTypefacedefaultHTMLFormatHasMarginsHeightAutomaticWidthAutomaticText?TextAlignmentleft : 0TextColorWhiteVisibleA message, and a huge question mark for the number. Step 3: A roll button
Drag a Button under the labels. Set its Text to
Roll, FontSize to24, Shape to rounded, BackgroundColor to Orange and Width to150pixels.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceButton?CheckBox?DatePicker?Image?Label?ListPicker?ListView?Notifier?PasswordTextBox?Slider?Spinner?Switch?TextBox?TimePicker?WebViewer?LayoutMediaDrawing and AnimationMapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41Dice RollerTap the button to roll?ComponentsScreen1Label1Label2Button1RenameDeletePropertiesButton1 (Button)BackgroundColorOrangeEnabledFontBoldFontItalicFontSize24FontTypefacedefaultHeightAutomaticWidth150pxImageNone...ShaperoundedShowFeedbackTextRollTextAlignmentcenter : 1TextColorDefaultVisibleA big orange Roll button. Step 4: Roll a number
Go to Blocks. Build: when Button1.Click (from Button1), with set Label2.Text to (from Label2, dark green) inside it. From the Math drawer, drag random integer from 1 to 100 into the socket, then click the 100 and change it to 6.
App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1Label1Label2Button1Any componentRenameDeleteShow Warningsrandom integer from 1 to 1000=+Click Math (1) and drag the random integer block (2). App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1Label1Label2Button1Any componentRenameDeleteShow Warningswhen Button1.Clickdoset Label2.Text to random integer from 1 to 6Each click sets the number to a random whole number from 1 to 6. Step 5: Roll it on your phone
Connect the Companion and tap Roll a few times. Every tap shows a new number from 1 to 6.
9:41Dice RollerTap the button to roll4Tap Roll: a random number from 1 to 6. Step 6: Celebrate a six
Make the app cheer when you roll a six. From Control, drag an if then block under the set block. Click its blue gear and drag an else into it. Then build the test from Math: the = block, with Label2.Text on one side and the number 6 on the other.
If it is a six, set Label1's text to
A six! You win!, and otherwise toRoll again. Text blocks are in the Text drawer.App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1Label1Label2Button1Any componentRenameDeleteShow Warningswhen Button1.Clickdoset Label2.Text to random integer from 1 to 6if Label2.Text = 6thenset Label1.Text to “ A six! You win! ”elseset Label1.Text to “ Roll again ”If the number is 6, cheer. Otherwise, roll again. Tip Blocks run from top to bottom, so the number is rolled before it is checked.Step 7: Roll for a six
Try it on the phone. How many rolls does it take to get a six?
9:41Dice RollerA six! You win!6A six!
Test it
Tick each one when it works. If one does not, look back at the step it comes from.
Stretch challenges
Finished early? Try these. Each one has a hint if you get stuck.
Two dice
Roll two dice at once and show the total.
Show a hint
Add Label3 for the second dice. Then set Label1's text to a join of
Total:and Label2.Text + Label3.Text, using the + block from Math.Shake to roll
Roll when the phone is shaken, as well as when Roll is tapped.
Show a hint
Add an AccelerometerSensor from Sensors. Its when AccelerometerSensor1.Shaking event can hold the same blocks as the button.
A different dice
Make it a 20 sided dice, as some board games use.
Show a hint
Change the 6 in the random block to 20, and the test to 20.
Notes for the teacher
Timing
- 15 minutes: steps 1 to 3, styling the screen.
- 20 minutes: steps 4 and 5, the first blocks, and testing.
- 15 minutes: steps 6 and 7. Adding else with the gear (the mutator) is fiddly the first time: demonstrate it.
Mistakes to look out for
- set Label1.Text instead of set Label2.Text: the message changes instead of the number.
- The = block from Logic instead of Math: both work for numbers, but the Math one is the one in the picture.
- A number left as 100 in the random block.
Going further
Is it fair? Each student rolls 30 times and tallies. Pool the class results: each number should come up about one time in six.