Visual Basic 2010: Rock Paper Scissors

| May 7, 2012 | 1 Comment

Learn some simple game logic and find out how to create this simple game.

making a game with visual basic

When you’re learning to program in a language like Visual Basic one interesting way to extend your skills is to recreate a game as a VB program. Here I’ll discuss the game: Rock Paper Scissors. I’ll discuss the logic that you would use to design and code the game and then I’ll show how to build the game itself.

The concept of Rock Paper Scissors if it is played against the computer is that you will make a choice of one of the three options, rock, paper and scissors. When you make your choice, the computer will make its choice and then the result will be determined. If you and the computer choose the same object, neither wins. If you choose rock and the computer chooses scissors, the computer wins. Paper beats Rock and Scissors beats Paper.

Computers aren’t smart enough to choose to play rock, paper or scissors but they can calculate a random number (say between 0 and 2) so you can make the random number represent rock, paper or scissors – 0 for rock, 1 for paper, 2 for scissors.

Your game will need a combobox from which you can make your choice, you’ll need to know what the computer chose and you’ll need to know the game result.

I’ll use picture boxes to give the game some visual appeal and I’ll source some images from the Microsoft online clip art collection. You will need three images all the size and save saved as rock.jpg, paper.jpg, scissors.jpg and rps.jpg (a composite image used to ‘hide’ the computer’s choice) while you make your choice.

making a game step 1

Get Started

Launch Visual Basic Express 2010, start a new project and choose a Windows Form. Add two picture boxes, two buttons, three labels and a combobox. Set the collection for the combobox to Rock, Paper and Scissors and set it’s AllowDrop property to true.

The buttons are labeled Play and Cancel.

Label1 and Label2 have their text property set to You and Computer respectively.

Label3 has no entry in its text property – this will be set programmatically.

To add the images to the resource file, select the Solution Explorer and click My Project, Resources.resx. Choose Add Resource > Existing Itemand import all four images.

making a game step 2

Set the image property of both pictureboxes to the image rps.jpg which will be in your Project Resource file.

making a game step 3

This is the code required to run the program:

 

Public Class Form1
   Dim userSelected As Integer
   ' gameResult: 0-draw, 1-computer wins, 2-user wins
   Dim gameResult As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rand As New Random()
Dim computerChoice = rand.Next(3)
If computerChoice = userSelected Then gameResult = 0
Select Case computerChoice
Case 0
' computer chooses Rock / user chooses Paper - user wins
If userSelected = 1 Then gameResult = 2
' computer chooses Rock / user chooses Scissors - computer wins
If userSelected = 2 Then gameResult = 1
PictureBox2.Image = My.Resources.rock
Case 1
' computer chooses Paper / user chooses Rock - computer wins
If userSelected = 0 Then gameResult = 1
' computer chooses Paper / user chooses Scissors - user wins
If userSelected = 2 Then gameResult = 2
PictureBox2.Image = My.Resources.paper
Case 2
' computer chooses Scissors / user chooses Rock - user wins
If userSelected = 0 Then gameResult = 2
' computer chooses Scissors / user chooses Paper - computer wins
If userSelected = 1 Then gameResult = 1
PictureBox2.Image = My.Resources.scissors
End Select
Select Case gameResult
Case 0
Label3.Text = "Game is a draw"
Case 1
Label3.Text = "Bad Luck! Computer wins"
Case 2
Label3.Text = "You win!"
End Select
End Sub

Private Sub ComboBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus
PictureBox2.Image = My.Resources.rps
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
userSelected = ComboBox1.SelectedIndex
Select Case userSelected
Case 0
PictureBox1.Image = My.Resources.rock
Case 1
PictureBox1.Image = My.Resources.paper
Case 2
PictureBox1.Image = My.Resources.scissors
End Select
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex = 0
End Sub
End Class

The code in the ComboBox1_SelectedIndexChanged event displays an image matching the choice you made in the ComboBox and the Close button exits the program.

Most of the work takes place in the Button1_Click event. A random number between 0 and 2 is created which is used to determine the computer’s ‘choice’. Then the result is calculated. If you and the computer make the same choice, the game is a draw. Otherwise the Select Case statement checks the choices and an integer representing the result is stored in the variable GameResult.

The code then looks at the value in GameResult and displays corresponding text indicating who won in Label3.

Here is a link to the file code.txt which contains the code above.

Tags: , , , , , , , ,

Category: Visual Basic

About the Author ()

Comments (1)

Trackback URL | Comments RSS Feed

  1. no name says:

    not easy to undertsand

Leave a Reply