猜拳(石头剪刀布)

本文介绍了一个使用WinForm开发的简单猜拳游戏。通过随机数生成器模拟电脑出拳,利用文本框输入玩家的选择,并通过图片显示每轮游戏的胜负结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天用winform做了个猜拳游戏。

拖完控件的摸样:

刚打开是的摸样:

 

 

随便选个出拳之后:

 

平的时候:

 

具体的代码:

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

namespace 练习3_猜拳
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /*************************系统出拳**********************************/

        private void textBox1_TextChanged(object sender, EventArgs e)
        {  
            string[] chuquanzhonglei = new string[3] { "石头", "剪刀", "布" };
            Random Roll = new Random();
            int roll = Roll.Next(0, 3);
            textBox2.Text = chuquanzhonglei[roll];
        }
        /*************************自己出拳**********************************/
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null) {
                textBox1.Clear();  
            }
            textBox1.Text = "石头";
            puanduan();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null)
            {
                textBox1.Clear();
            }
            textBox1.Text = "剪刀";
            puanduan();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null)
            {
                textBox1.Clear();
            }
            textBox1.Text = "布";
            puanduan();
        }

        /*************************判断胜负**********************************/
        public void puanduan() {
            if (textBox1.Text == "石头" && textBox2.Text == "剪刀")
            {
                pictureBox1.Image = Image.FromFile("胜.jpg");
                pictureBox2.Image = Image.FromFile("负.jpg");
            }
            else if (textBox1.Text == "剪刀" && textBox2.Text == "布")
            {
                pictureBox1.Image = Image.FromFile("胜.jpg");
                pictureBox2.Image = Image.FromFile("负.jpg");
            }
            else if (textBox1.Text == "布" && textBox2.Text == "石头")
            {
                pictureBox1.Image = Image.FromFile("胜.jpg");
                pictureBox2.Image = Image.FromFile("负.jpg");
            }
            else if (textBox1.Text == "布" && textBox2.Text == "布")
            {
                pictureBox1.Image = Image.FromFile("平.jpg");
                pictureBox2.Image = Image.FromFile("平.jpg");
            }
            else if (textBox1.Text == "石头" && textBox2.Text == "石头")
            {
                pictureBox1.Image = Image.FromFile("平.jpg");
                pictureBox2.Image = Image.FromFile("平.jpg");
            }
            else if (textBox1.Text == "剪刀" && textBox2.Text == "剪刀")
            {
                pictureBox1.Image = Image.FromFile("平.jpg");
                pictureBox2.Image = Image.FromFile("平.jpg");
            }
            else
            {
                pictureBox1.Image = Image.FromFile("负.jpg");
                pictureBox2.Image = Image.FromFile("胜.jpg");
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/gongth-12/archive/2012/09/10/2791676.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值