猜拳(石头剪刀布)
今天用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");
}
}
}
}