cocos2d-x游戏开发系列教程-中国象棋04-摆棋

2024-02-28 19:59

本文主要是介绍cocos2d-x游戏开发系列教程-中国象棋04-摆棋,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

前情回顾

在之前的学习中,我们已经了解到,下棋主界面是由CCMainMenu类实现的,在它的init函数中,初始化了

主界面需要的各种数据,包括:创建控件,初始化32个棋子,初始化执行变量等等,在这个博文中,我们

主要来学习,32个棋子是如何被初始化并显示在界面上的。这个显示和initCoordinate以及

了解32个棋子显示在界面的目的,是为了让大家更好理解32个棋子的数据结构,这些数据结构在当棋子走动

时,需要改动,所以必须要理解。


initCoordinate

void CCMainMenu::initCoordinate(){ /*std::string str = "../coordinate.txt"; std::ofstream f_output; f_output.open(str.c_str(), std::ios::app);*/ float xSpan  = 48.0f*0.667ffloat ySpan  = 32.0f*0.6ffloat xStart = 1float yStart = xSpan; for (int i = 0, x = 9; i < 10; ++i, --x) {  for (int j = 0, y = 1; j < 9; ++j, ++y)  {   g_chess_coord[i][j] = ccp(xStart + xSpan*y, yStart*x + ySpan);  // 计算象棋坐标点的坐标,把象棋坐标转化成view坐标   /*f_output<< "[" << i << "][" << j <<"]: (" << xStart + xSpan*y << ", " << yStart*x + ySpan << ")" << " ";   if(j == 4 || j == 8)   {    f_output<<std::endl;   }*/  }  /*f_output<<std::endl;*/ } /*f_output.close();*/}

这个函数功能是将象棋的坐标转化成屏幕坐标,并保存在g_chess_coord中。有了这些坐标,为后面的摆棋工作打下基础。

initListImage

initListImage函数,初始化了32个棋子的位置,代码的逻辑很简单,就是调用32次getListSprite,获得32个棋子精灵并保存在m_pChess中

然后调用initChessPosition去设置32个棋子的位置,代码在MainMenu.cpp中

void CCMainMenu::initListImage(){ // red m_pChess[1][0] = this->getListSprite(CHESS_RK); this->addChild(m_pChess[1][0], 0); m_pChess[1][1] = this->getListSprite(CHESS_RA); this->addChild(m_pChess[1][1], 0); m_pChess[1][2] = this->getListSprite(CHESS_RA); this->addChild(m_pChess[1][2], 0); m_pChess[1][3] = this->getListSprite(CHESS_RB); this->addChild(m_pChess[1][3], 0); m_pChess[1][4] = this->getListSprite(CHESS_RB); this->addChild(m_pChess[1][4], 0); m_pChess[1][5] = this->getListSprite(CHESS_RN); this->addChild(m_pChess[1][5], 0); m_pChess[1][6] = this->getListSprite(CHESS_RN); this->addChild(m_pChess[1][6], 0); m_pChess[1][7] = this->getListSprite(CHESS_RR); this->addChild(m_pChess[1][7], 0); m_pChess[1][8] = this->getListSprite(CHESS_RR); this->addChild(m_pChess[1][8], 0); m_pChess[1][9] = this->getListSprite(CHESS_RC); this->addChild(m_pChess[1][9], 0);  m_pChess[1][10] = this->getListSprite(CHESS_RC); this->addChild(m_pChess[1][10], 0); m_pChess[1][11] = this->getListSprite(CHESS_RP); this->addChild(m_pChess[1][11], 0); m_pChess[1][12] = this->getListSprite(CHESS_RP); this->addChild(m_pChess[1][12], 0); m_pChess[1][13] = this->getListSprite(CHESS_RP); this->addChild(m_pChess[1][13], 0); m_pChess[1][14] = this->getListSprite(CHESS_RP); this->addChild(m_pChess[1][14], 0); m_pChess[1][15] = this->getListSprite(CHESS_RP); this->addChild(m_pChess[1][15], 0); //black m_pChess[0][0] = this->getListSprite(CHESS_BK); this->addChild(m_pChess[0][0], 0); m_pChess[0][1] = this->getListSprite(CHESS_BA); this->addChild(m_pChess[0][1], 0); m_pChess[0][2] = this->getListSprite(CHESS_BA); this->addChild(m_pChess[0][2], 0); m_pChess[0][3] = this->getListSprite(CHESS_BB); this->addChild(m_pChess[0][3], 0); m_pChess[0][4] = this->getListSprite(CHESS_BB); this->addChild(m_pChess[0][4], 0); m_pChess[0][5] = this->getListSprite(CHESS_BN); this->addChild(m_pChess[0][5], 0); m_pChess[0][6] = this->getListSprite(CHESS_BN); this->addChild(m_pChess[0][6], 0); m_pChess[0][7] = this->getListSprite(CHESS_BR); this->addChild(m_pChess[0][7], 0); m_pChess[0][8] = this->getListSprite(CHESS_BR); this->addChild(m_pChess[0][8], 0); m_pChess[0][9] = this->getListSprite(CHESS_BC); this->addChild(m_pChess[0][9], 0); m_pChess[0][10] = this->getListSprite(CHESS_BC); this->addChild(m_pChess[0][10], 0); m_pChess[0][11] = this->getListSprite(CHESS_BP); this->addChild(m_pChess[0][11], 0); m_pChess[0][12] = this->getListSprite(CHESS_BP); this->addChild(m_pChess[0][12], 0); m_pChess[0][13] = this->getListSprite(CHESS_BP); this->addChild(m_pChess[0][13], 0); m_pChess[0][14] = this->getListSprite(CHESS_BP); this->addChild(m_pChess[0][14], 0); m_pChess[0][15] = this->getListSprite(CHESS_BP); this->addChild(m_pChess[0][15], 0); this->initChessPosition();}

getListSprite

这个函数负责创建棋子精灵,根据不同的棋子类型,使用不同的图片创建精灵,在你们自己的环境中,尤其注意图片路径

图片路劲不对,那什么都不显示,所以如果有错,先考虑是不是图片路径不对了。

CCSprite* CCMainMenu::getListSprite(CHESS_TYPE nType){ CCSprite* pSprite; switch(nType) { case CHESS_RK:  pSprite = CCSprite::create(RES_PATH"rk.png");  breakcase CHESS_RA:  pSprite = CCSprite::create(RES_PATH"ra.png");  breakcase CHESS_RB:   pSprite = CCSprite::create(RES_PATH"rb.png");  breakcase CHESS_RN:   pSprite = CCSprite::create(RES_PATH"rn.png");  breakcase CHESS_RR:   pSprite = CCSprite::create(RES_PATH"rr.png");  breakcase CHESS_RC:  pSprite = CCSprite::create(RES_PATH"rc.png");  breakcase CHESS_RP:   pSprite = CCSprite::create(RES_PATH"rp.png");  breakcase CHESS_BK:  pSprite = CCSprite::create(RES_PATH"bk.png");  breakcase CHESS_BA:  pSprite = CCSprite::create(RES_PATH"ba.png");  breakcase CHESS_BB:   pSprite = CCSprite::create(RES_PATH"bb.png");  breakcase CHESS_BN:   pSprite = CCSprite::create(RES_PATH"bn.png");  breakcase CHESS_BR:   pSprite = CCSprite::create(RES_PATH"br.png");  breakcase CHESS_BC:  pSprite = CCSprite::create(RES_PATH"bc.png");  breakcase CHESS_BP:  pSprite = CCSprite::create(RES_PATH"bp.png");  break; } pSprite->setScaleX(0.5f); pSprite->setScaleY(0.5f); return pSprite;}

initChessPosition

initListImage最后调用的是initChessPosition,为32个棋子指定位置。这里位置信息用到了g_chess_coord,这个坐标信息是之前函数

void CCMainMenu::initChessPosition(){ m_pChess[1][0]->setPosition(g_chess_coord[9][4]); m_pChess[1][1]->setPosition(g_chess_coord[9][3]); m_pChess[1][2]->setPosition(g_chess_coord[9][5]); m_pChess[1][3]->setPosition(g_chess_coord[9][2]); m_pChess[1][4]->setPosition(g_chess_coord[9][6]); m_pChess[1][5]->setPosition(g_chess_coord[9][1]); m_pChess[1][6]->setPosition(g_chess_coord[9][7]); m_pChess[1][7]->setPosition(g_chess_coord[9][0]); m_pChess[1][8]->setPosition(g_chess_coord[9][8]); m_pChess[1][9]->setPosition(g_chess_coord[7][1]); m_pChess[1][10]->setPosition(g_chess_coord[7][7]); m_pChess[1][11]->setPosition(g_chess_coord[6][0]); m_pChess[1][12]->setPosition(g_chess_coord[6][2]); m_pChess[1][13]->setPosition(g_chess_coord[6][4]); m_pChess[1][14]->setPosition(g_chess_coord[6][6]); m_pChess[1][15]->setPosition(g_chess_coord[6][8]); //black m_pChess[0][0]->setPosition(g_chess_coord[0][4]); m_pChess[0][1]->setPosition(g_chess_coord[0][3]); m_pChess[0][2]->setPosition(g_chess_coord[0][5]); m_pChess[0][3]->setPosition(g_chess_coord[0][2]); m_pChess[0][4]->setPosition(g_chess_coord[0][6]); m_pChess[0][5]->setPosition(g_chess_coord[0][1]); m_pChess[0][6]->setPosition(g_chess_coord[0][7]); m_pChess[0][7]->setPosition(g_chess_coord[0][0]); m_pChess[0][8]->setPosition(g_chess_coord[0][8]); m_pChess[0][9]->setPosition(g_chess_coord[2][1]); m_pChess[0][10]->setPosition(g_chess_coord[2][7]); m_pChess[0][11]->setPosition(g_chess_coord[3][0]); m_pChess[0][12]->setPosition(g_chess_coord[3][2]); m_pChess[0][13]->setPosition(g_chess_coord[3][4]); m_pChess[0][14]->setPosition(g_chess_coord[3][6]); m_pChess[0][15]->setPosition(g_chess_coord[3][8]);}

好,当程序完全执行完这些代码后,就完成了32个棋子的初始化。

下一遍博文我们讲讲述这个象棋是怎么走起来


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述

这篇关于cocos2d-x游戏开发系列教程-中国象棋04-摆棋的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/756468

相关文章

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

全网最全Tomcat完全卸载重装教程小结

《全网最全Tomcat完全卸载重装教程小结》windows系统卸载Tomcat重新通过ZIP方式安装Tomcat,优点是灵活可控,适合开发者自定义配置,手动配置环境变量后,可通过命令行快速启动和管理... 目录一、完全卸载Tomcat1. 停止Tomcat服务2. 通过控制面板卸载3. 手动删除残留文件4.

Python的pandas库基础知识超详细教程

《Python的pandas库基础知识超详细教程》Pandas是Python数据处理核心库,提供Series和DataFrame结构,支持CSV/Excel/SQL等数据源导入及清洗、合并、统计等功能... 目录一、配置环境二、序列和数据表2.1 初始化2.2  获取数值2.3 获取索引2.4 索引取内容2

python依赖管理工具UV的安装和使用教程

《python依赖管理工具UV的安装和使用教程》UV是一个用Rust编写的Python包安装和依赖管理工具,比传统工具(如pip)有着更快、更高效的体验,:本文主要介绍python依赖管理工具UV... 目录前言一、命令安装uv二、手动编译安装2.1在archlinux安装uv的依赖工具2.2从github

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建