源代码:https://github.com/manguoge/game_Ranking
一、游戏设计简介

“打仙人掌”游戏是个简单的“打狗熊”类游戏。在手机画面的沙丘里,不断有仙人掌跳出来,玩家必须尽量快得去拍打跳出来的仙人掌,每拍打一个仙人掌得分加1,如果在限制的时间内没有把跳出来的仙人掌拍打回去,玩家的生命数将减1,生命数为0时游戏结束。随着玩家的分数的增加,游戏的难度会不断的增加,即跳出来的仙人掌的频率越来越快。

二、游戏规则设计

1.每拍打一个仙人掌分数加1;

2.每次游戏的玩家生命数是5,每没有拍打一个仙人掌生命数减1,至0游戏结束;

3.仙人掌的位置随机生成,其中水平位置可在屏幕任意位置,竖直位置为三种随机位置;

4.当分数到达30分时,玩家没拍打5株仙人掌,同时出现的仙人掌数加1;

5.游戏支持暂停、接着玩及退出功能。

三、游戏开发逻辑

1.默认采用横屏,游戏由两个控制器页面组成,采用导航控制器层级结构,点击首页play按钮进入游戏页面;

2.游戏状态分为正在游戏,暂停游戏和结束游戏三种,初始化游戏的相关参数;

- (void)viewDidLoad
{//初始化基本参数gameOver=NO;pause=NO;life=5;score=0;[super viewDidLoad];[self.pauseBtn setImage:[UIImage imageNamed:@"PausePressed"] forState:UIControlStateHighlighted];UILabel* tipLabel=[[UILabel alloc] initWithFrame:CGRectMake((SCREENWIDTH-300)*0.5, (SCREENHEIGHT-100)*0.5, 300, 100)];//进入游戏,提示游戏将开始tipLabel.text=@"Game Will Start After 3s!";[tipLabel setFont:[UIFont systemFontOfSize:20]];[tipLabel setTextAlignment:NSTextAlignmentCenter];tipLabel.layer.borderWidth=5;tipLabel.layer.cornerRadius=15;tipLabel.layer.masksToBounds=YES;tipLabel.alpha=1;tipLabel.backgroundColor=[UIColor redColor];[self.view addSubview:tipLabel];[UIView animateWithDuration:3 animations:^{tipLabel.alpha=0;tipLabel.backgroundColor=[UIColor grayColor];} completion:^(BOOL finished){[tipLabel removeFromSuperview];//初始化两个仙人掌[self spawnCactus];[self performSelector:@selector(spawnCactus) withObject:nil afterDelay:2.0];}];
}

2.随机生成仙人掌,x轴位置随机,Y轴位置有三种情况,仙人掌的大小有三种随机大小;

