基于IOS平台塔防游戏的设计与实现

2014-09-24 12:14王海南邵国强介龙梅
软件工程 2014年6期

王海南+邵国强+介龙梅

摘 要:目前随着我国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