学习quick cocos2d-x 第二天 ,使用quick-x 做了一个井字棋游戏 。

我假设读者已经 http://wiki.quick-x.com/doku.php?id=zh_cn阅读了这个链接下的内容 ,并学会了如何搭建环境和创建新的工程,并假高读者有一定cocos2d-x基础

建议读者多研究一下quick-x自带的例子coinflip。并阅读framework下的lua源码,尤其注意用lua模拟出面象对象的部分(可参考《Lua程序设计》第二版的13,16两章)。

一。准备工作:

1.如何在场景(层)添加一个Sprite

我们在MainScene中添加一个Sprite

  1. function MainScene:ctor()
  2. self.bg = display.newSprite("board.png", display.cx, display.cy)
  3. self:addChild(self.bg)
  4. -- keypad layer, for android
  5. self.layer = Board.new()
  6. self:addChild(self.layer)
  7. end

display 是处理显示有关的一个“类”。

newSprite则类似cocos2d-x中的CCSprite::create()

注意:Lua中除用local 修饰的变量都是全局变量。我们self.bg这样定义,而不直接定义,目的是不污染全局环境,和把bg作为MainScene“类”(其实是表)的一个变量。

2.定义一个Layer

Board是我定义的一个层,添加在MainScene上。

定义层的方法为:

在Board.lua文件 中

  1. local Board = class("Board", function()
  2. return display.newLayer()
  3. end)

return Board

大家可以到framework下看看class是如何实现的。

3.如何增加touch事件

3.1在 Board:actor中增加以下代码

  1. self:addTouchEventListener(handler(self, self.onTouch))
  2. self:setNodeEventEnabled(true)

3.2 在onEnter,onExit中分别设置和移除相关事件监听

  1. function Board:onEnter()
  2. self:setTouchEnabled(true)
  3. end
  4.  
  5. function Board:onExit()
  6. self:removeAllEventListeners()
  7. end

3.3 在Board:onTouch中处理事件

  1. function Board:onTouch(event, x, y)
  2. //TODO 处理点击事件
  3. end

二。定义数据 

我使用一个2维的表来描述整个棋盘(也可以使用一维表)

  1. myBoard = {{"-","-","-"},
  2. {"-","-","-"},
  3. {"-","-","-"}}
  4. theWiner = "-1"

myBoard即棋盘,“-1”表示没有棋子,“X”表示有“X”形棋子,“O”表示有“O”型棋子。

theWiner表示获胜者,初始为-1。

三。程序流程

1.玩家点击事件后,在相应的位置放置棋子,并修改myBoard数据

比如简单,直接附代码了,写的比较粗糙,因为 也刚学Lua才两三天。

  1. turn = "O"
  2. function Board:makeMove(x,y)
  3. if theWiner ~= "-1" then
  4. return
  5. end
  6. row,co = self:getBoardLocation(x,y)
  7. if row == - then
  8. return
  9. end
  10. self:makeEle(row,co)
  11.  
  12. end
  13. function Board:makeEle(row,co)
  14. local file = "piece_o.png"
  15. if turn == "X" then
  16. file = "piece_x.png"
  17. else
  18. file = "piece_o.png"
  19. end
  20.  
  21. myBoard[row][co] = turn;
  22. self.ele = display.newSprite(file, display.cx+*(co-) , display.cy+*(-row))
  23. self:addChild(self.ele)
  24. local ret = Board:winCheck(row,co)
  25. print("winCheck",ret)
  26.  
  27. if ret == "O" then
  28. self.lable:setString("O is the winer")
  29. end
  30. if ret == "X" then
  31. self.lable:setString("X is the winner")
  32. end
  33. if ret == "He" then
  34. self.lable:setString("No one is the winner")
  35. end
  36. if ret == "Wh" then
  37. self.lable:setString("Continue")
  38. end
  39.  
  40. if turn == "X" then
  41. turn = "O"
  42. else
  43. turn = "X"
  44. end
  45. end
  1. function Board:getBoardLocation(x,y)
  2. if x < display.cx- or x >display.cx+ then
  3. return -
  4. end
  5. if y > display.cy+ or y < display.cy- then
  6. return -
  7. end
  8.  
  9. local co
  10. if x <= display.cx - then
  11. co =
  12. elseif x > display.cx- and x < display.cx+ then
  13. co =
  14. else
  15. co =
  16. end
  17.  
  18. local row
  19. if y <= display.cy - then
  20. row =
  21. elseif y > display.cy- and y < display.cy+ then
  22. row =
  23. else
  24. row =
  25. end
  26.  
  27. return row,co
  28.  
  29. end

