吳曉雪 何南 繆新穎 王魏
關鍵詞: 貪吃蛇; Cocos2d?x; 手機游戲; LUA; 邏輯功能設計; 碰撞檢測
中圖分類號: TN912.3?34; TP311.5 ? ? ? ? ? ? ? 文獻標識碼: A ? ? ? ? ? ? ? ? ?文章編號: 1004?373X(2018)24?0106?04
Design and application of mobile game based on Cocos2d?x
WU Xiaoxue, HE Nan, MIAO Xinying, WANG Wei
(School of Information Engineering, Dalian Ocean University, Dalian 116023, China)
Abstract: A relaxation puzzle game called Gluttonous Snake suitable for the Android system is developed in the Visual Studio 2017 development environment by using the Cocos2d?x game engine and combining with the LUA script language. The overall framework of Android game development is proposed. The key technologies involved in Android game development are summarized, including the development environment configuration technology, logic function design technology and collision detection technology. Part of source codes of the Cocos2d?x mobile phone game engine are deeply analyzed, and the codes unsuitable for framework establishment of the Gluttonous Snake game are improved by using ways like inheritance and rewriting. The test results show that the system can achieve the basic functions of Gluttonous Snake, and has a certain reference value for design and implementation of the mobile phone game based on Cocos2d?x.
Keywords: Gluttonous Snake; Cocos2d?x; mobile phone game; LUA; logic function design; collision detection
Android是Google開發(fā)基于Linux平臺的開源手機操作系統(tǒng),包括操作系統(tǒng)、用戶界面和應用程序[1]。手機游戲作為Android系統(tǒng)平臺的一個重要組成部分,正在被越來越多的人重視,如何在手機平臺上開發(fā)一款高效的系統(tǒng)框架成為主要考慮的因素[2]。
Cocos2d?x正是為提高游戲開發(fā)效率而誕生的一款優(yōu)秀的游戲引擎,并且已經(jīng)得到了廣泛的應用,如捕魚達人游戲和91部落游戲。Cocos2d?x允許開發(fā)者使用C++,Lua,JS等語言并編譯到iOS,Android,Windows Phone,Mac OS X,微軟桌面系統(tǒng)和Linux系統(tǒng)中,同時可以節(jié)省時間、減少精力和降低成本[3?4]。
本文基于Visual Studio開發(fā)環(huán)境,采用LUA腳本語言,結合Cocos2d?x游戲引擎,設計一款適用于Android系統(tǒng)的手機應用APP“貪吃蛇”。
由于Android操作系統(tǒng)內(nèi)核是基于Linux系統(tǒng)的,且主要使用Java作為開發(fā)語言,而本文在Windows平臺下開發(fā)Android程序,所以需要下載安裝Windows平臺下所需要的開發(fā)工具[5?6] ,如表1所示。
下載安裝完Python以后需要配置環(huán)境變量 (下載地址:https://www.python.org)。點擊我的電腦,選擇屬性,點擊環(huán)境變量,選擇Path路徑,把剛剛安裝Python時選擇的路徑放在Path路徑里面,待配置完環(huán)境變量后運行客戶端,在cmd命令行下鍵入Python,如果得到Python的版本信息表示安裝完成,如圖1所示。
2.1 ?游戲功能描述
參考經(jīng)典貪吃蛇游戲規(guī)則并改進此游戲,程序主要實現(xiàn)的功能如下:
1) 模式選擇。點擊進入游戲,游戲啟動后會出現(xiàn)“吃”“不吃”“吃不吃”三種模式進行選擇,選擇任意模式進入游戲。
2) 角色設計。游戲開始后蛇從左向右移動,隨機生成食物和障礙物,蛇需要躲避障礙物和吃食物來增長身體長度獲得分數(shù),通過滑動屏幕改變蛇運動方向,如果蛇在移動中撞到墻、障礙物或者蛇身體,蛇死亡游戲結束。按游戲界面左上角“返回”鍵或者按“home鍵”即可退出游戲。
3) 音效設計。為了增加玩家的體驗感,需要根據(jù)游戲的具體場景來增加適當?shù)囊粜Вū尘耙粜?、游戲過關音效以及游戲失敗的音效。
2.2 ?游戲設計框架
游戲的設計思想:用數(shù)組變量[7]描述蛇的狀態(tài)。首先進行初始化,包括:蛇頭、蛇身體、運動方向、蛇身長度和節(jié)點數(shù)。第二,游戲者選擇關卡進入游戲,蛇開始移動,在每次移動前判斷是否滑動屏幕,有則修改蛇的運動狀態(tài)。第三,判斷下一步蛇是否吃到食物,若吃到食物則移動一步,清除尾點,蛇身體長度加1,反復直至蛇碰到障礙物死亡,游戲結束,狀態(tài)如2圖所示。
3.1 ?類模塊設計
程序實現(xiàn)主要用到的類模塊有MainScene.lua主界面類。GameScene.lua游戲背景和游戲場景繪制類。GameLayer.lua布景層類,包括數(shù)據(jù)初始化、數(shù)據(jù)更新、生成食物和障礙物、游戲刷新、事件回調(diào)。利用SnakeBody.lua定義蛇類,包括蛇頭、蛇身體、方向、拐彎點、初始化。SnakeManager.lua生成食物和障礙物,確定產(chǎn)生范圍,控制蛇身體增長,改變蛇移動方向等。
3.2 ?主要代碼
1) 圍墻
圍墻是由不可見的50行25列方格和可見的圖片組成,該部分均在function GameScene:showMap()中實現(xiàn)定義起始行列、結束行列、空隙local startX,startY, endX,endY,space。
space = BodyRadius*2 ? ? ? ? ? ? ? ? //空隙是蛇身半徑的2倍
循環(huán)劃線
for i=1,xCount+1 do
startX=(display.cx?space*xCount*0.5)+ space*(i?1)
startY = display.cy?space*yCount*0.5
endX = startX
endY = display.cy+space*yCount*0.5
self.drawNode_:drawLine(cc.p(startX,startY), cc.p(endX, endY), cc.c4f(1, 0, 0, 1))
根據(jù)劃線范圍界定游戲中蛇的活動范圍、食物和障礙物出現(xiàn)的范圍。
cc.exports.GameMinX= display.cx?space*xCount*0.5
cc.exports.GameMaxX=display.cx+space*xCount*0.5
cc.exports.GameMinY= display.cy?space*yCount*0.5
cc.exports.GameMaxY=display.cy+space*yCount*0.5
2) 蛇的創(chuàng)建
定義蛇類,按照傳入的bodyType創(chuàng)建蛇,包括身體類型、方向、拐彎點、初始化。在function SnakeBody:init()中實現(xiàn)初始化,直接把最初要呈現(xiàn)給玩家的蛇的情況定義出來,使得在最初開始游戲時,有蛇可以進行自由移動。如果蛇身節(jié)數(shù)為空,則創(chuàng)建蛇身并添加到父級SnakeBody中。在function SnakeBody:drawSnake()中創(chuàng)建蛇身體、食物和障礙物。蛇身體、食物和障礙物由圓圈組成,當蛇身類型為0或1時分別用不同的顏色畫出。蛇身類型為0時其性質為食物,用綠色顯示;蛇身類型為1時其性質為障礙物,用藍色顯示。
function SnakeBody:init()
if self.drawNode_ == nil then
self.drawNode_= cc.DrawNode:create(1)
self:addChild(self.drawNode_)
end
local color
if self.bodyType_ == 0 then
color = cc.c4f(0, 1, 0, 1)
else
color = cc.c4f(0, 0, 1, 1)
end
self.drawNode_:drawCircle(cc.p(0, 0), BodyRadius, 0, 8, false, color)
end
3) 碰撞檢測
通常針對2D游戲常用到的檢測技術有矩形檢測、圓形檢測和像素檢測。矩形檢測方法是,每個物體記錄一個能將自己框住的最小矩形的左上角坐標和矩形長寬,碰撞時判斷矩形與矩形是否重疊,而這僅需4次比較即可得出,速度較快,所以本文選用矩形檢測法進行碰撞檢測[8]。
首先確定碰撞檢測對象,本游戲中蛇頭、食物和障礙物都需要碰撞檢測。如果蛇頭和食物發(fā)生碰撞蛇長度加1,如果蛇頭和障礙物發(fā)生碰撞,蛇死亡游戲結束。下面是該檢測方法的算法:取兩個矩形實體的左上角坐標(x1,y1)和(x2,y2)以及二者的寬度w1,w2和高度h1,h2;判斷是否x1<x2+w2,且x2<x1+w1,且y1<y2+h2,且y2<y1+h1。如果滿足這4個條件,則說明兩個矩形實體發(fā)生了碰撞[9]。
function SnakeBody:getBox()用于獲取snakebody所在的矩形區(qū)域,判斷碰撞;myRect = cc.rect(x,y,w,h),x,y是矩形左上角坐標,w,h是矩形的寬和高。checkContainsRectPoints函數(shù)判斷蛇頭矩形和食物矩形是否重疊。
//判斷snake是否撞到食物或者障礙物
count = #self.foodArr_ ? ? ? ? ? ? ? ? ? //將food總數(shù)賦給count
local oneFood
for i=1,count do
oneFood= self.foodArr_[count+1?i] //Index是食物在foodArr_中的序數(shù),將self.foodArr_中存儲的所有food依次賦給oneFood。
//如果蛇頭撞到食物,food的類型是1,即撞到障礙物或者圍墻,蛇死亡游戲結束
if self.snakeArr_[1]:checkContainsRectPoints(oneFood:
getBox()) then
if oneFood.bodyType_ == 0 then
self:changeSnake(count+1?i)
else if oneFood.bodyType_ == 1 then
self.gameLayer_:gameOver()
4) 改變蛇運動方向
根據(jù)觸摸方法中獲得兩個點的位置來判斷手指滑動的方向,在方法function SnakeBody:setDirection(pos)中實現(xiàn),若新方向和原方向相同,則不做處理;若新方向和原方向不同,設置轉彎的坐標,使snake中其他的snakebody隨后也在這個坐標位置轉彎。
//設置轉彎的坐標
function SnakeBody:setTurnPos(pos)
self.turnPos_ = pos
//獲得轉彎的坐標
function SnakeBody:getTurnPos()
return self.turnPos_
//獲得移動方向
function SnakeBody:getDirection()
return cc.p(self.direction_.x, self.direction_.y)
//設置移動方向
function SnakeBody:setDirection(pos)
//若和當前方向相同
if pos.x == self.direction_.x and pos.y == self.direction_.y
then
return false
//判斷新的方向是否合法,x,y是-1~1之間的數(shù)字,新的x,y必須和舊的x,y不同
local bValid = false
if pos.x ~= 0 or pos.y ~= 0 then
if self.direction_.x==0 and self.direction_.y == 0 then
bValid = true
else if self.direction_.x ~= 0 and pos.y ~= 0 then
pos.x = 0 bValid = true
else if self.direction_.y ~= 0 and pos.x ~= 0 then
pos.y = 0 bValid = true
end
if ? bValid == true then
self.direction_ = cc.p(pos.x, pos.y)
local posX, posY = self:getPosition()
end
return bValid
end
測試程序時,首先將游戲的APK文件下載到Android手機上并進行安裝,安裝完成后手機上顯示游戲圖標,點擊貪吃蛇圖標運行結果如圖3所示,進入游戲開始界面,選擇關卡進入游戲。當蛇撞到圍墻或者障礙物時,蛇死亡游戲結束,如圖4所示。
本文采用Cocos2d?x引擎系統(tǒng)創(chuàng)建貪吃蛇游戲,利用Cocos2d?x進行圖形渲染,構建框架游戲、應用程序和圖形界面的交互。系統(tǒng)在Visual Studio 2017平臺下,采用LUA腳本語言進行編程。經(jīng)測試,系統(tǒng)可以實現(xiàn)貪吃蛇的基本功能。本文通過對游戲開發(fā)環(huán)境、實現(xiàn)功能、框架設計進行介紹,并對程序中重要代碼進行分析,掌握了利用Cocos2d?x游戲引擎編程的思想,可以將Cocos2d?x易移植和模塊化思想擴展到其他應用[10],滿足不同的顯示要求,極大地縮短了游戲開發(fā)周期,對移動端游戲開發(fā)有重要意義。
注:本文通訊作者為王魏。
參考文獻
[1] 陳羽修.基于Android貪吃蛇游戲的設計與實現(xiàn)[J].科技視界,2016(1):220?221.
CHEN Yuxiu. Design and implementation of Gluttonous Snake game based on Android [J]. Science & technology vision, 2016(1): 220?221.
[2] 舒沂.基于Cocos2d?x引擎的手機游戲快速開發(fā)工具的設計與實現(xiàn)[D].南京:南京大學,2013.
SHU Yi. Design and implementation of fast development tools based on Cocos2d?x engine for mobile phone games [D]. Nanjing: Nanjing University, 2013.
[3] 李勇.基于Cocos2d?x引擎的游戲架構設計與實現(xiàn)[D].北京:北京郵電大學,2015.
LI Yong. Design and implementation of game framework based on Cocos2d?x engine [D]. Beijing: Beijing University of Posts and Telecommunications, 2015.
[4] 霍常偉.基于Cocos2d?x引擎的移動游戲UI系統(tǒng)設計及應用[D].北京:北京交通大學,2012.
HUO Changwei. Design and application of UI system based on Cocos2d?x engine for mobile game [D]. Beijing: Beijing Jiaotong University, 2012.
[5] 韓文智,駱文亮.Android平臺的移動APP開發(fā)方法與應用研究[J].四川理工學院學報(自然科學版),2015,28(3):22?26.
HAN Wenzhi, LUO Wenliang. Development method and application research of mobile APP based on Android platform [J]. Journal of Sichuan University of Science & Engineering, 2015, 28(3): 22?26.
[6] 佚名.Android開發(fā):開發(fā)環(huán)境搭建之工具下載[EB/OL].[2014?02?13]. https://jingyan.baidu.com/article/9989c746006248f648ecf
eb7.html.
Anon. Android development: downloading of tools for development environment establishment [EB/OL]. [2014?02?13]. https://jingyan.baidu.com/article/9989c746006248f648ecfeb7.html.
[7] 賀敬凱.基于FPGA平臺下的貪吃蛇游戲開發(fā)研究[J].深圳信息職業(yè)技術學院學報,2015,13(3):17?21.
HE Jingkai. Development of snake game based on FPGA platform [J]. Journal of Shenzhen Institute of Information Technology, 2015, 13(3): 17?21.
[8] 崔浩然.基于Android平臺的手機游戲的設計與實現(xiàn)[D].西安:西安科技大學,2011.
CUI Haoran. Design and implementation of mobile phone game based on Android platform [D]. Xian: Xian University of Science and Technology, 2011.
[9] 楊傳億.基于移動設備的教育游戲的設計與開發(fā)[D].上海:華東師范大學,2010.
YANG Chuanyi. Design and development of education games based on mobile devices [D]. Shanghai: East China Normal University, 2010.
[10] 楊偉,肖義平.基于STM32F103C8T6單片機的LCD顯示系統(tǒng)設計[J].微型機與應用,2014,33(20):29?31.
YANG Wei, XIAO Yiping. LCD display system design based on MCU STM32F103C8T6 [J]. Microcomputer & its applications, 2014, 33(20): 29?31.
[11] 周飛龍.Cocos2d?x引擎中MVC框架的設計與實現(xiàn)[D].武漢:華中科技大學,2013.
ZHOU Feilong. Design and implementation of MVC framework on Cocos2d?x engine [D]. Wuhan: Huazhong University of Science and Technology, 2013.