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.玩家可以设置双方 ...
随机推荐
- php 正则表达式 将形如 "天," ,"安", "门" 转化为"天、安、门", (仅匹配汉字)
#!/usr/bin/php<? $rows = file("illwods_deal1.txt"); $goalfile = fopen("illwods_res ...
- 低版本的xcode打开xcode8上的xib错误
XIB和Storeboard适配 在Xcode8之前,创建一个XIB或SB文件,都是一个600*600的方块XIB文件.在Xcode8之后,创建的XIB文件默认是6s尺寸的大小. 但是Xcode8打开 ...
- ssh 的搭建
struts包的下载:http://struts.apache.org/download.cgi#struts252 string包的下载: http://repo.spring.io/release ...
- js 判断IOS版本号
先来观察 iOS 的 User-Agent 串: Phone 4.3.2 系统:Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; e ...
- Mysql字符类型比较
一. binary和char比较: binary 字节为单位,char字符为单位,字符占几个字节取决于字符集 binary 比较规则基于字节值,char基于字符,即使是_bin的比较规则 范围都0- ...
- Windows程序设再读笔记03-窗口与消息
1.关于LoadIcon/LoadCursor,这两个函数,第一个参数为实例句柄,如果是从保存在磁盘中的可执行文件中加载资源,则需要则需要指定可执行文件的hInstance,如果是系统资源,该句柄为N ...
- PS Web切图界面设置
界面为移动工具时(快捷键V),选中左上角的图层. 点击视图,选中显示→智能参考线,与标尺. 点击窗口,把"库" "颜色"去掉,把屏幕右上角的"通道&q ...
- java上传xls文件
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...
- kaggle& titanic代码
这两天报名参加了阿里天池的’公交线路客流预测‘赛,就顺便先把以前看的kaggle的titanic的训练赛代码在熟悉下数据的一些处理.题目根据titanic乘客的信息来预测乘客的生还情况.给了titan ...
- Matrix QR Decomposition using OpenCV
Matrix QR decomposition is very useful in least square fitting model. But there is no function avail ...