welcome to Developer-pk in this one
we're gonna make a classic it's a random
number generator and what we're gonna do
is turn that into a dice rolling app
let's get started so this is this the
final app this we should have moving
pictures
you should have sound effects and if you
listen carefully you can hear the motor
buzzing as well let's get started by
adding in a button drag and drop it onto
your screen choose whatever colors you
like and we're just going to use some
text to show the numbers before we move
on and do any pictures in the blocks
then what's gonna happen is the user
will come along they'll click the button
and what we're gonna do is we're gonna
change that text property for the moment
and we're gonna have a display a random
number so if it's a standard die we're
looking numbers between 1 & 6 so the
next thing we're gonna do is add in our
sound component and upload our sound
file if you're unsure what component you
want to use there's little question
marks beside it and they give you a
little tooltips and what's happening
back in our code all we have to do is
add in the sound wound up play and if
you like you can use san1 fiber it as
well putting in a value here in
milliseconds so 500 milliseconds all
that means is that error and motor is
gonna pause for half a second the final
thing we'll do then to set up our app is
set the source for sound one that's
going to be our wav file in this case.
So
I had a few of them like dice 1 and dice
2 - WAV but again just make sure that
you've got the correct file extension
there so to improve the functionality
rather than just carrying one random
number we want to spit out a whole load
different numbers just like I dice
rolling we're gonna add in the clock
component and then we're gonna create a
variable our app so I've called this
spin counter it's gonna keep track of
where it's gonna count the number of
spins the die is done I will start
counting ash as 0 or 1 doesn't really
make a huge difference here and every
time our clock goes off we are going to
put essentially display another random
number on the + button for the user so
when the user clicks that the die clicks
the button we're gonna change this now
to set the timer enable to be true and
then once we've got to a certain point
in their app we're going to
get the timer to stop counting.
So where
is that predefined point what we'll do
is we'll check has a spawn at ten times
so what we'll do we'll check is greater
than or equal to ten so then just grab
our value here and spin counter pop into
ten there everything should be more or
less ready to go so now what we want to
do is one increment or one to increase
our spin counter every time the clock
ticks this is very standard kind of
thing to do we're gonna set it to spin
counter plus one so one counter one spin
at a time every time then the dice is
spinning around what we'll do is we have
changed this one here the button text to
be another random number is fine for the
moment again so we should see just a
whole series of changing numbers the
last thing we want to do is add in our
images what I've done is I've used
similar names for all six sides of my
die dice - and then the number of spots
that will be Amish over in our app what
we're going to do is to import or load
in these images we're gonna join
together at the word dice and the symbol
- at the beginning we're put in a random
number so it doesn't go through them in
any particular order.
So it could be dice
one choice three toys to any any order
we want and at the end then we put in
our file extension or file type which is
dot PNG so if you want to save yourself
a little bit of time it's sometimes a
good idea to duplicate your blocks just
by right-clicking on them and choosing
duplicate so we could reuse it here and
duplicate it again and display a new
image when the dice is spinning and here
in our clock one timer event but if
you're observant you'll notice that
being kind of lazy like this it's also
error-prone because what I've done is
I've used text property instead of the
image so not too hard we don't get three
changes to make here it's not not a big
deal and at this stage now everything is
up and running and should work just like
we saw at the beginning of the app all
I'm noticing there is that we've got a
little bit of repetition so we're using
those exact same four blocks in three
different locations
to make our lives a little bit easier to
make our cold easier to maintain
maybe if we want to use different types
of dyes may be done with 12 phases what
we can do is put this all into a
procedure we'll call it random image and
then we just use the random image block
three times here and then in our screen
initialize event as well so when we're
finished with the join block just delete
them there's no need for them anymore
and that's it that's the whole thing
done it still works the exact same way
it just means the code is a little bit
easier to read it's a little bit easier
to update and this would generally
consider kind of good coding practice so
the whole thing then when we tested it
works perfectly we're able to change
different random numbers play their
sound effect gets the motor dip was a
little bit as well if you liked the
video hit the thumbs up and don't forget
to subscribe for more and I will see you
in the next Post.
0 Comments