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

前端之家收集整理的这篇文章主要介绍了quick cocos2d-x 入门---井字棋前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
学习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@H_301_59@()
    return@H_301_59@ display.newLayer()
end)

return Board

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

3.如何增加touch事件

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

@H_301_59@self:addTouchEventListener(handler(self,self.onTouch))
self:setNodeEventEnabled(true)

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

function@H_301_59@ Board:onEnter()
    self:setTouchEnabled(true@H_301_59@)
end

function@H_301_59@ Board:onExit()
    self:removeAllEventListeners()
end

3.3 在Board:onTouch中处理事件

function@H_301_59@ Board:onTouch(event,x,y)
    //@H_301_59@TODO 处理点击事件
end

二。定义数据

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

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

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

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

三。程序流程

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

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

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

    return@H_301_59@ row,co
    
end

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

function@H_301_59@ Board:winCheck(row,co)
    local cur =@H_301_59@ myBoard[row][co]
    
    if myBoard[1][2] == cur and myBoard[1][3] == cur and myBoard[1][1] ==cur  then
        return@H_301_59@ cur
    end
    
    if myBoard[2][2] == cur and myBoard[2][3] == cur and myBoard[2][1]  == cur then
        return@H_301_59@ cur
    end

    if myBoard[3][2] == cur and myBoard[3][3] == cur and myBoard[3][1] == cur then
        return@H_301_59@ cur
    end

    if myBoard[1][1] == cur and myBoard[2][1] == cur and myBoard[3][1] ==  cur then
        return@H_301_59@ cur
    end
    if myBoard[1][2] == cur and myBoard[2][2] == cur and myBoard[3][2] == cur then
        return@H_301_59@ cur
    end
    if myBoard[1][3] == cur and myBoard[2][3] == cur and myBoard[3][3] == cur then
        return@H_301_59@ cur
    end

    
    if myBoard[1][1] == cur and myBoard[2][2] == cur and myBoard[3][3] == cur  then
        return@H_301_59@ cur
    end

    
    if myBoard[1][3] == cur and myBoard[2][2] == cur and myBoard[3][1] == cur then
        return@H_301_59@ cur
    end@H_301_59@
    
    open = true@H_301_59@;
    for i = 1,3 do 
        for j = 1,3 do 
            if myBoard[i][j] == "-" then@H_301_59@ 
                open = false
            end
        end
    end
    
    if open then 
        return "He"
    else 
        return "Wh"
    end
end

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

猜你在找的Cocos2d-x相关文章