王海南+邵國強+介龍梅
摘 要:目前隨著我國3G網絡的快速發展,智能手機的市場也日益擴大,其中最受歡迎的莫過于蘋果公司生產的iPhone手機。本文介紹基于IOS平臺的擁有良好操作性及娛樂性的小型塔防游戲開發過程。該游戲是運用Cocos2d引擎以及Xcode進行開發的,不僅實現了塔防游戲的基本對戰功能,還增添了場景選擇,音效設置等功能,游戲界面唯美,動畫效果精彩,為手機游戲開發提供了參考。
關鍵詞:IOS平臺;塔防游戲;Cocos2d引擎
中圖分類號:TP393.02 文獻標識碼:A
The Design and Implementation of Tower and Defense Game Based IOS
WANG Hainan1,SHAO Guoqiang2,JIE Longmei2
(1.Fine Arts Academy,Northeast Normal University,Changchun 130024,China;
2.Computer Science & Information Technology Collage,Daqing Normal University,Daqing 163712,China)
Abstract:At present,with the rapid development of 3G network in China,intelligent mobile phone market is growing gradually.One of the most popular is iPhone produced by Apple Corporation.In the paper,it introduce you the procedure that how a good operability and entertaining small tower defense game based on IOS platform is developed.The game program is developed by Cocos2d engine and the Xcode tools. It not only impliments the basic attack-defence functions of tower defense game, but also provides scene selection,sound settings,etc.The game is of beautiful interface and wonderful animation effect.It can be inferenced by other mobile game developer.
Keywords:the IOS platform;tower defense game;the cocos2d engine
1 引言(Introduction)
隨著3G的網絡技術的普及,手機游戲成為軟件開發的一個重要方向。蘋果公司生產的iPhone手機,憑借其獨特的外表、強大的硬件環境以及App Store上提供的高效的軟件受到了人們的好評[1]。
塔防游戲是一款經典的益智類游戲。塔防游戲的模式為堵怪模式,一張作戰地圖,讓怪獸按照你設定的路線來回移動,讓炮塔按照你設定的位置進行創建,并且可以通過使用特效技能,以便于炮塔不斷的轟擊怪獸,從而獲得游戲的勝利[2]。
2 系統分析(System analysis)
玩家和游戲系統形成的業務流程如圖1所示。
圖1 游戲業務流程圖
Fig.1 The business flow chart of the game
3 游戲總體設計(The overall design of the game)
3.1 游戲主流程設計
游戲按照以下流程進行設計,如圖2所示。
圖2 游戲主流程圖
Fig.2 The main flow chart of the game
3.2 怪物流程圖設計
根據需求分析,游戲會根據不同的場景不同的地圖,設置怪物的行走路線。倘若怪物在規定的路線上移動時,進入到炮塔的攻擊范圍內,怪物被攻擊后血條會逐漸減少,當血量減到0時,怪物移除游戲場景。倘若怪物攻入城堡,玩家的塔則會相應地減少生命值。
3.3 炮塔流程圖設計
玩家在金幣足夠的情況下,點擊拖動不同價格的炮塔,然后在地圖上選取一個地方釋放鼠標,假如這個地方可以放置炮塔,則炮塔會出現在這一位置。反之,該地會提示不允許放置炮塔。建造好的炮塔會攻擊進入其設定的攻擊范圍內的怪物。
4 系統實現(System implementation)
系統實現是基于Cocos2d引擎的,該引擎具有功強大能、快速入門、效果上佳的特點,是IOS游戲開發者的首選[3,4]。
4.1 打怪的實現
游戲中最難實現的為打怪功能。要設定一個計時器,隨時判斷塔和怪物之間的距離,以確定塔向著什么方向進行攻擊。在此游戲中,是以坐標系的方法來判斷塔的攻擊方向的[5]。核心實現代碼如下:
float dx = enemy.position.x - self.position.x;
float dy = enemy.position.y - self.position.y;
NSString *s = [[[NSString alloc] init] autorelease];endprint
if (dx < 0 && dy < 0) {
if (-dx > -dy) {
s = @"l";
}else if (-dx < -dy){
s = @"d";
}
}else if (dx < 0 && dy > 0) {
if (-dx > dy) {
s = @"l";
}else if (-dx < dy) {
s = @"u";
}
}else if (dx > 0 && dy > 0) {
if (dx > dy) {
s = @"r";
}else if (dx < dy) {
s = @"u";
}
}else if (dx > 0 && dy < 0) {
if (dx > -dy) {
s = @"r";
}else if (dx < -dy) {
s = @"d";
}
}
NSMutableArray *monsterArray = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < 8; i++) {
NSString *monsterName = [NSString stringWithFormat:@"%@%@%d.png",name,s,i];
CCSpriteFrame *oneMonster = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:monsterName];
[monsterArray addObject:oneMonster];
4.2 粒子特效頁面
進入游戲前,會顯示粒子特效界面。實現粒子特效的核心代碼:
CCParticleSystem *lizi = [[ARCH_OPTIMAL_PARTICLE_SYSTEM alloc] initWithFile:@"lingxiaohuo.plist"];
lizi.autoRemoveOnFinish=NO;
lizi.positionType = kCCPositionTypeRelative;
[self addChild:lizi];
[lizi release];
通過粒子編輯器,選取合適的粒子特效,生成plist文件,輸入這段代碼即可實現。
5 結論(Conclusion)
經過詳細測試,系統穩定、流暢,用戶體驗效果好,游戲界面美觀。基于IOS的塔防游戲是一款經典的游戲,用到了包括粒子、菜單、技能條、塔、怪物等核心對象的實現,系統流程較復雜,對未來的更加龐大的手機軟件開發有所幫助。
參考文獻(References)
[1] 王章靜.IOS環境下塔防游戲實現[J].價值工程,2012,(32):194-
197.
[2] 弋榮靜,王振凱.基于iOS平臺的雜志閱讀軟件的設計與實現
[J].軟件,2012,(32):31-37.
[3] 安毅生,劉衛方.基于Cocos2D框架的交互式迷宮游戲設計
與實現[J].網絡安全技術與應用,2011(12):154-158.
[4] 邢芳,張小欽.基于Cocos2d-x的三消類游戲的設計[J].科技廣
場,2013(5):113-115.
[5] 劉婷婷.游戲中怪物人工智能的實現[J].數字技術與應用,
2012(4):200-202.
作者簡介:
王海泉(1967-),男,碩士,工程師.研究領域:計算機硬件和
數據庫技術.
邵國強(1981-),男,碩士,講師.研究領域:3G和軟件系統
研發.
介龍梅(1975-),女,碩士,講師.研究領域:計算機網絡與通
信,人工智能.endprint
if (dx < 0 && dy < 0) {
if (-dx > -dy) {
s = @"l";
}else if (-dx < -dy){
s = @"d";
}
}else if (dx < 0 && dy > 0) {
if (-dx > dy) {
s = @"l";
}else if (-dx < dy) {
s = @"u";
}
}else if (dx > 0 && dy > 0) {
if (dx > dy) {
s = @"r";
}else if (dx < dy) {
s = @"u";
}
}else if (dx > 0 && dy < 0) {
if (dx > -dy) {
s = @"r";
}else if (dx < -dy) {
s = @"d";
}
}
NSMutableArray *monsterArray = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < 8; i++) {
NSString *monsterName = [NSString stringWithFormat:@"%@%@%d.png",name,s,i];
CCSpriteFrame *oneMonster = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:monsterName];
[monsterArray addObject:oneMonster];
4.2 粒子特效頁面
進入游戲前,會顯示粒子特效界面。實現粒子特效的核心代碼:
CCParticleSystem *lizi = [[ARCH_OPTIMAL_PARTICLE_SYSTEM alloc] initWithFile:@"lingxiaohuo.plist"];
lizi.autoRemoveOnFinish=NO;
lizi.positionType = kCCPositionTypeRelative;
[self addChild:lizi];
[lizi release];
通過粒子編輯器,選取合適的粒子特效,生成plist文件,輸入這段代碼即可實現。
5 結論(Conclusion)
經過詳細測試,系統穩定、流暢,用戶體驗效果好,游戲界面美觀。基于IOS的塔防游戲是一款經典的游戲,用到了包括粒子、菜單、技能條、塔、怪物等核心對象的實現,系統流程較復雜,對未來的更加龐大的手機軟件開發有所幫助。
參考文獻(References)
[1] 王章靜.IOS環境下塔防游戲實現[J].價值工程,2012,(32):194-
197.
[2] 弋榮靜,王振凱.基于iOS平臺的雜志閱讀軟件的設計與實現
[J].軟件,2012,(32):31-37.
[3] 安毅生,劉衛方.基于Cocos2D框架的交互式迷宮游戲設計
與實現[J].網絡安全技術與應用,2011(12):154-158.
[4] 邢芳,張小欽.基于Cocos2d-x的三消類游戲的設計[J].科技廣
場,2013(5):113-115.
[5] 劉婷婷.游戲中怪物人工智能的實現[J].數字技術與應用,
2012(4):200-202.
作者簡介:
王海泉(1967-),男,碩士,工程師.研究領域:計算機硬件和
數據庫技術.
邵國強(1981-),男,碩士,講師.研究領域:3G和軟件系統
研發.
介龍梅(1975-),女,碩士,講師.研究領域:計算機網絡與通
信,人工智能.endprint
if (dx < 0 && dy < 0) {
if (-dx > -dy) {
s = @"l";
}else if (-dx < -dy){
s = @"d";
}
}else if (dx < 0 && dy > 0) {
if (-dx > dy) {
s = @"l";
}else if (-dx < dy) {
s = @"u";
}
}else if (dx > 0 && dy > 0) {
if (dx > dy) {
s = @"r";
}else if (dx < dy) {
s = @"u";
}
}else if (dx > 0 && dy < 0) {
if (dx > -dy) {
s = @"r";
}else if (dx < -dy) {
s = @"d";
}
}
NSMutableArray *monsterArray = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < 8; i++) {
NSString *monsterName = [NSString stringWithFormat:@"%@%@%d.png",name,s,i];
CCSpriteFrame *oneMonster = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:monsterName];
[monsterArray addObject:oneMonster];
4.2 粒子特效頁面
進入游戲前,會顯示粒子特效界面。實現粒子特效的核心代碼:
CCParticleSystem *lizi = [[ARCH_OPTIMAL_PARTICLE_SYSTEM alloc] initWithFile:@"lingxiaohuo.plist"];
lizi.autoRemoveOnFinish=NO;
lizi.positionType = kCCPositionTypeRelative;
[self addChild:lizi];
[lizi release];
通過粒子編輯器,選取合適的粒子特效,生成plist文件,輸入這段代碼即可實現。
5 結論(Conclusion)
經過詳細測試,系統穩定、流暢,用戶體驗效果好,游戲界面美觀。基于IOS的塔防游戲是一款經典的游戲,用到了包括粒子、菜單、技能條、塔、怪物等核心對象的實現,系統流程較復雜,對未來的更加龐大的手機軟件開發有所幫助。
參考文獻(References)
[1] 王章靜.IOS環境下塔防游戲實現[J].價值工程,2012,(32):194-
197.
[2] 弋榮靜,王振凱.基于iOS平臺的雜志閱讀軟件的設計與實現
[J].軟件,2012,(32):31-37.
[3] 安毅生,劉衛方.基于Cocos2D框架的交互式迷宮游戲設計
與實現[J].網絡安全技術與應用,2011(12):154-158.
[4] 邢芳,張小欽.基于Cocos2d-x的三消類游戲的設計[J].科技廣
場,2013(5):113-115.
[5] 劉婷婷.游戲中怪物人工智能的實現[J].數字技術與應用,
2012(4):200-202.
作者簡介:
王海泉(1967-),男,碩士,工程師.研究領域:計算機硬件和
數據庫技術.
邵國強(1981-),男,碩士,講師.研究領域:3G和軟件系統
研發.
介龍梅(1975-),女,碩士,講師.研究領域:計算機網絡與通
信,人工智能.endprint