当前位置:网站首页>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的出现,优化下棋体验。
边栏推荐
- Shell:数组和函数用法
- 筑牢安全“防火墙”河南通许县冯庄乡开展消防培训
- command,shell,raw,script模块的作用和区别,file、copy、fetch、synchronize的应用
- Transaction rolled back because it has been marked as rollback-only
- Chapter7、基于图像的光照
- 什么是SIP请求和SIP响应?
- 【Word】Word 正文中同时加上下标
- list 字符串的输出方法 print(*a) print(““.join(str(c) for c in a) )
- 明明加了唯一索引,为什么还是产生重复数据?
- 分布式定时任务框架 xxl-job 源码解析
猜你喜欢
SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
MATLAB图像处理入门
JDBC_PreparedStatement预编译对象
著名数字藏品专家、数藏大会创始人高泽龙接受中国企业家杂志采访
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
5年测试开发工程师感悟——写给还在迷茫中的朋友
想要精准营销,从学习搭建一套对的标签体系开始丨DTVision分析洞察篇
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
excel表格如何不需鼠标往下拖动而自动往下填
Three Musketeers Advanced
随机推荐
著名数字藏品专家、数藏大会创始人高泽龙接受中国企业家杂志采访
harbor企业级镜像仓库搭建
The 24th day of the special assault version of the sword offer
Three Musketeers Advanced
Getting Started with MATLAB Image Processing
Used to import the data type
Xilinx Zynq ZynqMP DNA
多行字符串排序在线工具
废品回收小程序、APP UNIAPP开发带有用户端和回收员端
The difference between LDO and DC-DC
untiy 倒计时
Transaction rolled back because it has been marked as rollback-only
feof它可不简单。
cglib获取不到接口注解
P8462 「REOI-1」奶油蛋糕
Polyamide-amine (PAMAM) dendrimer-bismuth sulfide composite nanoparticles | bismuth sulfide modified Gd‑DTPA‑OA ligand | for scientific research
RNN-T
获取开发版安全码SHA1时遇到的报错
明明加了唯一索引,为什么还是产生重复数据?
phpstudy install flarum forum