Making a Rock Paper Scissors Game with Visual Studio C# Form Application

Making a Rock Paper Scissors Game with Visual Studio C# Form Application

Hello friends, in this post I will show you how to create the rock paper scissors game in Visual Studio Form Application using C#.

Game features
1 Player - Against Computer
2 Player - 2 Friends Can Play

Elements I Used in the Project

  • 5 buttons
  • 6 labels
  • 3 panels
How the game should look when it ends


First, let's make the start part of the game, that is, the 1st panel.
First, we place a single 420x300 sized panel and put 2 buttons in the middle.
With these buttons, we let the user select whether they will play against the computer or a friend.

The codes for our buttons will be as follows

1 Player Button

panel1.Visible = false;
panel2.Visible = true;

2 Player Button

panel1.Visible = false;
panel3.Visible = true;

Then, to make the part for playing against the computer, add the 2nd panel and add the elements as shown in the image above, but don't forget, there is an extra label between the score and play against computer texts to tell the user who won.
I named the label giving the info as tek_oyuncu_bilgi, and the one showing the score as tek_oyuncu_puan.
I kept a universal int value called single_score where I stored the user's score.

Now, let's write a function to determine how someone wins.
        
 public int single_score = 0;
 public void single_tas_kagit_makas(int secim_user)
        {
            Random rand = new Random();
            int secim_pc = rand.Next(0, 2 + 1);

            string[] rsp = { "Rock", "Paper", "Scissors" };

            // Rock = 0 Paper = 1 Scissors = 2
            if (secim_user == 0 && secim_pc == 0 || secim_user == 1 && secim_pc == 1 || secim_user == 2 && secim_pc == 2)
            {
                tek_oyuncu_bilgi.Text = "It's a draw" + "\n Computer : " + rsp[secim_pc] + "\n You : " + rsp[secim_user];
            }
            else if (secim_user == 0 && secim_pc == 2 || secim_user == 1 && secim_pc == 0 || secim_user == 2 && secim_pc == 1)
            {
                tek_oyuncu_bilgi.Text = "You won" + "\n Computer : " + rsp[secim_pc] + "\n You : " + rsp[secim_user];
                single_score++;
                tek_oyuncu_puan.Text = "Score : " + single_score.ToString();
            }
            else
            {
                tek_oyuncu_bilgi.Text = "You lost" + "\n Computer : " + rsp[secim_pc] + "\n You : " + rsp[secim_user];
                single_score--;
                tek_oyuncu_puan.Text = "Score : " + single_score.ToString();
            }

        }

We received user information in a way the computer can understand, and let the computer make a random choice.
In this function, we called the integer values 0 1 2 as rock paper scissors so the computer could understand, and defined who wins and who loses using comparison operators.
Yes, friends, after this for the single player part all that is left is to assign this function to the buttons to execute it.

For the button labeled Rock

  single_tas_kagit_makas(0);

For the button labeled Paper

  single_tas_kagit_makas(1);

For the button labeled Scissors

  single_tas_kagit_makas(2);

We assigned these codes and called our function, thus the single player part is finished, now let's do the multiplayer part.
For the multiplayer system, I created a system that is played by pressing keys on the keyboard. Otherwise, it would be visible what each person pressed, but in this way, the 1st player uses a,s,d, and the 2nd player uses 1,2,3 keys to make their choice quickly, or rather, simultaneously, so it can be played multiplayer.

For multiplayer, add a 3rd panel and design the appearance similar to above. Don't forget to add the info label.
Let's move to the code.
When the application first opens, we need to enter the command "this.KeyPreview = true" to track user keyboard inputs.

 public Form1()
        {
            this.KeyPreview = true;

            InitializeComponent();
        }

Now, we need to write a function to detect the competition between users. Take the one we used for the computer, add an extra int and use the user-supplied values instead of the computer value.

      public void  multiplayer_tas_kagit_makas(int user1, int user2)
        {           

            string[] rsp = { "Rock", "Paper", "Scissors" };

            // Rock = 0 Paper = 1 Scissors = 2
            if (user1 == 0 && user2 == 0 || user1 == 1 && user2 == 1 || user1 == 2 && user2 == 2)
            {
                ark_bilgilendirme.Text = "It's a draw" + "\n 1st Player : " + rsp[user1] + "\n 2nd Player : " + rsp[user2];
            }
            else if (user1 == 0 && user2 == 2 || user1 == 1 && user2 == 0 || user1 == 2 && user2 == 1)
            {
                ark_bilgilendirme.Text = "1st Player Won, 2nd Player Lost" + "\n 1st Player : " + rsp[user1] + "\n 2nd Player : " + rsp[user2];

                ark_puan.Text = "Score : " + single_score.ToString();
            }
            else
            {
                ark_bilgilendirme.Text = "1st Player Lost, 2nd Player Won" + "\n 1st Player : " + rsp[user1] + "\n 2nd Player : " + rsp[user2];

                ark_puan.Text = "Score : " + single_score.ToString();
            }

        }


There is no need to explain the code here, as I already explained what we did for the computer above.

Now let's handle detecting the values entered from the keyboard.

First, we create universal integer values to capture which key each player presses.

 public int oyuncu_1 = -1, oyuncu_2 = -1;


I set the value to -1 because I don't want to show the result before both players make their selections, and we'll control it that way. You'll understand when you look at the code below.
Now we can capture the keyboard and process inputs.

        private void Key_Press(object sender, KeyPressEventArgs e)
         {
           
            if(e.KeyChar.ToString() == "a")
            {
                oyuncu_1 = 0;
            }
            else if(e.KeyChar.ToString() == "s")
            {
                oyuncu_1 = 1;
            }
            else if (e.KeyChar.ToString() == "d")
            {
                oyuncu_1 = 2;
            }

            if (e.KeyChar.ToString() == "1")
            {
                oyuncu_2 = 0;
            }
            else if (e.KeyChar.ToString() == "2")
            {
                oyuncu_2 = 1;
            }
            else if (e.KeyChar.ToString() == "3")
            {
                oyuncu_2 = 2;
            }


            if (oyuncu_1 != -1 && oyuncu_2 != -1)
            {
                multiplayer_tas_kagit_makas(oyuncu_1, oyuncu_2);
                oyuncu_1 = -1;
                oyuncu_2 = -1;
            }

        }


Let me explain what we did here immediately.
When the a,s,d keys are pressed on the keyboard we assign rock paper scissors to player 1.
For keys 1,2,3 we assign choices to player 2.
Then, if both players have made their choices, we call the function we wrote for comparison and show the results to the users.
After the process is finished, we reset the user data, or rather set it to -1, so that they're unselected, and they can play again, even consecutively.

While capturing the keyboard here, the most important point is not to forget to assign the function you wrote for the KeyPress event from within the Form. (The image also shows Key_Press_Up, but you don't need to assign those, just Key_Press is enough.)


You can access the project I made via GitHub.
GitHup - Urhoba Rock Paper Scissors

Screenshots from the Game