quick cocos2d-x 入门---井字棋

quick cocos2d-x 入门---井字棋,第1张

概述学习quick cocos2d-x 第二天 ,使用quick-x 做了一个井字棋游戏 。 我假设读者已经 http://wiki.quick-x.com/doku.php?id=zh_cn阅读了这个链接下的内容 ,并学会了如何搭建环境和创建新的工程,并假高读者有一定cocos2d-x基础  建议读者多研究一下quick-x自带的例子coinflip。并阅读framework下的lua源码,尤其注意 学习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

function MainScene:ctor()
self.bg = display.newSprite("board.png",display.cx,display.cy)
self:addChild(self.bg)

-- keypad layer,for androID
self.layer = Board.new()
self:addChild(self.layer)
end

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

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

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

2.定义一个Layer

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

定义层的方法为:

在Board.lua文件 中

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

return Board

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

3.如何增加touch事件

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

self:addtouchEventListener(handler(self,self.ontouch))self:setNodeEventEnabled(true)

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

function Board:onEnter()    self:settouchEnabled(true)endfunction Board:onExit()    self:removeAllEventListeners()end

3.3 在Board:ontouch中处理事件

function Board:ontouch(event,x,y)    //Todo 处理点击事件end

二。定义数据

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

myBoard = {{"-","-","-"},{"-","-"}}theWiner = "-1"

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

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

三。程序流程

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

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

turn = "O"function Board:makeMove(x,y)    if theWiner ~= "-1" then        return     end    row,co = self:getBoardLocation(x,y)    if row == -1 then         return    end    self:makeEle(row,co)    endfunction Board:makeEle(row,co)    local file = "pIEce_o.png"    if turn == "X" then         file = "pIEce_x.png"    else        file = "pIEce_o.png"       end              myBoard[row][co] = turn;       self.ele = display.newSprite(file,display.cx+100*(co-2),display.cy+100*(2-row))       self:addChild(self.ele)    local ret = Board:winCheck(row,co)    print("winCheck",ret)        if ret == "O" then        self.lable:setString("O is the winer")    end     if ret == "X" then        self.lable:setString("X is the winner")    end     if ret == "He" then         self.lable:setString("No one is the winner")    end     if ret == "Wh" then         self.lable:setString("Continue")    end        if turn == "X" then         turn = "O"    else        turn = "X"       endend
function Board:getBoardLocation(x,y)    if x < display.cx-150 or x >display.cx+150 then         return  -1    end    if y > display.cy+150 or y < display.cy-150 then        return  -1    end        local co     if x <= display.cx - 50  then         co = 1    elseif x > display.cx-50 and x < display.cx+50 then        co = 2    else         co = 3    end         local row    if y <= display.cy - 50  then         row = 3    elseif y > display.cy-50 and y < display.cy+50 then        row = 2    else         row = 1    end     return row,co    end

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

function Board:winCheck(row,co)    local cur = myBoard[row][co]        if myBoard[1][2] == cur and myBoard[1][3] == cur and myBoard[1][1] ==cur  then        return cur    end        if myBoard[2][2] == cur and myBoard[2][3] == cur and myBoard[2][1]  == cur then        return cur    end    if myBoard[3][2] == cur and myBoard[3][3] == cur and myBoard[3][1] == cur then        return cur    end    if myBoard[1][1] == cur and myBoard[2][1] == cur and myBoard[3][1] ==  cur then        return cur    end    if myBoard[1][2] == cur and myBoard[2][2] == cur and myBoard[3][2] == cur then        return cur    end    if myBoard[1][3] == cur and myBoard[2][3] == cur and myBoard[3][3] == cur then        return cur    end        if myBoard[1][1] == cur and myBoard[2][2] == cur and myBoard[3][3] == cur  then        return cur    end        if myBoard[1][3] == cur and myBoard[2][2] == cur and myBoard[3][1] == cur then        return cur    end        open = true;    for i = 1,3 do         for j = 1,3 do             if myBoard[i][j] == "-" then                 open = false            end        end    end        if open then         return "He"    else         return "Wh"    endend

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

总结

以上是内存溢出为你收集整理的quick cocos2d-x 入门---井字棋全部内容,希望文章能够帮你解决quick cocos2d-x 入门---井字棋所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1082673.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存