本文主要是介绍Unity入门小游戏——记忆大师(Remember Master),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 游戏展示
- 概述
- 游戏基本玩法
- 源代码展示
- 组件代码
- 样式代码
- 时间判断逻辑
- 核心判断逻辑
- 完整代码展示
游戏展示
《这你都记不住?》
概述
该游戏为unity入门级游戏,使用IMGUI组件而不是标准的3d组件,旨在熟悉unity的基本操作。
游戏基本玩法
你在一间封闭的房间醒来,昏暗的灯光下你只能看见门前有一个5位数的密码锁,密码锁的密码不停变化,每次只出现3秒钟,记忆大师,动用你的超强记忆力,解救自己吧。
游戏玩法简单,游戏随机生成一个由123数字组成的五位数,玩家需要在3秒内进行记忆并尝试正确输入该五位数字。
源代码展示
游戏构造较为简单,包含四个lable组件和四个按钮组件。
组件代码
GUI.Box(new Rect(210,25,300,350),"Try to remember!");
GUI.Label(new Rect(235,55,200,50),"provided number:");
GUI.Label(new Rect(260, 55, 200, 100), answer, label_style);
GUI.Label(new Rect(235,140,200,50),"enter number:");
GUI.Label(new Rect(260,140,200,100),res,label_style);
GUI.Button(new Rect(250,235,60,60),"1",btn_style);
GUI.Button(new Rect(330,235,60,60),"2",btn_style);
GUI.Button(new Rect(410,235,60,60),"3",btn_style);
样式代码
在该代码中,我使用了GUIStyle类来对组件样式进行修改。
达到以下目的:
- 组件中显示文字变大
- 错误时文字变红进行提示
- 成功时出现 绿色 的YOU WIN增加视觉效果
代码示例
// 样式修改label_style = new GUIStyle(GUI.skin.label);btn_style = new GUIStyle(GUI.skin.button);win_style = new GUIStyle(GUI.skin.label);win_style.normal.textColor = Color.green;btn_style.fontSize = 30;label_style.fontSize = 35;win_style.fontSize = 35;
时间判断逻辑
使用C#自带类 DateTime来进行计时
if (!timeStarted)
{startTime = DateTime.Now;timeStarted = true;
}DateTime currentDateTime = DateTime.Now;
TimeSpan elapsedTime = currentDateTime - startTime;
int time = (int)elapsedTime.TotalSeconds;
核心判断逻辑
每次点击按钮让 res 字符串增加对应按钮的字符,并与当前answer的当前位置字符进行判断
以按钮“1”为例
if(GUI.Button(new Rect(250,235,60,60),"1",btn_style)){fix_wrong();res += "1";if(!('1' == answer[ctn])){wrong = true;}elsectn++;
}
完整代码展示
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class remembergame : MonoBehaviour
{private DateTime startTime;private bool timeStarted = false;private int ctn = 0;private bool wrong = false;private string answer = "";private string res = "";private GUIStyle label_style;private GUIStyle win_style;private GUIStyle btn_style;// Start is called before the first frame updatevoid Start(){random_num();timeStarted = false;}void OnGUI(){// 样式修改label_style = new GUIStyle(GUI.skin.label);btn_style = new GUIStyle(GUI.skin.button);win_style = new GUIStyle(GUI.skin.label);win_style.normal.textColor = Color.green;btn_style.fontSize = 30;label_style.fontSize = 35;win_style.fontSize = 35;// 输入错误,文字变红进行提示if(wrong){label_style.normal.textColor = Color.red;}else{label_style.normal.textColor = Color.white;}// 文字居中显示label_style.alignment = TextAnchor.MiddleCenter;GUI.Box(new Rect(210,25,300,350),"Try to remember!");GUI.Label(new Rect(235,55,200,50),"provided number:");//时间流逝if (!timeStarted){startTime = DateTime.Now;timeStarted = true;}DateTime currentDateTime = DateTime.Now;TimeSpan elapsedTime = currentDateTime - startTime;if ((int)elapsedTime.TotalSeconds <= 1){GUI.Label(new Rect(260, 55, 200, 100), answer, label_style);}else{GUI.Label(new Rect(260, 55, 200, 100), ""); // 在超过2秒后清空数字// 创建按钮if(GUI.Button(new Rect(250,235,60,60),"1",btn_style)){fix_wrong();res += "1";if(!('1' == answer[ctn])){wrong = true;}elsectn++; }if(GUI.Button(new Rect(330,235,60,60),"2",btn_style)){fix_wrong();res += "2";if(!('2' == answer[ctn])){wrong = true;}elsectn++;}if(GUI.Button(new Rect(410,235,60,60),"3",btn_style)){fix_wrong();res += "3";if(!('3' == answer[ctn])){wrong = true;}elsectn++;}}GUI.Label(new Rect(235,140,200,50),"enter number:");// 输入打到五个后,说明游戏胜利,进行提示if(ctn == 5){GUI.Label(new Rect(275,165,200,100),"YOU WIN!",win_style);}elseGUI.Label(new Rect(260,140,200,100),res,label_style);if(GUI.Button(new Rect(310,320,100,30),"Restart")){// Debug.Log("restart");res = "";wrong = false;ctn = 0;timeStarted = false; // 重置时间startTime = DateTime.Now; // 重新开始计时random_num();}}
//恢复错误显示void fix_wrong(){if(wrong){res = "";wrong = false;ctn = 0;}}
//生成随机数void random_num(){System.Random random = new System.Random();answer = "";for (int i = 0; i < 5; i++){int randomNumber = random.Next(1, 4); // 生成1到3之间的随机数answer += randomNumber.ToString();}}
}
这篇关于Unity入门小游戏——记忆大师(Remember Master)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!