學(xué)習(xí)quick cocos2d-x 第二天 ,使用quick-x 做了一個(gè)井字棋游戲 。 我假設(shè)讀者已經(jīng) http://wiki./doku.php?id=zh_cn閱讀了這個(gè)鏈接下的內(nèi)容 ,并學(xué)會(huì)了如何搭建環(huán)境和創(chuàng)建新的工程,并假高讀者有一定cocos2d-x基礎(chǔ) 建議讀者多研究一下quick-x自帶的例子coinflip。并閱讀framework下的lua源碼,尤其注意用lua模擬出面象對(duì)象的部分(可參考《Lua程序設(shè)計(jì)》第二版的13,16兩章)。
一。準(zhǔn)備工作: 1.如何在場(chǎng)景(層)添加一個(gè)Sprite 我們?cè)贛ainScene中添加一個(gè)Sprite function MainScene:ctor() -- keypad layer, for android display 是處理顯示有關(guān)的一個(gè)“類”。 newSprite則類似cocos2d-x中的CCSprite::create()
注意:Lua中除用local 修飾的變量都是全局變量。我們self.bg這樣定義,而不直接定義,目的是不污染全局環(huán)境,和把bg作為MainScene“類”(其實(shí)是表)的一個(gè)變量。 2.定義一個(gè)Layer Board是我定義的一個(gè)層,添加在MainScene上。 定義層的方法為: 在Board.lua文件 中 local Board = class("Board", function() return display.newLayer() end) return Board 大家可以到framework下看看class是如何實(shí)現(xiàn)的。 3.如何增加touch事件 3.1在 Board:actor中增加以下代碼 self:addTouchEventListener(handler(self, self.onTouch)) self:setNodeEventEnabled(true) 3.2 在onEnter,onExit中分別設(shè)置和移除相關(guān)事件監(jiān)聽 function Board:onEnter() self:setTouchEnabled(true) end function Board:onExit() self:removeAllEventListeners() end 3.3 在Board:onTouch中處理事件
function Board:onTouch(event, x, y) //TODO 處理點(diǎn)擊事件 end
二。定義數(shù)據(jù) 我使用一個(gè)2維的表來描述整個(gè)棋盤(也可以使用一維表) myBoard = {{"-","-","-"}, {"-","-","-"}, {"-","-","-"}} theWiner = "-1" myBoard即棋盤,“-1”表示沒有棋子,“X”表示有“X”形棋子,“O”表示有“O”型棋子。 theWiner表示獲勝者,初始為-1。
三。程序流程 1.玩家點(diǎn)擊事件后,在相應(yīng)的位置放置棋子,并修改myBoard數(shù)據(jù) 比如簡單,直接附代碼了,寫的比較粗糙,因?yàn)?也剛學(xué)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) end function 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" end end 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" end end
搞了一天,有點(diǎn)累了,寫的不詳細(xì),有問題請(qǐng)大家在評(píng)論里問吧
|
|