代码讲解

演示视频链接:https://github.com/disschen/-/blob/master/%E6%BC%94%E7%A4%BA%E8%A7%86%E9%A2%91.mp4

变量声明

 public Texture2D img1;   //棋子1public Texture2D img2;   //棋子2public Texture2D imgBack;//棋子背景private int player;      //基数说明轮到1玩家,偶数说明轮到2玩家private int result;      //胜负private int [,] matrix2D;//棋盘矩阵private int [] repent;   //记录上一步棋,便于悔棋

重要函数

  1. 重开函数:重开函数是把所有的值都设为初始值,在这里都是0。start()函数通过调用reset()函数实现。
    void Start(){Reset();}void Reset(){player = 0;result = 0;matrix2D = new int [3,3]{{0,0,0},{0,0,0},{0,0,0}};repent = new int [2] {0,0};}
  1. 结果检查函数:用来检查是否已经有一方胜利
 int check(){for(int i = 0;i < 3;i ++){ if(matrix2D[i,0] == matrix2D[i,1] && matrix2D[i,1]== matrix2D[i,2] && matrix2D[i,0] != 0) return matrix2D[i,0];   // 检查列if(matrix2D[0,i] == matrix2D[1,i] && matrix2D[1,i]== matrix2D[2,i] && matrix2D[0,i] != 0) return matrix2D[0,i];   // 检查行}if(matrix2D[0,0] == matrix2D[1,1] && matrix2D[1,1] ==matrix2D[2,2]) return matrix2D[1,1];                              // 检查对角if(matrix2D[0,2] == matrix2D[1,1] && matrix2D[1,1] ==matrix2D[2,0]) return matrix2D[1,1];                              // 检查对角return 0;}
  1. 悔棋函数:用来对悔棋相关的变量进行处理
 void Repent(){player ++;                        // 将玩家设为想要悔棋的一方matrix2D[repent[0],repent[1]] = 0;// 将repent数组记录的坐标点棋子设为0,即没有棋子}
  1. OnGUI :显示游戏界面
 void OnGUI(){// 设置游戏界面的大小GUI.Label(new Rect(0,0,300,300),"");   // 设置重新开始按钮if (GUI.Button(new Rect(200, 0, 100, 50), "restart"))Reset();// 设置悔棋按钮if ( result == 0 && GUI.Button(new Rect(200, 50, 100, 50), "repent"))Repent();// 若游戏结束则去除悔棋按钮else ;// 设置游戏结果显示风格GUIStyle fontStyle = new GUIStyle();  fontStyle.normal.background = null;   fontStyle.normal.textColor= new Color(1, 0, 1);    fontStyle.fontSize = 15;   // 根据matrix2D保存的值对棋盘上的每个按钮进行设置for(int i = 0;i < 3;i ++ ){for(int j = 0;j < 3;++ j){if(matrix2D[i,j] == 1)GUI.Button(new Rect(50*i, 50*j, 50, 50), img1);else if(matrix2D[i,j] == 2)GUI.Button(new Rect(50*i, 50*j, 50, 50), img2);else if(result != 0){if(GUI.Button(new Rect(50*i, 50*j, 50, 50), imgBack));}else{if(GUI.Button(new Rect(50*i, 50*j, 50, 50), imgBack)){matrix2D[i,j] = 1 + player % 2;// 记录这一步的坐标,悔棋时使用repent[0] = i;   repent[1] = j;result = check();player ++;}}}} // 结果显示if (result == 1) {    GUI.Label (new Rect (200, 100, 100, 50), "Player1 wins!", fontStyle);    } else if (result == 2) {    GUI.Label (new Rect (200, 100, 100, 50), "Player2 wins!", fontStyle);    } else {GUI.Label (new Rect (200, 100, 100, 50), "Playing...", fontStyle);} }

完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UI : MonoBehaviour
{public Texture2D img1;public Texture2D img2;public Texture2D imgBack;private int player;private int result;private int [,] matrix2D;private int [] repent;void OnGUI(){GUI.Label(new Rect(0,0,300,300),"");if (GUI.Button(new Rect(200, 0, 100, 50), "restart"))Reset();if ( result == 0 && GUI.Button(new Rect(200, 50, 100, 50), "repent"))Repent();else ;GUIStyle fontStyle = new GUIStyle();  fontStyle.normal.background = null;   fontStyle.normal.textColor= new Color(1, 0, 1);    fontStyle.fontSize = 15;   for(int i = 0;i < 3;i ++ ){for(int j = 0;j < 3;++ j){// if(result != 0) continue;if(matrix2D[i,j] == 1)GUI.Button(new Rect(50*i, 50*j, 50, 50), img1);else if(matrix2D[i,j] == 2)GUI.Button(new Rect(50*i, 50*j, 50, 50), img2);else if(result != 0){if(GUI.Button(new Rect(50*i, 50*j, 50, 50), imgBack));}else{if(GUI.Button(new Rect(50*i, 50*j, 50, 50), imgBack)){matrix2D[i,j] = 1 + player % 2;repent[0] = i;    repent[1] = j;result = check();player ++;}}}} if (result == 1) {    GUI.Label (new Rect (200, 100, 100, 50), "Player1 wins!", fontStyle);    } else if (result == 2) {    GUI.Label (new Rect (200, 100, 100, 50), "Player2 wins!", fontStyle);    } else {GUI.Label (new Rect (200, 100, 100, 50), "Playing...", fontStyle);} }// Start is called before the first frame updatevoid Start(){Reset();}void Reset(){player = 0;result = 0;// Debug.Log("12");matrix2D = new int [3,3]{{0,0,0},{0,0,0},{0,0,0}};repent = new int [2] {0,0};}void Repent(){player ++;matrix2D[repent[0],repent[1]] = 0;}int check(){for(int i = 0;i < 3;i ++){if(matrix2D[i,0] == matrix2D[i,1] && matrix2D[i,1]== matrix2D[i,2] && matrix2D[i,0] != 0) return matrix2D[i,0];if(matrix2D[0,i] == matrix2D[1,i] && matrix2D[1,i]== matrix2D[2,i] && matrix2D[0,i] != 0) return matrix2D[0,i];}if(matrix2D[0,0] == matrix2D[1,1] && matrix2D[1,1] ==matrix2D[2,2]) return matrix2D[1,1];if(matrix2D[0,2] == matrix2D[1,1] && matrix2D[1,1] ==matrix2D[2,0]) return matrix2D[1,1];return 0;}// Update is called once per framevoid Update(){}
}

unity实现简单游戏——井字棋相关推荐

  1. 用Unity3D实现简单的井字棋小游戏

    用Unity3D实现简单的井字棋小游戏 项目地址 井字棋小游戏 完成效果图 实现思路 首先定义游戏的数据部分: /* 井字棋中每一个棋格中的逻辑控制常量,代表这个棋格的状态 */ private co ...

  2. python小游戏井字棋(人机对战)

    游戏简介:在九宫格内进行,如果一方抢先于另一方向(横.竖.斜)连成3子,则获得胜利.游戏中输入方格位置代号的形式如下: 设计前的思路: 游戏中,board棋盘存储玩家.计算机的落子信息,未落子处未EM ...

  3. 用c语言编写的打字母游戏,用C语言编写小游戏——“井字棋”

    原标题:用C语言编写小游戏--"井字棋" 作者:Milo Yip 来源:知乎 原文链接:https://zhuanlan.zhihu.com/p/39581573 在 Milo Y ...

  4. 用IE网页学游戏-井字棋

    IE网页学游戏-井字棋 井字棋实现交互的简单小游戏,学习使用表格,理解javascript函数. 1.构造棋盘 用table元素的行列构造三行三列的表格.设置好120像素的井字棋背景,三个小图表示方格 ...

  5. Python基础编程案例:简单的井字棋游戏设计与制作

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 前言 python井字棋游戏虽然看上去非常简陋,但是却非常值得学习. 先看怎么玩 ...

  6. 井字棋小游戏c语言简单编码,井字棋小游戏(C语言)

    1 #include 2 #include 3 #include 4 #include 5 #include 6 7 void drawBoard(char *board) //绘制棋盘 8 {9 p ...

  7. 【C语言】(实例游戏)实现童年游戏——井字棋

    初学C语言,虽然学到的内容不是很多,但是有一些简单的同年游戏我们是可以实现的,接下来,我们就看一下井字棋的实现,首先创建3个文件,分别为头文件 --定义与声明 函数的实现  以及测试文件 首先我们先说 ...

  8. 用Java实现简单的井字棋程序(α-β剪枝)

    利用α-β剪枝实现井字棋程序 剪枝思路如下: α可以认为是你的收益>=α,β可以认为是你的收益<=β,当α>β的时候,收益比α要大,比β要小,显然是一个空集.所以进行剪枝. α的初始 ...

  9. python写井字棋_python 游戏(井字棋)

    1. 游戏思路和流程图 实现功能,现实生活中的井字棋玩法 游戏流程图SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠. 2. 使用模块和游戏提示 impor ...

最新文章

  1. 谈python函数的参数处理
  2. java calendar去掉时分秒_java 8:只取年月日的java.util.Date(时分秒清零)对象
  3. linux 获取 基地址,linux - 每个函数加载的glibc基地址不同。 - SO中文参考 - www.soinside.com...
  4. 谷歌浏览器翻译插件 saladict:沙拉查词
  5. MYSQL中用正则筛选一定的数据
  6. 分类素材(part5)--大话Python机器学习(中)
  7. C++03:论容器的使用
  8. Python爬虫基础:验证码的爬取和识别详解
  9. 中国光谷大数据产业联盟成立 打造大数据产业生态圈
  10. 在Windows中安装SoapUI
  11. python使用matplotlib可视化矢量流场图、使用streamplot函数可视化矢量流场图、矢量流场图包含坐标和方向
  12. 自体脂肪填充面部能保持多长时间,这是能说的吗
  13. S32K144调试记录(一)
  14. 为什么我不建议你轻易入上位机的“坑”?
  15. [附源码]Python计算机毕业设计城市旅游景点门票订购系统
  16. 电脑老是出现无法登陆的界面,怎么解决
  17. 帝国CMS对接百度小程序实现文章自动收录的方法
  18. loaderruner
  19. 华唯鑫能油的推荐每日一练|你知道的液体燃料有哪些?
  20. esp8266之Arduino的mqtt客户端远程继电器控制,断电重连、断网重连,断mqtt服务器重连

热门文章

  1. iphoneipad图标尺寸
  2. 呼叫系统的技术实现原理和运作流程
  3. 网上那些代理IP是哪儿来的
  4. 给你三个必须要学C语言的理由!
  5. 考研英语 - word-list-41
  6. 云计算应用现状及其发展趋势和特点
  7. 〖Python 数据库开发实战 - Python与MySQL交互篇⑩〗- 创建新闻管理系统的具体python文件
  8. 开源并“免费”的Linux平台DAW——Ardour 4.0发布
  9. 微信小程序 MinUI 组件库系列之 abnor 异常流组件
  10. 钢琴家软件里曲谱的数据为什么更新不了_王者荣耀安卓与IOS互通?IOS恐怕得到不少利益,不然这事成不了...