-(void)spawnCactus
{if (gameOver){return;}if (pause){//暂停状态时,每隔1秒检查一次状态,若变为非暂停状态时,继续生成一个仙人掌[self performSelector:@selector(spawnCactus) withObject:nil afterDelay:1];return;}//根据随机生成数字0、1或者2确定生成的不同大小的仙人掌图片int cactusSize=arc4random()%3;UIImage* cactusImage=nil;switch (cactusSize){case 0:cactusImage=[UIImage imageNamed:@"CactusLarge"];break;case 1:cactusImage=[UIImage imageNamed:@"CactusMed"];break;case 2:cactusImage=[UIImage imageNamed:@"CactusSmall"];break;default:break;}//随机生成仙人掌的X、Y轴坐标int horizontalLocationX=arc4random()%(int)SCREENWIDTH;int VerticalLocation=arc4random()%3;//由于随机生成的水平位置的最长部分是屏幕边缘,所以应该将其水平位置向内移动仙人掌宽度的大小,、避免超出屏幕float cactusImageWidth=cactusImage.size.width;float cactusImageHeight=cactusImage.size.height;if (horizontalLocationX+cactusImageWidth>SCREENWIDTH){horizontalLocationX=SCREENWIDTH-cactusImageWidth;}UIImageView* duneToSpawnBehind=nil;switch (VerticalLocation){case 0:duneToSpawnBehind=dune1;break;case 1:duneToSpawnBehind=dune2;break;case 2:duneToSpawnBehind=dune3;break;default:break;}int verticalLocationY=duneToSpawnBehind.frame.origin.y;UIButton* cactusBtn=[[UIButton alloc] initWithFrame:CGRectMake(horizontalLocationX, verticalLocationY, cactusImageWidth, cactusImageHeight)];[cactusBtn setImage:cactusImage forState:UIControlStateNormal];[cactusBtn addTarget:self action:@selector(cactusHit:) forControlEvents:UIControlEventTouchUpInside];[self.view insertSubview:cactusBtn belowSubview:duneToSpawnBehind];//生成的仙人掌添加到沙丘的后面之后,就可以运用动画将其跳出来显示[UIView beginAnimations:@"slideCactus" context:nil];[UIView setAnimationDuration:0.2];[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];cactusBtn.frame=CGRectMake(horizontalLocationX, verticalLocationY-cactusImageHeight*0.5, cactusImageWidth, cactusImageHeight);[UIView commitAnimations];[self performSelector:@selector(cactusMissed:) withObject:cactusBtn afterDelay:2.0];
}

3.玩家与仙人掌的交互,仙人掌出来之后两秒内可以被玩家点击,玩家的分数加1,如果两秒内没有被点击,则玩家的生命数减1,玩家生命总共5条,耗完则游戏结束;

-(void)cactusHit:(UIButton*)cactusBtn
{[UIView animateWithDuration:0.1 animations:^{cactusBtn.alpha=0;} completion:^(BOOL finished){[cactusBtn removeFromSuperview];}];score++;[self updateScore];[self performSelector:@selector(spawnCactus) withObject:nil afterDelay:arc4random()%3+0.2];
}
-(void)cactusMissed:(UIButton*)cactusBtn
{CGRect frame=cactusBtn.frame;frame.origin.y+=cactusBtn.frame.size.height;[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState animations:^{cactusBtn.frame=frame;} completion:^(BOOL finished){[cactusBtn removeFromSuperview];[self performSelector:@selector(spawnCactus) withObject:nil afterDelay:arc4random()%3+0.5];}];life--;[self updateLife];
}
- (IBAction)pause:(id)pauseBtn
{pause=YES;UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Pause Now" message:nil delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:@"Quit", nil];alert.tag=kPauseAlert;[alert show];
}

4.玩家得分与生命数的更新;

-(void)updateLife
{UIImage* lifeImage=[UIImage imageNamed:@"heart"];for (UIView* view in [self.view subviews]){if (view.tag==kLifeImageViewTag){[view removeFromSuperview];}}for (int i=0; i<life; i++){UIImageView* lifeImageView=[[UIImageView alloc] initWithImage:lifeImage];CGRect frame=lifeImageView.frame;frame.origin.x=SCREENWIDTH-(i+1)*30;frame.origin.y=20;lifeImageView.frame=frame;lifeImageView.tag=kLifeImageViewTag;[self.view addSubview:lifeImageView];}if (life==0){gameOver=YES;UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Game Over!" message:[NSString stringWithFormat:@"You Total Score are %d points",score] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];alert.tag=kGameOver;[alert show];}
}
-(void)updateScore
{//当得分超过30并且5的倍数时,都将再生成一株仙人掌,从而增加游戏难度if (score>=30&&score%5==0){[self spawnCactus];}self.scoreLabel.text=[NSString stringWithFormat:@"Score:%d",score];
}
-(void)cactusMissed:(UIButton*)cactusBtn
{CGRect frame=cactusBtn.frame;frame.origin.y+=cactusBtn.frame.size.height;[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState animations:^{cactusBtn.frame=frame;} completion:^(BOOL finished){[cactusBtn removeFromSuperview];[self performSelector:@selector(spawnCactus) withObject:nil afterDelay:arc4random()%3+0.5];}];life--;[self updateLife];
}

5.在游戏期间可以暂停游戏,选择继续或者退出;

- (IBAction)pause:(id)pauseBtn
{pause=YES;UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Pause Now" message:nil delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:@"Quit", nil];alert.tag=kPauseAlert;[alert show];
}
#pragma --mark UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{if (alertView.tag==kPauseAlert){if (buttonIndex==0){pause=NO;}else{[self.navigationController popViewControllerAnimated:YES];}}else if (alertView.tag==kGameOver){[self.navigationController popViewControllerAnimated:YES];}
}

iOS ”打仙人掌“游戏一---游戏玩法实现相关推荐

  1. 分享实录 | 技术更迭视角下的游戏语音新玩法

    5月26日,2021 IGS·全球数字文创发展大会"腾讯云·游戏&新文娱分论坛"在成都成功落幕.在本次论坛中,腾讯云游戏多媒体引擎(GME)高级产品经理郑丁益进行了题为&l ...

  2. 《棋牌游戏服务器》玩法服务器架构

    大体上我们的玩法有两种模式,一种是小桌,比如斗地主,一局游戏需要2~6个人:另一种是大桌,所有用户都可以在一桌来玩. 所以"桌"是一个比较核心的概念,玩法服务器的结构也是围绕这个核 ...

  3. H5案例分享-H5游戏跳跃类玩法分享

    又到了每周一次的精品游戏分享时间!这是一款TOM游戏出品的html5实现的跳跃过关类小游戏.小编发现最近朋友圈很多人都在玩儿跳跃类h5游戏,天空熊猫.粽子哪里跑.跳跳犬等小游戏,为什么跳跃类游戏如此受 ...

  4. Pygame实战:打扑克嘛?Python教你“经典纸牌游戏21点”玩法

    导语 ​ 昨天不是周天嘛? 你们在家放松一般都会做什么呢? 周末逛逛街,出去走走看电影......这是你们的周末. 程序员的周末就是在家躺尸唐诗躺尸,偶尔加班加班加班,或者跟着几个朋友在家消遣时间打打 ...

  5. YY一下VR游戏的潜入玩法

    很早之前玩过两个小众的潜入游戏, 分别是<Warp>和<Mark of the Ninja>, 就喜欢上了这类游戏, 不过对我口味的不多, MGS5算一个. 最近又玩了另外两个 ...

  6. 红心大战c语言程序设计,红心大战怎么玩?Win7小游戏红心大战玩法操作介绍

    Win7系统中有很多个小游戏,其中红心大战是很多朋友喜欢的牌类小游戏,在闲暇时来一局还是比较惬意的,当然很多首次玩红心大战的朋友是不知道红心大战怎么玩的,下面小编就不吝赐教和大家分享下玩法. 首先我们 ...

  7. 适合普通人的108个短视频项目:快手游戏合伙人变现玩法(5)

    大家好,我是猫哥,专注于互联网网赚项目与短视频服务. 今天来跟大家分享一个快手游戏类的项目,每天花费的时间也不需要太多 ,利用忙碌完空闲的时间做一做,有人一周就能挣到大几千 这个快手推广小游戏也非常的 ...

  8. 记录七星qp游戏本地修改玩法-前端部分

    有个客户需要做一个扑克类的游戏新化炸弹,在客户提供案例APP后,我思考了一下.最后决定以七星为基础去仿.因为七星本身是有类似玩法的霸炸弹,以此为基础的话可以让开发中期减短,相对来说也可以帮客户节省一部 ...

  9. 一文了解加密游戏illuvium新玩法:探索神兽世界

    Axie是加密游戏发展的催化剂 随着Axie Infinity的崛起,加密游游(链游)和NFT越来越为人们所关注.相对于DeFi,游戏的受众更加广泛,这从Axie游戏用户的快速发展也可以看出来.之前蓝 ...

  10. h5互动小游戏定制开发玩法案例

    又到了每周行业游戏案例大赏时间!今天豆豆给大家带来的游戏是由TOM游戏平台推出的一款适用于积分兑换的百搭H5游戏,该产品效果极佳,目前仍然在对外阶段,想体验的定制商家可下载邮储信用卡app自行体验. ...

最新文章

  1. 求从n个数组任意选取一个元素的所有组合
  2. ORACLE 小时值必须介于1和12之间 解决方法
  3. 百度自动推送html5,百度暂停 JS 代码自动推送功能,代码是否需要删除?
  4. c++ 线程池_JAVA并发编程:线程池ThreadPoolExecutor源码分析
  5. DMAR(DMA remapping)与 IOMMU
  6. mysql汉字格式_mysql 中的varchar255 uft-8 的格式到底能放多少汉字
  7. 瑞星专家:lpk.dll病毒的现象和手工处理
  8. tween.js 中文使用指南
  9. .NET 安全编程 阅读笔记(四)
  10. 2020_0527_近期思考
  11. 土方回填施工方案范本_土方回填施工方案范例(模板)
  12. ker矩阵是什么意思_基向量、标准正交基、对称矩阵、Hermite阵
  13. AHU计科(伪)新生指南
  14. Excel使用条件格式
  15. radio默认选中并显示相应信息 php,php selectradio和checkbox默认选择的实现方法详解...
  16. Java基础----Java语言简介
  17. 邮箱扒头像来告诉你怎么写简单的脚本扒图
  18. 二进制空间权重矩阵_空间权重矩阵(SWM)
  19. Linux camera驱动(1) - 概述
  20. Avada 7.8.1 - 适用于 WordPress 和 WooCommerce 的网站构建器主图下载

热门文章

  1. UG区域拉伸和零件透明在装配中不显示
  2. HDUOJ 4513 吉哥系列故事——完美队形II
  3. 学计算机做纸质笔记,详细图文教你康奈尔大学推荐的超级笔记法,只要一张A4纸张,你也可以做学霸...
  4. adb命令查看手机电池信息
  5. 两个cgi的莫名其妙的core dump问题的解决
  6. javaweb-39:文件上传及拓展鸡汤
  7. 树枝学术 | 图书查找、论文查找全攻略
  8. 1星《微信软文营销实战技巧》:标题党,作者没有实战经验
  9. vc2010下调用miniblink控件实现浏览器简单实例
  10. 计算机锁屏域策略,域组策略锁屏界面设置不生效