00:00:00
[Music]
00:00:01
[Applause]
00:00:04
Hello friends and welcome to obsidian
00:00:06
soft
00:00:08
in this tutorial I will teach you how to
00:00:10
make a cool but easy basketball game in
00:00:13
MIT App Inventor
00:00:15
first of all open up MIT App Inventor go
00:00:18
to projects start a new project and call
00:00:21
it basketball
00:00:27
for screen one make a line horizontal
00:00:29
Center align vertical Center let's
00:00:33
upload some media so I will be uploading
00:00:36
a basketball court image in vertical
00:00:39
form
00:00:41
from drawing and animation
00:00:44
drag and drop a canvas make the height
00:00:48
and width both
00:00:50
95 percent
00:00:55
and
00:00:57
choose the background image the image
00:00:59
that we uploaded earlier and I'm also
00:01:03
going to make the paint color white
00:01:06
and the font size
00:01:10
30 because I will be also drawing the
00:01:15
score as a text
00:01:16
in the middle of the canvas now from
00:01:19
drawing an animation again drag and drop
00:01:23
two ball Sprites
00:01:25
rename the first one to ball so this
00:01:30
will be our actual basketball
00:01:33
and we are going to make the radius 13
00:01:39
and I'm going to make its interval which
00:01:42
is the interval after which it moves
00:01:45
to 50 milliseconds and I'm also going to
00:01:49
make the
00:01:50
color of the ball by going to paint
00:01:53
color a dark orange color so let's go to
00:01:57
custom and use it dark orange color Okay
00:02:03
rename the second ball Sprite
00:02:07
to basket
00:02:09
this will be actually an almost
00:02:11
invisible Sprite which will be
00:02:14
positioned over our Basket in this
00:02:18
basketball court image okay and the
00:02:21
reason we are not going to be making it
00:02:24
Invisible by unchecking visible is
00:02:27
because then we will not be able to
00:02:29
detect Collision events so for detecting
00:02:33
Collision events we have to make it
00:02:35
visible but we are going to make it
00:02:37
transparent by going to its paint color
00:02:42
and choosing again custom and choosing a
00:02:47
somewhat similar color to this color
00:02:49
where the basket is actually located so
00:02:54
somewhere here let's make it a bit
00:02:56
lighter and I'm also going to reduce
00:03:00
its opacity
00:03:03
so it becomes transparent
00:03:07
so as you can see it's not visible
00:03:09
anymore
00:03:11
I'm going to make its radius
00:03:14
10. we will give both these Sprites the
00:03:17
ball and the basket the position on this
00:03:20
canvas in the cord in the block section
00:03:22
now from sensors
00:03:25
drag and drop a clock sensor and I will
00:03:28
tell you why it is needed when we go to
00:03:31
the code but just make the time and
00:03:34
turbo 200 and the timer should always
00:03:36
fire and the timer should be enabled our
00:03:39
screen design is done so let's go to the
00:03:41
block section get the balls
00:03:44
get flung event
00:03:47
that is the event that is triggered when
00:03:49
we fling our finger on the screen and we
00:03:53
are going to set our ball speed to the
00:03:56
speed of the fling multiply by seven so
00:03:59
go to maths and get the multiplication
00:04:02
block and from ball get
00:04:06
etceter for Speed so set the speed to a
00:04:11
multiplication of the speed of the fling
00:04:15
so get speed multiply by 7.
00:04:23
and we are going to set the balls
00:04:25
heading
00:04:27
that is Direction
00:04:31
to The Heading of the fling
00:04:36
now why do we need the clock
00:04:39
so first get its timer event which is
00:04:42
triggered after 200 million seconds now
00:04:46
in this timer we will be setting the
00:04:48
invisible baskets position to be on top
00:04:52
of the actual basket here but why are we
00:04:55
not doing it in screens initialize event
00:04:59
and why hair because I have noticed that
00:05:01
in iPhone any initialization using
00:05:04
screen properties such as screen height
00:05:06
of it doesn't work properly in the
00:05:09
screen initialize event it is better to
00:05:11
do with such kind of work after a tiny
00:05:14
bit of delay that is I'm using 200
00:05:16
milliseconds here and when the timer
00:05:19
will go off we will set our basket's
00:05:21
position okay but first of all let's
00:05:24
disable the timer so that it doesn't
00:05:26
keep on firing so disable it by making
00:05:30
timer enabled to fall so from logic we
00:05:33
got the false clock
00:05:35
after disabling the timer we are going
00:05:38
to set the position of our basket so
00:05:42
setbasket dot x to the middle of the
00:05:46
canvas
00:05:48
in terms of width that is horizontally
00:05:51
so we have done this kind of calculation
00:05:54
before 2 this is
00:05:56
using maths block we need
00:06:00
a minus block and we need a division
00:06:03
block and we will be
00:06:07
plugging the division block here and
00:06:11
this is canvas dot width
00:06:17
divided by 2
00:06:20
and we have to also take care of the
00:06:23
balls width divided by 2 but we don't
00:06:25
have to divide it by 2 because we are
00:06:27
going to use the radius here which is
00:06:29
already
00:06:30
the balls sorry the baskets
00:06:33
width divided by 2.
00:06:36
so basket radius here it is
00:06:41
and this will give us the X position in
00:06:44
the middle of the screen for our basket
00:06:46
and I know that for my basket to come
00:06:52
over that image the Y should be
00:06:57
50.
00:06:59
so
00:07:02
the Y might be different for you if
00:07:04
you're using a different
00:07:06
basketball court image
00:07:09
okay next what happens when something
00:07:11
collides with our invisible basket it is
00:07:16
the ball so we should increase our score
00:07:19
but we should also reset a ball to its
00:07:24
starting or original position on the
00:07:26
canvas so let's first make a procedure
00:07:29
by going to procedure
00:07:31
and getting this
00:07:33
first block and call it reset ball
00:07:39
because we will be using it more than
00:07:41
once so it's better to make a procedure
00:07:43
first of all we are going to set the
00:07:46
pulse speed to zero because we don't
00:07:49
want the ball to keep on moving after
00:07:52
being reset so set the balls speed to
00:07:57
zero
00:08:02
and then we are going to give it a
00:08:05
position
00:08:07
in the middle of the canvas width wise
00:08:09
but somewhere over here okay so that is
00:08:13
its original or starting position so we
00:08:16
can duplicate this one because this will
00:08:18
give us the middle of the canvas for
00:08:21
balls X
00:08:23
but we just need to change the basket to
00:08:25
ball
00:08:27
and duplicate this
00:08:29
but here for the balls why we are going
00:08:33
to do a bit of calculation and it will
00:08:36
be a minus block from Max
00:08:40
okay and here
00:08:44
we are going to be subtracting
00:08:48
200 from the canvases height so I can
00:08:52
plug it back in and this is 200 and this
00:08:55
will give us a very nice balls original
00:08:58
position somewhere over here
00:09:01
okay also make a global variable by
00:09:04
going to variables for a score and
00:09:08
assign it zero so from Max get to zero
00:09:12
block
00:09:14
next we get the collided event for our
00:09:18
basket so when the basket
00:09:22
collides with some Sprites so in our
00:09:26
case it will be the ball so check when
00:09:29
it collides and for that we need from
00:09:32
control the if then block and here we
00:09:36
are going to check if our basket is
00:09:38
colliding with the ball okay so go to
00:09:42
basket and
00:09:44
use this procedure call
00:09:48
basket dot colliding with and the other
00:09:51
hair is the ball so if I go to ball
00:09:54
I can get its component its exact down
00:09:57
at the end
00:09:59
so if the basket is colliding with the
00:10:01
ball we are first of all going to reset
00:10:04
our ball
00:10:05
so call the procedure reset ball
00:10:10
and we're also going to clear up our
00:10:12
canvas because we are going to redraw
00:10:14
the new score
00:10:16
now we have to get the new score
00:10:19
so set Global score to a plus
00:10:24
of
00:10:25
whatever is inside the score at that
00:10:28
time
00:10:29
Plus
00:10:32
one
00:10:34
okay
00:10:36
and then we are going to draw on the
00:10:38
canvas so if I go to Canvas I can get
00:10:41
this draw
00:10:43
text procedure and here our text is
00:10:47
basically a join so go to text and get a
00:10:50
join
00:10:52
and
00:10:53
the first block in our join is just
00:10:58
score colon space
00:11:01
and then
00:11:03
I can duplicate the
00:11:06
getter for score here
00:11:09
and we want our score to be drawn
00:11:12
exactly in the middle of the canvas so
00:11:15
for that I am going to give it the
00:11:17
canvases width divided by 2 so I can
00:11:22
duplicate from here bring it down
00:11:25
duplicate again
00:11:28
and
00:11:31
make it
00:11:32
hide okay
00:11:38
now our reset ball procedure this one
00:11:42
which sets the speed to zero and give it
00:11:45
its starting position
00:11:47
we should call it when the screen is
00:11:50
initialized
00:11:51
so
00:11:54
call the procedure reset ball and what
00:11:58
about the situation in which the ball
00:12:00
hits the edges okay so if it hits the
00:12:04
top Edge or the bottom Edge we should
00:12:06
reset it then too and in case of right
00:12:09
or left Edge we are just going to make
00:12:12
it bounce okay so going back to blocks
00:12:16
so from ball get its Edge reached event
00:12:20
let me clean up the blocks
00:12:23
so this is The Edge reached event and we
00:12:27
are going to check for the edge so from
00:12:30
control get the if then else block and
00:12:34
from logic
00:12:36
get the or block plug it in the if
00:12:40
and again from logic get the equal to
00:12:44
block
00:12:45
and
00:12:46
if our Edge
00:12:49
is equal to 1
00:12:54
or
00:12:55
let's duplicate this one
00:13:01
is equal to minus 1
00:13:03
remember I duplicate by right clicking
00:13:07
on the Block Okay so
00:13:10
if it is top or bottom I am going to
00:13:14
call my procedure reset ball and in the
00:13:20
other situation that is right or left
00:13:21
Edge I'm just going to call
00:13:25
my ball to bounce and give it the edge
00:13:30
okay so the edge is here hover over it
00:13:35
Okay so
00:13:37
the ball should bounce now this is done
00:13:40
I hope you have fun making this simple
00:13:42
but cool game basketball game in MIT App
00:13:45
Inventor please like my video and if you
00:13:48
haven't subscribed to my channel kindly
00:13:50
do so so that you don't miss any of the
00:13:53
great projects that I've planned for you
00:13:54
thank you for watching this video have a
00:13:57
good day and goodbye