Basketball game in MIT App Inventor

Hello friends,
Welcome to Obsidian Soft.
I will teach you how to make a cool but easy basketball game in MIT app Inventor in this tutorial.


Open up MIT App Inventor, start a new project, and name it BasketBall.
For screen 1 make align horizontal and align vertical both centre. Upload a basket court image with baskets in vertical form.


I will give the link in the video description.

Drag and drop a canvas from Drawing and Animation. Make height and width 95 percent. Choose the court image as the background. Make the paint color white as we will write the score on our canvas too.

Drag and drop two ball sprites from Drawing and Animation onto the canvas. Rename one to ball and the other one to basket. Make the ball’s radius 13 and the interval after it moves 50. This is in milliseconds. Make the ball color dark orange like a real basketball.

For the basket, make the radius a bit smaller at 10. Give it a color similar to the background and make it almost transparent by going to custom and increasing transparency. We will give both sprites their positions on Canvas programmatically in the blocks section.

Also, add a clock sensor from Sensors.
And make timer interval 200. The timer should be enabled and the timer should always fire.

Our screen design is done so go to the blocks section.

Get ball’s getFlung event.
Set ball speed to speed x7 and set heading to heading. And remember that we have already set the interval to 50 ms in designer's view in the ball’s properties.


When the clock timer fires, we will set our basket position to be on top of the actual basket in our canvas background image. You must be wondering why we aren’t doing it in the screen initialize event and why in a clock timer event. I have noticed that in iPhone, any initialization using screen properties such as screen height or width doesn’t work properly in the screen initialize event so it is better to do such kind of work with a tiny delay so our clock runs after 200 ms and when the timer goes off, we set our basket position.


We will also disable the timer so that the timer doesn't keep on firing.
Next, what happens when something collides with our almost invisible basket, it is obviously the ball as that is the only thing that can move on our canvas. So, we should increase our score but also reset our ball to its original position. Let’s first make a procedure for resetting the ball to go to a starting position.

Also, make a global variable score.

Next, get the collided event for the basket. If the basket is colliding with the ball, then call procedure resetBall, clear the canvas so the previously written score is removed, set the new score, and draw it as text on canvas.


We should also reset the ball in two other events.
When the screen is initialized:


When the ball hits the top and bottom edge. So, get the ball edge reached event.


For other edges i.e. left and right just let the ball bounce.
This is done. I hope you have fun making this simple but cool basketball game in MIT App Inventor.

Thank you for reading this tutorial. If you like my work, please support it by buying me a cup of coffee. Just click on the following link:
You can download aia file for this project:
You can have a look at the video tutorial for the Basketball game app here:


Please like my videos and share them with your friends and family. Also, subscribe to my channel and press the bell icon so you don't miss any of the great projects I have planned for you.


Please like my Facebook page and Instagram page for more educational resources and tips.

Pinterest:
https://www.pinterest.co.uk/obsidiansoft/_created/
For links to free educational apps, have a look at the educational apps page

Comments

Popular posts from this blog

Jolly Phonics Group 1

Jolly Phonics Group 2

Jolly Phonics Flash Cards