吳曉雪 何南 繆新穎 王魏
關(guān)鍵詞: 貪吃蛇; Cocos2d?x; 手機(jī)游戲; LUA; 邏輯功能設(shè)計(jì); 碰撞檢測
中圖分類號: TN912.3?34; TP311.5 ? ? ? ? ? ? ? 文獻(xiàn)標(biāo)識碼: 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平臺的開源手機(jī)操作系統(tǒng),包括操作系統(tǒng)、用戶界面和應(yīng)用程序[1]。手機(jī)游戲作為Android系統(tǒng)平臺的一個(gè)重要組成部分,正在被越來越多的人重視,如何在手機(jī)平臺上開發(fā)一款高效的系統(tǒng)框架成為主要考慮的因素[2]。
Cocos2d?x正是為提高游戲開發(fā)效率而誕生的一款優(yōu)秀的游戲引擎,并且已經(jīng)得到了廣泛的應(yīng)用,如捕魚達(dá)人游戲和91部落游戲。Cocos2d?x允許開發(fā)者使用C++,Lua,JS等語言并編譯到iOS,Android,Windows Phone,Mac OS X,微軟桌面系統(tǒng)和Linux系統(tǒng)中,同時(shí)可以節(jié)省時(shí)間、減少精力和降低成本[3?4]。
本文基于Visual Studio開發(fā)環(huán)境,采用LUA腳本語言,結(jié)合Cocos2d?x游戲引擎,設(shè)計(jì)一款適用于Android系統(tǒng)的手機(jī)應(yīng)用APP“貪吃蛇”。
由于Android操作系統(tǒng)內(nèi)核是基于Linux系統(tǒng)的,且主要使用Java作為開發(fā)語言,而本文在Windows平臺下開發(fā)Android程序,所以需要下載安裝Windows平臺下所需要的開發(fā)工具[5?6] ,如表1所示。
下載安裝完P(guān)ython以后需要配置環(huán)境變量 (下載地址:https://www.python.org)。點(diǎn)擊我的電腦,選擇屬性,點(diǎn)擊環(huán)境變量,選擇Path路徑,把剛剛安裝Python時(shí)選擇的路徑放在Path路徑里面,待配置完環(huán)境變量后運(yùn)行客戶端,在cmd命令行下鍵入Python,如果得到Python的版本信息表示安裝完成,如圖1所示。
2.1 ?游戲功能描述
參考經(jīng)典貪吃蛇游戲規(guī)則并改進(jìn)此游戲,程序主要實(shí)現(xiàn)的功能如下:
1) 模式選擇。點(diǎn)擊進(jìn)入游戲,游戲啟動后會出現(xiàn)“吃”“不吃”“吃不吃”三種模式進(jìn)行選擇,選擇任意模式進(jìn)入游戲。
2) 角色設(shè)計(jì)。游戲開始后蛇從左向右移動,隨機(jī)生成食物和障礙物,蛇需要躲避障礙物和吃食物來增長身體長度獲得分?jǐn)?shù),通過滑動屏幕改變蛇運(yùn)動方向,如果蛇在移動中撞到墻、障礙物或者蛇身體,蛇死亡游戲結(jié)束。按游戲界面左上角“返回”鍵或者按“home鍵”即可退出游戲。
3) 音效設(shè)計(jì)。為了增加玩家的體驗(yàn)感,需要根據(jù)游戲的具體場景來增加適當(dāng)?shù)囊粜?,包括背景音效、游戲過關(guān)音效以及游戲失敗的音效。
2.2 ?游戲設(shè)計(jì)框架
游戲的設(shè)計(jì)思想:用數(shù)組變量[7]描述蛇的狀態(tài)。首先進(jìn)行初始化,包括:蛇頭、蛇身體、運(yùn)動方向、蛇身長度和節(jié)點(diǎn)數(shù)。第二,游戲者選擇關(guān)卡進(jìn)入游戲,蛇開始移動,在每次移動前判斷是否滑動屏幕,有則修改蛇的運(yùn)動狀態(tài)。第三,判斷下一步蛇是否吃到食物,若吃到食物則移動一步,清除尾點(diǎn),蛇身體長度加1,反復(fù)直至蛇碰到障礙物死亡,游戲結(jié)束,狀態(tài)如2圖所示。
3.1 ?類模塊設(shè)計(jì)
程序?qū)崿F(xiàn)主要用到的類模塊有MainScene.lua主界面類。GameScene.lua游戲背景和游戲場景繪制類。GameLayer.lua布景層類,包括數(shù)據(jù)初始化、數(shù)據(jù)更新、生成食物和障礙物、游戲刷新、事件回調(diào)。利用SnakeBody.lua定義蛇類,包括蛇頭、蛇身體、方向、拐彎點(diǎn)、初始化。SnakeManager.lua生成食物和障礙物,確定產(chǎn)生范圍,控制蛇身體增長,改變蛇移動方向等。
3.2 ?主要代碼
1) 圍墻
圍墻是由不可見的50行25列方格和可見的圖片組成,該部分均在function GameScene:showMap()中實(shí)現(xiàn)定義起始行列、結(jié)束行列、空隙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)建蛇,包括身體類型、方向、拐彎點(diǎn)、初始化。在function SnakeBody:init()中實(shí)現(xiàn)初始化,直接把最初要呈現(xiàn)給玩家的蛇的情況定義出來,使得在最初開始游戲時(shí),有蛇可以進(jìn)行自由移動。如果蛇身節(jié)數(shù)為空,則創(chuàng)建蛇身并添加到父級SnakeBody中。在function SnakeBody:drawSnake()中創(chuàng)建蛇身體、食物和障礙物。蛇身體、食物和障礙物由圓圈組成,當(dāng)蛇身類型為0或1時(shí)分別用不同的顏色畫出。蛇身類型為0時(shí)其性質(zhì)為食物,用綠色顯示;蛇身類型為1時(shí)其性質(zhì)為障礙物,用藍(lán)色顯示。
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游戲常用到的檢測技術(shù)有矩形檢測、圓形檢測和像素檢測。矩形檢測方法是,每個(gè)物體記錄一個(gè)能將自己框住的最小矩形的左上角坐標(biāo)和矩形長寬,碰撞時(shí)判斷矩形與矩形是否重疊,而這僅需4次比較即可得出,速度較快,所以本文選用矩形檢測法進(jìn)行碰撞檢測[8]。
首先確定碰撞檢測對象,本游戲中蛇頭、食物和障礙物都需要碰撞檢測。如果蛇頭和食物發(fā)生碰撞蛇長度加1,如果蛇頭和障礙物發(fā)生碰撞,蛇死亡游戲結(jié)束。下面是該檢測方法的算法:取兩個(gè)矩形實(shí)體的左上角坐標(biāo)(x1,y1)和(x2,y2)以及二者的寬度w1,w2和高度h1,h2;判斷是否x1<x2+w2,且x2<x1+w1,且y1<y2+h2,且y2<y1+h1。如果滿足這4個(gè)條件,則說明兩個(gè)矩形實(shí)體發(fā)生了碰撞[9]。
function SnakeBody:getBox()用于獲取snakebody所在的矩形區(qū)域,判斷碰撞;myRect = cc.rect(x,y,w,h),x,y是矩形左上角坐標(biāo),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,即撞到障礙物或者圍墻,蛇死亡游戲結(jié)束
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) 改變蛇運(yùn)動方向
根據(jù)觸摸方法中獲得兩個(gè)點(diǎn)的位置來判斷手指滑動的方向,在方法function SnakeBody:setDirection(pos)中實(shí)現(xiàn),若新方向和原方向相同,則不做處理;若新方向和原方向不同,設(shè)置轉(zhuǎn)彎的坐標(biāo),使snake中其他的snakebody隨后也在這個(gè)坐標(biāo)位置轉(zhuǎn)彎。
//設(shè)置轉(zhuǎn)彎的坐標(biāo)
function SnakeBody:setTurnPos(pos)
self.turnPos_ = pos
//獲得轉(zhuǎn)彎的坐標(biāo)
function SnakeBody:getTurnPos()
return self.turnPos_
//獲得移動方向
function SnakeBody:getDirection()
return cc.p(self.direction_.x, self.direction_.y)
//設(shè)置移動方向
function SnakeBody:setDirection(pos)
//若和當(dāng)前方向相同
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
測試程序時(shí),首先將游戲的APK文件下載到Android手機(jī)上并進(jìn)行安裝,安裝完成后手機(jī)上顯示游戲圖標(biāo),點(diǎn)擊貪吃蛇圖標(biāo)運(yùn)行結(jié)果如圖3所示,進(jìn)入游戲開始界面,選擇關(guān)卡進(jìn)入游戲。當(dāng)蛇撞到圍墻或者障礙物時(shí),蛇死亡游戲結(jié)束,如圖4所示。
本文采用Cocos2d?x引擎系統(tǒng)創(chuàng)建貪吃蛇游戲,利用Cocos2d?x進(jìn)行圖形渲染,構(gòu)建框架游戲、應(yīng)用程序和圖形界面的交互。系統(tǒng)在Visual Studio 2017平臺下,采用LUA腳本語言進(jìn)行編程。經(jīng)測試,系統(tǒng)可以實(shí)現(xiàn)貪吃蛇的基本功能。本文通過對游戲開發(fā)環(huán)境、實(shí)現(xiàn)功能、框架設(shè)計(jì)進(jìn)行介紹,并對程序中重要代碼進(jìn)行分析,掌握了利用Cocos2d?x游戲引擎編程的思想,可以將Cocos2d?x易移植和模塊化思想擴(kuò)展到其他應(yīng)用[10],滿足不同的顯示要求,極大地縮短了游戲開發(fā)周期,對移動端游戲開發(fā)有重要意義。
注:本文通訊作者為王魏。
參考文獻(xiàn)
[1] 陳羽修.基于Android貪吃蛇游戲的設(shè)計(jì)與實(shí)現(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引擎的手機(jī)游戲快速開發(fā)工具的設(shè)計(jì)與實(shí)現(xiàn)[D].南京:南京大學(xué),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引擎的游戲架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)[D].北京:北京郵電大學(xué),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)設(shè)計(jì)及應(yīng)用[D].北京:北京交通大學(xué),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ā)方法與應(yīng)用研究[J].四川理工學(xué)院學(xué)報(bào)(自然科學(xué)版),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è)技術(shù)學(xué)院學(xué)報(bào),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平臺的手機(jī)游戲的設(shè)計(jì)與實(shí)現(xiàn)[D].西安:西安科技大學(xué),2011.
CUI Haoran. Design and implementation of mobile phone game based on Android platform [D]. Xian: Xian University of Science and Technology, 2011.
[9] 楊傳億.基于移動設(shè)備的教育游戲的設(shè)計(jì)與開發(fā)[D].上海:華東師范大學(xué),2010.
YANG Chuanyi. Design and development of education games based on mobile devices [D]. Shanghai: East China Normal University, 2010.
[10] 楊偉,肖義平.基于STM32F103C8T6單片機(jī)的LCD顯示系統(tǒng)設(shè)計(jì)[J].微型機(jī)與應(yīng)用,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框架的設(shè)計(jì)與實(shí)現(xiàn)[D].武漢:華中科技大學(xué),2013.
ZHOU Feilong. Design and implementation of MVC framework on Cocos2d?x engine [D]. Wuhan: Huazhong University of Science and Technology, 2013.