本文使用winform实现简单的石头剪刀布的游戏,主要实现,电脑随机出拳,玩家手动点击出拳;实现简易背景图片3秒切换;简易统计信息。

1、效果图

2.实现代码

新建一个windows窗体程序,用数字1代表石头,用数字2代表剪刀,用数字3代表布,结果取玩家和电脑出拳之差,有三种结果

  • 玩家赢: -1,2
  • 平手: 0
  • 玩家输: 其它值

新建3个类:

1)Computer.cs 电脑随机出拳

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 石头剪刀布
{
class Computer
{ public string Fist
{
get;
set;
} public int ShowFist()
{
Random rnd = new Random();
int fist = rnd.Next(, );
switch (fist)
{
case : Fist = "石头"; break;
case : Fist = "剪刀"; break;
case : Fist = "布"; break;
}
return fist;
} }
}

2)、Judge.cs 裁判类 判断输赢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 石头剪刀布
{ class Judge
{ public enum RESULT
{
玩家赢,
电脑赢,
平手
} public static RESULT WhoWin(int playerNum, int computerNum)
{
int result = playerNum - computerNum;
if (result == - || result == )
{
return RESULT.玩家赢;
}
else if (result == )
{
return RESULT.平手; }
else
{
return RESULT.电脑赢;
} } }
}

3)、Player.cs 玩家,出拳

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 石头剪刀布
{
class Player
{
public static int ShowFist(string fist)
{
switch (fist)
{
case "石头": return ;
case "剪刀": return ;
case "布": return ;
default: return ;
}
} }
}

界面后台实现代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 石头剪刀布
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 点击石头按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStone_Click(object sender, EventArgs e)
{
String fist = "石头";
Game(fist);
}
/// <summary>
/// 点击剪刀按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnScissors_Click(object sender, EventArgs e)
{
String fist = "剪刀";
Game(fist);
}
/// <summary>
/// 点击布按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCloth_Click(object sender, EventArgs e)
{
String fist = "布";
Game(fist); } //背景图片轮播
String[] paths = Directory.GetFiles(@"C:\work\stone");//此目录里面必须有图片,否则会报错
private void timer1_Tick(object sender, EventArgs e)
{
this.BackgroundImage = Image.FromFile(paths[new Random().Next(, paths.Length)]); }
static int playerWinTimes = ;//玩家赢的次数
static int gameTimes = ;//总共次数
static int tieTimes = ;//平手次数 /// <summary>
/// 通用方法
/// </summary>
/// <param name="fist"></param>
private void Game(String fist)
{
gameTimes++;
lbPlayer.Text = fist;
int playerNum = Player.ShowFist(fist);
Computer cpu = new Computer();
int cpuNum = cpu.ShowFist();
lbComputer.Text = cpu.Fist;
Judge.RESULT result = Judge.WhoWin(playerNum, cpuNum);
lbJudge.Text = result.ToString();
lbStatistics.Text = "统计信息:\n\n1.您赢了" + playerWinTimes + "场比赛!\n\n" + "2.平手了" + tieTimes + "次; \n\n" + "3.输掉了" + (gameTimes - playerWinTimes - tieTimes) + "场比赛; \n\n" + "4.共进行了" + gameTimes + "场比赛!\n\n"; if (result == Judge.RESULT.玩家赢)
{
playerWinTimes++;
MessageBox.Show("恭喜,您已经赢了" + playerWinTimes + "场比赛!" + " 共进行了" + gameTimes + "场比赛!");
}
else if (result == Judge.RESULT.平手)
{
tieTimes++;
} } }
}

实现游戏的难点在于要想到将石头剪刀布用数字来替换,如果逻辑通了实现起来并不难。

本文源码:https://github.com/amosli/CSharp/tree/stone

