使用C#实现一个石头剪刀布游戏
创始人
2024-11-16 13:21:21
0次

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 石头剪刀布
{
  public partial class Form1 : Form
  {
    int computer_sorce = 0;
    int user_sorce = 0;
    int i=0,j = 0;
    int last_step = -1;//0表示平局,1为电脑上步加一,2为用户上步加一
    public Form1()
    {
      InitializeComponent();
      this.Text = "石头剪刀布";
      label3.Text = ":";
      label1.Visible = false;
      label2.Visible = false;
      label3.Visible = false;
      label4.Visible = false;
      button1.Text = "开始游戏";
      button2.Text = "重新开始";
      button3.Text = "悔棋";
      button4.Text = "退出";
      button2.Visible = false;
      button3.Visible = false; 
      button4.Visible = false;
      pictureBox2.Visible = false;
      pictureBox3.Visible = false;
      pictureBox3.Visible = false;
      pictureBox4.Visible = false;
      pictureBox5.Visible = false;
      pictureBox6.Visible = false;
      pictureBox7.Visible = false;
      pictureBox8.Visible = false;
      pictureBox9.Visible = false;
      pictureBox10.Visible =false;
      pictureBox11.Visible = false;
    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private void button1_Click(object sender, EventArgs e)
    {
      pictureBox1.Visible = false;
      button1.Visible = false;
      button1.Enabled = false;
      pictureBox2.Visible = true;
      pictureBox3.Visible = true;
      pictureBox4.Visible = true;
      pictureBox5.Visible = true;
      pictureBox6.Visible = true;
      pictureBox7.Visible = true;
      pictureBox8.Visible = true;
      pictureBox9.Visible = true;
      pictureBox10.Visible = true;
      pictureBox11.Visible = true;
      button2.Visible = true;
      button3.Visible = true;
      button4.Visible = true;
      
    }
    private void game_computer()
    {
      Random rand = new Random();
      i = rand.Next(4);
      if (i == 1)
      {
        pictureBox10.Load("..\\..\\picture\\1.jpg");
      }
      else if (i == 2)
      {
        pictureBox10.Load("..\\..\\picture\\2.jpg");
      }
      else if(i == 3)
      {
        pictureBox10.Load("..\\..\\picture\\3.jpg");
      }
    }
    private void game_judgment()
    {
      if (i == 1)//电脑出石头
      {
        if (j == 1)
        {
          last_step = 0;
        }
        else if (j == 2)//用户出剪刀
        {
          computer_sorce++;
          last_step = 1;
        }
        else if (j == 3)//用户出布
        {
          user_sorce++;
          last_step = 2;
        }
      }
      else if (i == 2)//电脑出剪刀
      {
        if (j == 1)//用户出石头
        {
          user_sorce++;
          last_step = 2;
        }
        else if (j == 2)
        {
          last_step = 0;
        }
        else if (j == 3)//用户出布
        {
          computer_sorce++;
          last_step = 1;
        }
      }
      else if (i == 3)//电脑出布
      {
        if (j == 1)//用户出石头
        {
          computer_sorce++;
          last_step = 1;
        }
        else if (j == 2)//用户出剪刀
        {
          user_sorce++;
          last_step = 2;
        }
        else if (j == 3)
        {
          last_step = 0;
        }
      }
      if (computer_sorce == 2)//当电脑分数到2时,显示you lose
      {
        label1.Visible = true;
        pictureBox7.Enabled = false;//结果出来后三个图片按钮失效
        pictureBox8.Enabled = false;
        pictureBox9.Enabled = false;
        label1.Text = "YOU LOSE!";
      }
      else if (user_sorce == 2)//当用户分数到2时,显示you win
      {
        label1.Visible = true;
        pictureBox7.Enabled = false;
        pictureBox8.Enabled = false;
        pictureBox9.Enabled = false;
        label1.Text = "YOU WIN!";
      }
    }
    private void button4_Click(object sender, EventArgs e)//退出按钮
    {
      this.Close();
    }
    private void pictureBox7_Click(object sender, EventArgs e)//用户石头图片
    {
      j = 1;
      pictureBox10.Visible = true;//划拳图片显示正常
      pictureBox11.Visible = true;
      label2.Visible = true;//计分器三个label显示
      label3.Visible = true;
      label4.Visible = true;
      game_computer();
      game_judgment();
      label2.Text = computer_sorce.ToString();
      label4.Text = user_sorce.ToString();
      pictureBox11.Load("D:\\newdesktop\\1.jpg");
    }
    private void pictureBox8_Click(object sender, EventArgs e)//用户剪刀图片
    {
      j = 2;
      pictureBox10.Visible = true;//划拳图片显示正常
      pictureBox11.Visible = true;
      label2.Visible = true;//计分器三个label显示
      label3.Visible = true;
      label4.Visible = true;
      game_computer();
      game_judgment();
      label2.Text = computer_sorce.ToString();
      label4.Text = user_sorce.ToString();
      pictureBox11.Load("D:\\newdesktop\\2.jpg");
    }
    private void pictureBox9_Click(object sender, EventArgs e)//用户布图片
    {
      j = 3;
      pictureBox10.Visible = true;//划拳图片显示正常
      pictureBox11.Visible = true;
      label2.Visible = true;//计分器三个label显示
      label3.Visible = true;
      label4.Visible = true;
      game_computer();
      game_judgment();
      label2.Text = computer_sorce.ToString();
      label4.Text = user_sorce.ToString();
      pictureBox11.Load("D:\\newdesktop\\3.jpg");
    }
    private void button2_Click(object sender, EventArgs e)
    {
      pictureBox7.Enabled = true;//重新开始后三个图片按钮生效
      pictureBox8.Enabled = true;
      pictureBox9.Enabled = true;
      i = 0;
      j = 0;
      computer_sorce = 0;
      user_sorce = 0;
      label2.Text = computer_sorce.ToString();
      label4.Text = user_sorce.ToString();
      pictureBox10.Visible = false;
      pictureBox11.Visible = false;
      label1.Visible = false;
      label2.Visible = false;
      label3.Visible = false;
      label4.Visible = false;
    }
    private void button3_Click(object sender, EventArgs e)
    {
      label1.Visible = false;
      pictureBox7.Enabled = true;
      pictureBox8.Enabled = true;
      pictureBox9.Enabled = true;
      pictureBox10.Visible = false;
      pictureBox11.Visible = false;
      if (last_step == 0)
      {
      }
      else if (last_step == 1)
      {
        computer_sorce--;
        label2.Text = computer_sorce.ToString();
        label4.Text = user_sorce.ToString();
      }
      else if (last_step == 2)
      {
        user_sorce--;
        label2.Text = computer_sorce.ToString();
        label4.Text = user_sorce.ToString();
      }
      last_step = -1;
    }
  }
}

效果图

开始游戏

使用C#实现一个石头剪刀布游戏

点击开始

使用C#实现一个石头剪刀布游戏

划拳

使用C#实现一个石头剪刀布游戏

判定胜败

使用C#实现一个石头剪刀布游戏
使用C#实现一个石头剪刀布游戏

重新开始

使用C#实现一个石头剪刀布游戏

悔棋

使用C#实现一个石头剪刀布游戏
使用C#实现一个石头剪刀布游戏

退出

使用C#实现一个石头剪刀布游戏

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...