当前位置:网站首页>Unity五子棋游戏设计 和简单AI实现(1)
Unity五子棋游戏设计 和简单AI实现(1)
2022-08-09 05:56:00 【makise2333】
1 准备工作:
vs unity ,棋子,一张15*15的棋局.
我们先把准备好的图直接拖上来,将左下方的点设为(0,0),因此将中心点设为(7,7),由于图中的中心和图片pivot不完全吻合,所以会往下拖一点,但不影响。
2 代码部分
1 创建一个Player脚本和player的空物体,将脚本挂到player上 ,以下代码能够点击并且反馈点击在棋盘中的位置。
void Update()
{
PlayChess();
}
void PlayChess()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log((int)pos.x+" "+(int)pos.y);
}
}
2 防止点击棋盘外并且为了玩家更好的体验(玩家可能点击到2个点的中间位置)对代码进行修改,pos需要各加0.5。
void PlayChess()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (pos.x > 14 || pos.x < 0 || pos.y > 14 || pos.y < 0) return;
pos.x += (float) 0.5;pos.y += (float)(0.5);
Debug.Log((int)pos.x+" "+(int)pos.y);
}
}
3之后创建脚本gametable,创建int类型数组记录每次下棋的点。play函数负责创建棋子.turn负责判断黑子还是白子。
public class gametable : MonoBehaviour
{
public int[,] grid;
public ChessType turn;
public GameObject[] prefabs;
private static gametable instance;
public static gametable Instance
{
get
{
return instance;
}
}
private void Awake()
{
grid = new int[15,15];
if (instance == null)
{
instance = this;
}
}
private void Update()
{
}
public void Play(int[] pos)
{
grid = new int[15, 15];
if (grid[pos[0], pos[1]]!=0)return;
if (turn == ChessType.Black)
{
Instantiate(prefabs[0], new Vector3(pos[0], pos[1], 0), Quaternion.identity);
turn = ChessType.White;
}
else if (turn == ChessType.White)
{
Instantiate(prefabs[1], new Vector3(pos[0], pos[1], 0), Quaternion.identity);
turn = ChessType.Black;
}
}
public enum ChessType
{
Watch,
White,
Black
}
}
4设置timer时间控制器,以及在grid中记录位置。
public class gametable : MonoBehaviour
{
public int[,] grid;
public ChessType turn;
public GameObject[] prefabs;
private static gametable instance;
public float timer=0;
public static gametable Instance
{
get
{
return instance;
}
}
private void Awake()
{
grid = new int[15,15];
if (instance == null)
{
instance = this;
}
}
private void Update()
{
timer += Time.deltaTime;
}
public void Play(int[] pos)
{
if (grid[pos[0], pos[1]]!=0)return;
if (turn == ChessType.Black)
{
Instantiate(prefabs[0], new Vector3(pos[0], pos[1], 0), Quaternion.identity);
grid[pos[0], pos[1]] = (int)ChessType.Black;
turn = ChessType.White;
}
else if (turn == ChessType.White)
{
Instantiate(prefabs[1], new Vector3(pos[0], pos[1], 0), Quaternion.identity);
grid[pos[0], pos[1]] = (int)ChessType.White;
turn = ChessType.Black;
}
}
public enum ChessType
{
Watch,
White,
Black
}
}
5在play脚本中判断下是否该自己下棋。
public class Player : MonoBehaviour
{
public gametable.ChessType chessType;
void Update()
{
if(gametable.Instance.timer>0.3f)
PlayChess();
}
void PlayChess()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (pos.x > 14 || pos.x < 0 || pos.y > 14 ||
pos.y<0||chessType!=gametable.Instance.turn) return;
pos.x += (float) 0.5;pos.y += (float)(0.5);
Debug.Log((int)pos.x+" "+(int)pos.y);
gametable.Instance.Play(new int[2] { (int)pos.x, (int)pos.y });
gametable.Instance.timer = 0;
}
}
}
6最后设置两个空物体player 将chesstype分别设置为white black就可以依次下棋了。
7判断5子胜负以及AI的实现放到下一篇继续。
总结:五子棋实现的问题主要在于如何分辨黑白子,记录每个棋子的位置,防止重复下棋,或者超出界外,控制每一步下棋的时间,防止秒下,以及一些bug的出现,优化下棋体验。
边栏推荐
- MySQL LIMIT + order by limit n,m 和 limit n的小坑
- 【Word】Add subscripts to the text of Word at the same time
- S7-200SMART PLC Modbus TCP communication
- Regular Expression - Determine if a string matches the "AABB" pattern
- tidb 宕机测试
- LDO和DC-DC的区别
- 声母-字母查询工具-词语缩写查询在线工具
- 2022牛客多校联赛第七场 题解
- 著名数字藏品专家、数藏大会创始人高泽龙接受中国企业家杂志采访
- Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
猜你喜欢
Used to import the data type
list 字符串的输出方法 print(*a) print(““.join(str(c) for c in a) )
筑牢安全“防火墙”河南通许县冯庄乡开展消防培训
feof它可不简单。
harbor企业级镜像仓库搭建
牛客每日刷题之链表
想要精准营销,从学习搭建一套对的标签体系开始丨DTVision分析洞察篇
MYSQL高级篇-----查询截取分析,锁机制,主从复制
How to automatically fill down an excel table without dragging the mouse down
年薪35W的测试工程师被裁亲身经验:不得不听的忠告
随机推荐
feof它可不简单。
51串口通信(上)
[mysql database] the use of mysql database
pytorch实现GAN入门案例
契约测试(上):什么是契约测试
声母-字母查询工具-词语缩写查询在线工具
具有CT造影功能的硫化铋纳米棒|硫化铋-锌原卟啉复合材料(PAMAM/Bi2S3复合纳米粒子)
直播电商平台开发,点击查看更多显示所有内容
年薪35W的测试工程师被裁亲身经验:不得不听的忠告
[MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
Distributed timing task framework xxl-job source code analysis
废品回收小程序、APP UNIAPP开发带有用户端和回收员端
软件测试工程师简历要怎么写,才能让HR看到
AIOT介绍
Spark源码-任务提交流程-8-DAGScheduler任务切分
Xilinx Zynq ZynqMP DNA
多字段关联校验
kubernetes 安全
Getting Started with MATLAB Image Processing
shell函数、数组