C# Winform学习--- 实现石头剪刀布的游戏的更多相关文章

  1. 微信小程序开发入门学习(1):石头剪刀布小游戏

    从今天起开始捣鼓小程序了2018-12-17   10:02:15 跟着教程做了第一个入门实例有兴趣的朋友可以看看: 猜拳游戏布局 程序达到的效果 猜拳游戏的布局是纵向显示了三个组件:文本组件(tex ...

  2. Winform学习手册(目录)

    一.基础: WINFORM学习笔记——创建Winform项目 WINFORM学习手册——TextBox.Lable.Button WINFORM学习笔记——窗体生命周期 WINFORM学习手册——对话 ...

  3. winform小程序---猜拳小游戏

    因为学的时间不长,所以借鉴了一些资料做了这个小程序,大家共同学习,共同进步.感觉很有自信,世上无难事,只怕有心人. using System; using System.Collections.Gen ...

  4. java学习之坦克大战游戏

    总结:由于这几天快过年比较忙然后没怎么写,写代码途中一些经验总结现在给忘记了.这次的小项目感觉比上次写的思路清楚了点.没有之前第一次写那么逻辑混乱,结构也搞的比之前的要好,添加功能比较容易.学习了之前 ...

  5. 【Unity 3D】学习笔记29:游戏的例子——简单的小制作地图

    无论学习.只看不练是坏科学. 因此,要总结回想这怎么生产MMROPG小地图的游戏.于MMROPG游戏类,在游戏世界中行走时导致各地,通常在屏幕的右上角,将有一个区域,以显示当前的游戏场景微缩.在游戏世 ...

  6. 【Visual C++】游戏编程学习笔记之三:游戏循环的使用

     本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44208419 作者:Zee ...

  7. 增强学习训练AI玩游戏

    1.游戏简介 符号A为 AI Agent. 符号@为金币,AI Agent需要尽可能的接取. 符号* 为炸弹,AI Agent需要尽可能的躲避. 游戏下方一组数字含义如下: Bomb hit: 代表目 ...

  8. C语言编程学习打造——做题游戏

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  9. python学习之掷骰子游戏

    """ 通过学习的python知识,写一个简单的python小游戏 游戏名字:掷骰子比大小 游戏规则: 1.玩家可以选择玩掷几个骰子游戏(默认3个) 2.玩家可以设置双方 ...

随机推荐

  1. IOS开发者账号申请记录

    1. 准备好一个企业邮箱 .企业营业执照复印件.一张可以支付的VISA或者MasterCard 2. 2016/01/13 注册appid https://developer.apple.com/pr ...

  2. AC 设置DMZ口上网

  3. jQuery 学习笔记(函数调用机制)

    最近在学前端框架amazeui,之前用其中的CSS样式搭建了一个伪360网页,学会了点布局的东西,但是始终觉得有点无聊.所以这几天就开始研究jquery代码了. 对于我这样一个初学者来说,有很多东西都 ...

  4. 在js中怎么样选择互斥的相邻元素

    在使用jquery中,我们通常会选择siblings()去选择相邻元素,使用eq()方法去匹配元素,使用index()获取对应元素的索引值,具体jquery代码如下: <style> *{ ...

  5. C#正则提取html图片等

    去除html标记,比较实用,分享给大家. ///   <summary>   ///   去除HTML标记   ///   </summary>   ///   <par ...

  6. java守护线程的理解

    package daemonThread; /*setDaemon(true)方法将线程设置为守护线程,线程的Daemon默认值为false * 只要当前JVM实例中存在任何一个非守护线程没有结束,守 ...

  7. flex+java+blazeds 多通道好文

    http://www.cnblogs.com/noam/archive/2010/08/05/1793504.html blazeds, spring3整合实现RPC服务和消息服务 环境: MyEcl ...

  8. 特殊js事件

    1:点击enter事件 $(document).keypress(function(e) { // 回车键事件 if(e.which == 13) { submitForm(); } }); 2:JQ ...

  9. Foundation ----->NSSet

    1.集合类     NSString *s1 = @"zhangsan";     NSString *s2 = @"lisi";     NSString * ...

  10. java 在linux环境下写入 syslog 问题研究

    1.Syslog 在Unix类操作系统上,syslog广泛应用于系统日志.syslog日志消息既可以记录在本地文件中,也可以通过网络发送到接收syslog的服务器.接收syslog的服务器可以对多个设 ...