Slot Machine Tutorial
Slot Machine Tutorial
Slot Machine Tutorial
In this tutorial we will discuss and show how to create a very simple slot machine game using visual
studio and visual basic programming language. If you read some of the other Visual Basic we have
discussed that visual basic has many different aspects of it one of which is game programming.
Visual Basic is a easy program to learn and syntaxes are similar to other programming languages.
Now let's get started.
If you want to create an identical one to mine then download the assets from www.mooict.com and
you can import those images as resource to the windows form.
This is what our final product will look like. Check out the
Demo video to see the full functionality of the game itself.
Don't just drag 3 of them there. Here is an idea drag 1 to the form lets change some properties add
some resources and then copy and paste them twice so we won't have to do it for the rest. Make life
easy for yourself.
Once you have imported the files. Now we need to change a few things in the properties window
before moving on to copy and paste. Hold on for a second itchy fingers.
Now you can copy and paste them Check the image below.
I will go over the game rules in a short while. Let's get the GUI sorted out first.
Click on a label and you will see the font property in the
properties window.
Click on that and it will give you the option to change the font style for the labels.
Also you can change the colour of the text, look for the fore color option in the properties window.
its under the font option.
If you can't find the toolbox at any point in the tutorial refer back to the View -> Toolbox
Here it is.
Drag it to the form and resize it to your liking. Since the button is the main interaction with the use I
will make mine a little bigger. You can change the font and colour of the button if you choose.
We need to notify the player what they won or lost. So we will Add one last label to the form.
Now we still have one more thing to add which is of teeny tiny importance the TIMER.
Lets move the timer to the form and look at the properties setting
Change the properties to the following name to gameTimer so we can easily understand it. Interval
to 1000. In programming we run things in milliseconds which is 1000 = 1second. Good to know right.
So now, we have all the necessary GUI in order time to discuss the matter of the actual code.
Game Logic
We all know how slot machines work and what they do. However it's time to think like an engineer.
We are creating a slot machine we are creating a virtual simulation of one.
Here are the rules of our game. Now we just need to adjust the code to run this. We won't need
anything fancy to program just plain old simple visual basic.
Thing to remember. All the we are doing goes inside the class which means nothing goes outside
that END CLASS line.
We will cover the CLASS system in another tutorial for now let's get this thing working.
End Class
Here we have added some variables for this game. We need these to check what the outcome of
the game will be and how we can use those to either deduct the money or reward the player.
Money variable will hold the players money in the beginning of the game.
result will contain what the player won or lost in the game
counter will be used with the timer to help us simulate a animation for the slot machine
cost is how much we are going to charge the player for each round
Now lets start adding some events to this game. Out first component for interactivity is the button
on stage. double click on the button.
Visual studio will automatically a function for that button. So each time its clicked it will trigger this
function.
End Sub
End Class
In visual basic the SUB means a function so we start with a sub and end the sub to determine where
it starts or ends.
End If
End Sub
Now we have given the Slot A B C a value of one. Just for testing purposes at the moment.
There is a IF statement which is checking if the amount of money is less than the cost then what do
we do. In any game the most important thing is to end the game and know when to quit. So as long
as the player has more than the cost per round they can play otherwise we don't let them. Strict
rules but hey why not.
We will be creating a nesting if statements inside this one remember where to start and end.
End If
End Sub
Lets take another look at the newly added code on the screen.
Label 6 is the status label we added last we are giving feedback to the user about what happened.
Game timer is being stopped so it doesn't run the timer function which we will add to the game
soon.
Notice we added a small ELSE end of the if statement before the END IF. All out game logic where
the player has the money to play each round will go inside this.
Here we are going to add some reset variable to the else statement.
we are starting the game timer by using the START() function which is embedded into the timer
itself. Its the same as STOP() used in the timer.
Finally we will disable the button. We are doing this so the player cannot cheat by pressing the
button multiple times. One click each round.
End If
End If
Here we have a nested IF statement. Now we will check if all the three variable contain the same
value. Is that is true then we can reward user with £10 and add that money to their money variable.
Look closely at that IF statement first we are checking if slot A and B are the same then we are
checking if Slot B and C are the same. If they are its a Jackpot. Now you can change the reward if you
wish but lets complete this game first.
End If
End If
Now it's time to check if TWO of the slots are the same. We have seen how to use a AND condition
in the IF statement here is the perfect example of OR. Or does what it says on the box it checks a
condition or another condition or another to see which matches and takes the appropriate action
from it.
Here we need to find a match for only two out of three so that opens up a new issue however by
using we can simply check between the slots which are matching with others.
If A is equals to B or is A is equals to C or is B equals to C then we give the user £1 reward for the
effort and add that to their money variable.
There is nothing more to add to this button 1 function for now. However We will come back to it
once we added our next function.
its begins just as any function Private Sub changeImageA Then inside it you can see we are
requesting two different variables. first one is pp as PictureBox since we have 3 different picture
boxes to control on the form we can use this one function to control them 3. Next variable is num as
integer this will pass the number we have stored in each slot and change the images appropriately. If
you are wondering about the ByVal keyword before each variable in the argument this was added
automatically by visual basic. If yours not doing that then just add it yourself. It simply implies that
we will use the values from each variables in the function. Well duuh right.
This line of code works similar to the if statement of switch statement from C#. In visual basic we use
the Select Case. This select condition starts with a case, the case of what if inside of num variable.
We will manually write what to do in each case.
Case 1
pp.Image = My.Resources.apple
If the number is 1 then we change the pp picture box to the apple image from our resources. Now
you might be wondering what's this PP picture box and why am I writing the code for it. Well all will
be cleared soon enough. Just follow along for now.
We do the same thing for each case up to 7 and also we put a case in for 0 which is out animated GIF
in the resources folder.
Don't forget to end the select condition before the function ending.
End Sub
counter = counter + 1
If counter = 1 Then
changeImageA(PictureBox1, slotA)
ElseIf counter = 3 Then
changeImageA(PictureBox2, slotB)
ElseIf counter = 5 Then
changeImageA(PictureBox3, slotC)
gameTimer.Stop()
Label6.Text = money
Button1.Enabled = True
Label6.Text = "you won £" + Str(result)
Label3.Text = money
End If
End Sub
first we will call counter variable and increase it by one. We are going to increase it by checking the
amount in the counter and adding 1 on top of that.
Now we will check the values in the counter. If the value is one then we will call that function we
created earlier.
See how we are calling it. by using one function we can change all 3 picture boxes.
changeImageA(PictureBox1, slotA)
inside the change image A function we are doing the following changeImageA (name of the picture
box , number which the select statement will respond to.)
Now its time to put some final touch to the program and it will be working flawlessly.
Add these 3 change image functions to the button 1. Once the button is clicked we will play the
animation before we set it up. Run it now and see how it works.
Now the final ingredient to the program. Once again it will be residing inside the button 1 function.
Remember that Dim rn As New Random line in the program earlier. We will use this random class to
generate a random number between 1 and 7 and each will be residing inside the button 1 function
from there we can create a unpredictable change and miss game.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
slotA = rn.Next(1, 7)
slotB = rn.Next(1, 7)
slotC = rn.Next(1, 7)
look at the slot A B and C we are using rn.next(1 to 7) line to generate a random number between 1
and 7 and then we will pass the value to our timer which will animate each picture box.
There we played 3 rounds of the game and we only won £1 huh its closer to a real slot machine lol.
You can research to see if you can add sound to each time the image is loaded. That will add a good
auditory response for the user. You can have a each round end sound and a final sound for different
wins.
We came a long way in this tutorial. Make sure to have fun with all this.