本文使用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. socket 网络编程

    1. 基础socket库 socket.h: /** * 网络套接字库 */ #ifndef Socket_h #define Socket_h #include <stdio.h> #i ...

  2. URL特殊字符的转义

    + 转义符为 %2B 空格 转义符为 + 或 %20 / 转义符为 %2F ? 转义符为 %3F % 转义符为 %25 # 转义符为 %23 & 转义符为 %26 = 转义符为 %3D

  3. 获取系统进程信息和进程依赖的dll信息

    body { font-family: Bitstream Vera Sans Mono; font-size: 11pt; line-height: 1.5; } html, body { colo ...

  4. python中的goto

    python中没有像C语言中的goto,不过,查找着之后发现有python大牛写了一个goto,我直接拿来用啦,在此分享下: 代码地址:https://github.com/snoack/python ...

  5. 【转】如何在Mac系统中安装R的rattle包

    [转自知乎]:https://www.zhihu.com/question/28944497 1. 安装 xquartz (http://xquartz.macosforge.org)2. 安装 GT ...

  6. 传说中的requestAnimFrame

    //让浏览器以10ms绘制 兼容写法                window.requestAnimFrame = (function() {                    return ...

  7. Linux Shell脚本实现根据进程名杀死进程

    Shell脚本源码如下: #!/bin/sh #根据进程名杀死进程 if [ $# -lt 1 ] then echo "缺少参数:procedure_name" exit 1 f ...

  8. HostOnly Cookie和HttpOnly Cookie

    怎么使用Cookie? 通常我们有两种方式给浏览器设置或获取Cookie,分别是HTTP Response Headers中的Set-Cookie Header和HTTP Request Header ...

  9. Winform 中DataGridView控件添加行标题

    有很多种方法. 1.可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value) /// <summary> / ...

  10. RequireJS基础(二)

    上一篇是把整个jQuery库作为一个模块.这篇来写一个自己的模块:选择器. 为演示方便这里仅实现常用的三种选择器id,className,attribute. RequireJS使用define来定义 ...