VB: Rock Paper Scissors – Part 2

| May 7, 2012 | 0 Comments

Here’s how to add additional functionality to the VB Rock Paper Scissors game.

how to make a game part two

In an earlier post I showed you how to use Visual Basic to create the bare bones of a Rock Paper Scissors game. Now I’ll show you how to add some additional features to it.

When you start adding things to a program take care because it is possible to end up breaking the program rather than improving it. Start by making a list of the options you want to add to the program and then add them one or two at a time. Test the program regularly as you go to make sure it is behaving as expected.

For my game I’ll add options that allow you to play one round, three, five or an unlimited number of rounds. If the user opts to play an unlimited number of rounds they will be offered a checkbox to click when they’re ready to finish. The options will be offered using four radio buttons positioned inside a groupbox. The checkbox can be placed anywhere on the screen. I’ll also add a scoring system to report the final game score. This will require some additional variables to manage the number of rounds to be played, the number already played and variables for computer and player scores.

Once the player has made their choice from the radio buttons, these will be hidden from view. Behind the scenes, the program will maintain the player score and the number of rounds currently played.

Once the required number of rounds have been played or where the player opts to finish an unlimited game, the new finishGame subroutine is called. This calculates who has won and displays the scores in two new labels (label4 and label5). Once this is done, the rounds_played variable is set to -2 to indicate a new game and the button text is changed to read Play Again.

The new resetGame subroutine redisplays the hidden radio buttons and the groupbox, it hides the checkbox, resets the player’s score and the rounds_played counter. It also hides the labels and changes the main button text back to Play. Once the player has chosen the number of rounds to play and their starting Rock/Paper or Scissors, the process starts over again.

New form controls

To add these features to your project, add four radio buttons inside a groupbox. Also add a checkbox and set its visibility to false. Add two additional labels, label4 and label5 and set their text properties to nothing.

part 2 step 2 making a game

This code manages all the radio buttons:


Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged,
RadioButton2.CheckedChanged, RadioButton3.CheckedChanged,
If RadioButton1.Checked Then
rounds_to_play = 1
ElseIf RadioButton2.Checked Then
rounds_to_play = 0 ' this is the code for unlimited
ElseIf RadioButton3.Checked Then
rounds_to_play = 3
ElseIf RadioButton4.Checked Then
rounds_to_play = 5
End If
rounds_played = -1
End Sub

These are the new finishGame and resetGame subroutines:

Sub resetGame()
RadioButton1.Visible = True
RadioButton2.Visible = True
RadioButton3.Visible = True
RadioButton4.Visible = True
GroupBox1.Visible = True
CheckBox1.Visible = False
CheckBox1.Checked = False
player_score = 0
rounds_played = -1
Button1.Text = "Play"
Label3.Text = ""
Label4.Text = ""
Label5.Text = ""
End Sub

making a game adding code

Private Sub finishGame()
If rounds_played - player_score > player_score Then
Label3.Text = "Computer wins. It scored: " & rounds_played - player_score & 
" out of " & rounds_played & " rounds."
ElseIf rounds_played - player_score = player_score Then
Label3.Text = "Game is a draw " & player_score & 
" rounds each."
Label3.Text = "You win! You scored: " & player_score & " out of " & rounds_played
 & " rounds."
End If
rounds_played = -2 'new game trigger
Button1.Text = "Play Again"
End Sub

Add these variables to the Form Declarations area:

Dim rounds_played As Integer
Dim rounds_to_play As Integer
Dim player_score As Integer
Dim computer_score As Integer
Dim resultMessage As String

making a gameAdd this code to the beginning of the Button1_click event:

If rounds_played = -2 Then
Exit Sub
End If
If rounds_played = -1 Then
RadioButton1.Visible = False
RadioButton2.Visible = False
RadioButton3.Visible = False
RadioButton4.Visible = False
GroupBox1.Visible = False
If RadioButton2.Checked Then CheckBox1.Visible = True
player_score = 0
rounds_played = 0
End If
Label4.Text = "Round #: " & rounds_played + 1

And add this code to the end of the Button1_click event replacing the previous Select Case gameResult code:

Select Case gameResult
Case 0 ' Draw
resultMessage = "It's a draw, play again."
Case 1 ' computer wins
rounds_played = rounds_played + 1
resultMessage = "Computer won that round"
Case 2 ' player wins
player_score = player_score + 1
rounds_played = rounds_played + 1
resultMessage = "You won that round"
End Select
If (rounds_to_play > 0 And rounds_played = rounds_to_play) Then
Call finishGame()
ElseIf CheckBox1.Checked = True Then
Call finishGame()
End If
Label5.Text = resultMessage

Save your file and test it.

Click here to download a file containing all the code for the project.


Tags: , , , , , , , , ,

Category: Visual Basic

About the Author ()

Leave a Reply