unity3d 入门——实现井字棋
2017-03-04 21:42
169 查看
代码部分是用c#实现的,作为有过c,c++基础的程序员,c#上手的感觉还可以,也没有到什么高级的特性。
作为3d游戏编程课程的第一个作业,还有师兄的博客的参考,整体难度并不大。
实现中主要用到了
行为对象属性中的:
OnGui()//渲染处理GUI事件,没一帧调用一次
GUI标签
GUI.Label() //在屏幕上创建一个文本或者纹理标签。
GUI.Button()//创建一个单次按下按钮。用户点击按钮事件立即触发。当用户点击按钮的时候返回true。
以下是整体实现的代码。如果有bug欢迎指出哟~
作为3d游戏编程课程的第一个作业,还有师兄的博客的参考,整体难度并不大。
实现中主要用到了
行为对象属性中的:
OnGui()//渲染处理GUI事件,没一帧调用一次
GUI标签
GUI.Label() //在屏幕上创建一个文本或者纹理标签。
GUI.Button()//创建一个单次按下按钮。用户点击按钮事件立即触发。当用户点击按钮的时候返回true。
以下是整体实现的代码。如果有bug欢迎指出哟~
using System.Collections; using System.Collections.Generic; using UnityEngine; public class mybutton : MonoBehaviour { //显示棋盘内容 private string[] info = new string [9]; //记录棋盘的状态,state 0代表没有按下,1代表X按下, 2代表O按下 private int[] state = new int[9]; //1代表X的回合,0代表O的回合 private int turn =0; private string result=""; private int i; public Texture buttonTexture; private int temp; private int temp1; void Start () { init(); } void init() { for (i = 0; i < 9; i++) { info[i] = ""; state[i] = 0; } turn = 0; } // Update is called once per frame void Update () { } void OnGUI() { int x, y, num; GUI.Label(new Rect(40, 320, 50, 50), result); if(GUI.Button(new Rect(200,320, 100, 40), "reStart")) { init(); result = ""; } GUI.color = Color.yellow; GUI.backgroundColor = Color.red; //游戏结束后只能操作reStart健 if (isend() ) { x = 20; y = 20; num = 0; //保留游戏结果的情况 for (temp = 0; temp < 3; temp++) { for (temp1 = 0; temp1 < 3; temp1++) { GUI.Button(new Rect(x, y, 80, 80), info[num]); x += 80; num++; } y += 80; x = 20; } if (GUI.Button(new Rect(200, 320, 100, 40), "reStart")) { init(); result = ""; } return; } //检测玩家操作 x = 20; y = 20; num = 0; for(temp=0;temp<3;temp++) { for(temp1=0;temp1<3;temp1++) { if(GUI.Button(new Rect(x, y, 80, 80), info[num])) { if(state[num]==0&&turn==0) { info[num] = "O"; state[num] = 2; } else if(state[num]==0&&turn==1) { info[num] = "X"; state[num] = 1; } if(turn==0) { turn = 1; } else if(turn==1) { turn = 0; } } x += 80; num++; } y += 80; x = 20; } } bool isend() { //检查横排 int j = 0; for (j = 0; j < 7; j = j + 3) { if (state[j] == state[j + 1] && state[j + 1] == state[j + 2]) { if (state[j] == 1) { result = "X wins by row"; return true; } else if (state[j] == 2) { result = "O wins by row"; return true; } } } //检查竖排 for(j=0;j<3;j++) { if(state[j]==state[j+3]&&state[j+3]==state[j+6]) { if (state[j] == 1) { result = "X wins by column"; //print("x win"); return true; } else if (state[j] == 2) { result = "O wins by column"; //print("O win"); return true; } } } //检查对角线 if (state[0] == state[4] && state[ 4] == state[8]) { if (state[0] == 1) { result = "X wins by diagonal"; return true; } else if (state[0] == 2) { result = "O wins by diagonal"; return true; } } if (state[2] == state[4] && state[4] == state[6]) { if (state[2] == 1) { result = "X wins by diagonal"; return true; } else if (state[2] == 2) { result = "O wins by diagonal"; return true; } } //检查是否平局 int flag = 0; for(j=0;j<9;j++) { if(state[j]==0) { flag = 1; break; } } if(flag==0) { result = "a draw"; return true; } return false; } }
相关文章推荐
- 一步一步使用Unity3d工具实现AR开发入门
- Unity3D从入门到放弃(七) ----DoTween的实现
- arm wince EVC入门之一: 实现远程调试的最佳硬件途径
- AXIS简单实现入门
- Visual C# 入门 -Office 编程(如何实现 - C#)
- EVC入门之一: 实现远程调试的最佳硬件途径
- 入门6-Silverlight 1.0中多媒体影音的实现
- HQL入门及基础实现(转)
- dwr 登录实现 (入门知识)
- web content management新手入门--分页功能的实现
- 入门: tomcat6013+myeclipse55m1+jsf12+spring2+proxool+jtds 的开发实现
- J2ME 2D小游戏入门之旅(四) 加入子弹群,实现碰撞运算
- WinSock初学入门:实现TCP服务器
- C#入门(8)-实现单链表
- AOP入门的简单例子(Spring AOP实现)
- WinSock初学入门:实现TCP服务器(zz)
- J2ME 2D小游戏入门之旅(五) 实现爆炸效果、并加入道具导弹
- [Eclipse]GEF入门系列(十一、树的一个实现)
- JSP入门初级教程之实现页面跳转
- 入门6-Silverlight 1.0中多媒体影音的实现