Today App Inventor article, we will make use on Accelerometer in our mobile phone to create our Tilting Ball game. Let discuss this simple rolling ball game.

crank home alone GIF by Art of the Title

Titling Ball Design

Main Screen display

  • Add 4 x Labels for Score, Lives, Timer, and Roll and 4 x Text box.
    • Rename Text box to LivesValueText , ScoreValueText, RollValueText, TimerValueText.
  • Add HorizontalArrangement and 2xTableArrangement for the top 4 labels.
  • Add Canvas for the Ball Image Sprite, and TargetImage Sprite.
  • Upload 2X Image for Ball and Target.
  • Add Clock1 and Accelerometer components.
    • Set Clock1 interval to 5000.

Second Screen

  • Add Text Box to show "Game Over"
  • Add Button for Play Again.
  • Upload 1x Image for Screen2's Background Image

Titling Ball Block Code

  • Declare score, lives, timer, roll1, pitch1 variables.
    • Set lives = 5, timer = 10, score = 0
  • Clock Timer function for following:
    • TimerEnabled = false;
    • Change the timer variable - 0.5
    • Check if less than 0, if yes, go to endgame procedure;
    • If no, call jumptarget procedure;
    • Reduce the lives by -1;
    • Show the lives in the LivesValueText Label;
    • TimerEnable = true;
  • When AccelerometerSensor.AccelerationChange,
    • Set the xAccel to pitch1
    • Set yAccel to roll1
    • Set the Ball1.X to pitch1
    • Set Ball1.Y to roll1.
    • Show RollValueLabel to roll1.
  • When Ball1 collided with,
    • Call jumptarget procedure;
    • Increase the score by 1.
    • Show the score in ScoreValueLabel
  • Jumptarget procedure, is to change the TargetImage sprite to random X, Y position with the canvas Width and Height.
  • EndGame procedure is to
    • Set the Accelerometer enabled to false;
    • TimerEnabled = fase
    • Show end game text in the endgame label in Screen1.
    • Open screenframe to Screen2

Finally, let try to get as high as the score within 10 seconds. If not possible, increase the timer.

Take care and enjoy your day.


This free site is ad-supported. Learn more