2.检查玩家是否获胜或平局

  1. function Board:winCheck(row,co)
  2. local cur = myBoard[row][co]
  3.  
  4. if myBoard[][] == cur and myBoard[][] == cur and myBoard[][] ==cur then
  5. return cur
  6. end
  7.  
  8. if myBoard[][] == cur and myBoard[][] == cur and myBoard[][] == cur then
  9. return cur
  10. end
  11.  
  12. if myBoard[][] == cur and myBoard[][] == cur and myBoard[][] == cur then
  13. return cur
  14. end
  15.  
  16. if myBoard[][] == cur and myBoard[][] == cur and myBoard[][] == cur then
  17. return cur
  18. end
  19. if myBoard[][] == cur and myBoard[][] == cur and myBoard[][] == cur then
  20. return cur
  21. end
  22. if myBoard[][] == cur and myBoard[][] == cur and myBoard[][] == cur then
  23. return cur
  24. end
  25.  
  26. if myBoard[][] == cur and myBoard[][] == cur and myBoard[][] == cur then
  27. return cur
  28. end
  29.  
  30. if myBoard[][] == cur and myBoard[][] == cur and myBoard[][] == cur then
  31. return cur
  32. end
  33.  
  34. open = true;
  35. for i = , do
  36. for j = , do
  37. if myBoard[i][j] == "-" then
  38. open = false
  39. end
  40. end
  41. end
  42.  
  43. if open then
  44. return "He"
  45. else
  46. return "Wh"
  47. end
  48. end

搞了一天,有点累了,写的不详细,有问题请大家在评论里问吧

quick cocos2d-x 入门---井字棋的更多相关文章

  1. 程序设计入门—Java语言 第五周编程题 2井字棋(5分)

    2 井字棋(5分) 题目内容: 嗯,就是视频里说的那个井字棋.视频里说了它的基本思路,现在,需要你把它全部实现出来啦. 你的程序先要读入一个整数n,范围是[3,100],这表示井字棋棋盘的边长.比如n ...

  2. TicTacToe井字棋 by reinforcement learning

    对于初学强化学习的同学,数学公式也看不太懂, 一定希望有一些简单明了的代码实现加强对入门强化学习的直觉认识,这是一篇初级入门代码, 希望能对你们开始学习强化学习起到基本的作用. 井字棋具体玩法参考百度 ...

  3. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  4. [C++] 井字棋游戏源码

    TicTac.h #define EX 1 //该点左鼠标 #define OH 2 //该点右鼠标 class CMyApp : public CWinApp { public: virtual B ...

  5. [游戏学习22] MFC 井字棋 双人对战

    >_<:太多啦,感觉用英语说的太慢啦,没想到一年做的东西竟然这么多.....接下来要加速啦! >_<:注意这里必须用MFC和前面的Win32不一样啦! >_<:这也 ...

  6. 井字棋(Tic-Tac-Toe)

    井字棋介绍:https://en.wikipedia.org/wiki/Tic-tac-toe 井字棋简单,但是获胜策略却和直觉不同,四角比中间重要性要高,而且先手有很大的获胜概率获胜(先手胜:91, ...

  7. [HTML5实现人工智能]小游戏《井字棋》发布,据说IQ上200才能赢

    一,什么是TicTacToe(井字棋)   本 游戏 为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿 ...

  8. [LeetCode] Valid Tic-Tac-Toe State 验证井字棋状态

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  9. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

随机推荐

  1. dp与px转换

    名词 解释 Px (Pixel像素) 不同设备显示效果相同.这里的“相同”是指像素数不会变,比如指定UI长度是100px,那不管分辨率是多少UI长度都是100px.也正是因为如此才造成了UI在小分辨率 ...

  2. socket阻塞与非阻塞,同步与异步

    socket阻塞与非阻塞,同步与异步 作者:huangguisu 转自:http://blog.csdn.net/hguisu/article/details/7453390 1. 概念理解 在进行网 ...

  3. De4Dot+Reflector 支持多种反混淆

    官网: http://www.de4dot.com/ 源码:https://github.com/brianhama/de4dot 使用方法 通过CMD命令方式进入: F:\2\de4dot-v3-1 ...

  4. jQuery 的属性

    一.显示和隐藏的属性  hide(隐藏),show(显示) 下面是例子 <script type="text/javascript"> $(document).read ...

  5. Ueditor 编译发布后无法使用上传图片、附件等功能

    Ueditor 发布后上传到服务器会出现无法使用上传功能,在本地源代码模式下上传功能正常,这是因为在网站发布期间把 net/Uploader.cs 给编译了,发布后的代码不包含Uploader.cs故 ...

  6. Oracle 分页原理

    oracle rownum 及分页处理的使用方法 在实际应用中我们经常碰到这样的问题,比如一张表比较大,我们只要其中的查看其中的前几条数据,或者对分页处理数据.在这些情况下我们都需要用到rownum. ...

  7. [转载]Docker的安装配置及使用详解

    简介    官网:http://www.docker.com/,点击get started进入下载,目前三个系统的docker容器都有,Windows版需要win10系统,我的是win7系统一开始用的 ...

  8. XHTML标签的嵌套规则分析

    在 XHTML 的语言里,我们都知道:ul 标签包含着 li.dl 标签包含着 dt 和 dd——这些固定标签的嵌套规则十分明确.但是,还有许多标签是独立的,它们没有被捆绑在一起,比如 h1.div. ...

  9. WNDR3700V4恢复原厂固件(使用TFTP刷网件原厂固件)

    WNDR3700v4原厂固件下载地址: http://support.netgear.cn/doucument/More.asp?id=2203 操作方法: 1.将设备断电: 2.按住设备背面的Res ...

  10. 深入理解Java虚拟机(一)、Java内存区域与内存溢出异常

    Java虚拟机所管理的内存包括以下几个运行时数据区: 程序计数器(PCR): 1.是一块较小的内存空间,可以看做是当前线程所执行的字节码的行号指示器 2.为线程私有 3.执行Java方法有PCR,执行 ...