C# Winform学习--- 实现石头剪刀布的游戏
本文使用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):石头剪刀布小游戏
从今天起开始捣鼓小程序了2018-12-17 10:02:15 跟着教程做了第一个入门实例有兴趣的朋友可以看看: 猜拳游戏布局 程序达到的效果 猜拳游戏的布局是纵向显示了三个组件:文本组件(tex ...
- Winform学习手册(目录)
一.基础: WINFORM学习笔记——创建Winform项目 WINFORM学习手册——TextBox.Lable.Button WINFORM学习笔记——窗体生命周期 WINFORM学习手册——对话 ...
- winform小程序---猜拳小游戏
因为学的时间不长,所以借鉴了一些资料做了这个小程序,大家共同学习,共同进步.感觉很有自信,世上无难事,只怕有心人. using System; using System.Collections.Gen ...
- java学习之坦克大战游戏
总结:由于这几天快过年比较忙然后没怎么写,写代码途中一些经验总结现在给忘记了.这次的小项目感觉比上次写的思路清楚了点.没有之前第一次写那么逻辑混乱,结构也搞的比之前的要好,添加功能比较容易.学习了之前 ...
- 【Unity 3D】学习笔记29:游戏的例子——简单的小制作地图
无论学习.只看不练是坏科学. 因此,要总结回想这怎么生产MMROPG小地图的游戏.于MMROPG游戏类,在游戏世界中行走时导致各地,通常在屏幕的右上角,将有一个区域,以显示当前的游戏场景微缩.在游戏世 ...
- 【Visual C++】游戏编程学习笔记之三:游戏循环的使用
本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44208419 作者:Zee ...
- 增强学习训练AI玩游戏
1.游戏简介 符号A为 AI Agent. 符号@为金币,AI Agent需要尽可能的接取. 符号* 为炸弹,AI Agent需要尽可能的躲避. 游戏下方一组数字含义如下: Bomb hit: 代表目 ...
- C语言编程学习打造——做题游戏
C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...
- python学习之掷骰子游戏
""" 通过学习的python知识,写一个简单的python小游戏 游戏名字:掷骰子比大小 游戏规则: 1.玩家可以选择玩掷几个骰子游戏(默认3个) 2.玩家可以设置双方 ...
随机推荐
- java路径问题
使用了java这么久一直对java获取路径存在困惑,将一些常用的获取路径方式记录如下: val property = System.getProperty("user.dir")) ...
- MVC之路由规则 (自定义,约束,debug)
自定义路由规则的要求,小范围写在前,大范围写在后.路由规则可以注册多条,路由规则的名称不能重复路由规则有顺序,并且按照顺序进行匹配,建议小范围写在前,大范围写在后.路由规则可以设置约束 即正则表达式路 ...
- 11,SFDC 管理员篇 - 报表和数据的可视化
1,Report Builder 1,每一个report type 都有一个 primay object 和多个相关的object 2,Primary object with related obje ...
- 【SSM 5】Mybatis分页插件的使用
一.添加maven依赖项 <span style="font-family:KaiTi_GB2312;font-size:18px;"><dependency&g ...
- css内容样式属性
设置元素的最大高度.最小高度.最大宽度.最小宽度,用max-height.min-height.max-width.min-width. visibility:设置元素是否可见.visible和hid ...
- 仿google art图片预览算法及demo(web版本)
演示地址: http://codeman35.itongyin.com:19001/v3/preview.html 功能支持:拖动 滚轴放大缩小 按钮放大缩小 鹰眼预览 鹰眼拖动等功能
- VUE 入门笔记
前端的MVVM概念今年来也算是如火如荼,了解完 MVVM的概念,也该找个去尝试下 首先我先试了下 国内小而美的 VUE 试着照着文档敲出入门文件,内容都在注释里 <!doctype html&g ...
- Visual Studio 2015 工具箱丢失
网上主要的解答分为两种:1. 未打开设计界面 2. 重置 实际上,还有一个原因是,没有启动完整版的VS. 安装完后,会有两个VS的程序,一个是Blend For Visual Studio 2015, ...
- HTML5来回拖动实例
<html> <meta charset="utf-8"> <script> //规定被拖动的数据 function tdwhat(ev,obj ...
- Visual Studio 15 Preview 4安装
今天看到了有Visual Studio 15 Preview 4的安装文件放出,便想去安装体验一下C# 7.0的新语法.谁知安装时遇到一个错误: 手动下载这个补丁安装后,还是提示这个错误.本来以为是还 ...