愤怒的小鸟这个游戏非常经典,火了很长一段时间
我们可以不借助第三方库进行实现,这里只是进行了一些简单的实现
没有添加碰撞检测 完成了一些功能 我们点击屏幕小鸟会飞
如果不点击小鸟会落下
这里用到的技术也很简单,用到了用户交互,transform 动画
#import "BirdViewController.h" //初速 const float MaxTime = 30; //加速度 方向向下 const float VG = 0.05; //初速度 const float MaxV = 2.5; //初始化总路程 const float AllLength = 692; typedef enum{ GameStart, GamePlaying, GameOver } GameState; @interface BirdViewController () { NSTimer *birdTimer; //开始游戏开关 BOOL isStart; //游戏状态 GameState gameState; //小鸟 UIImageView *birdImgView; UIImageView *birdClotherView; //总场景 UIView *playLayer; //跳跃时间 float maxJumpTime; } @end @implementation BirdViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor =[UIColor orangeColor]; [self initBirdAndOther]; birdTimer = [NSTimer scheduledTimerWithTimeInterval:0.008 target:self selector:@selector(update) userInfo:nil repeats:YES]; } -(void)update { //判断 if (isStart == YES && gameState == GamePlaying) { [self updateBird]; } } -(void)initBirdAndOther { playLayer = [[UIView alloc]initWithFrame:self.view.bounds]; [self.view addSubview:playLayer]; //添加手势 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(screenTap)]; [self.view addGestureRecognizer:tapGesture]; birdClotherView = (UIImageView *)[[UIView alloc]initWithFrame:CGRectMake(60, 300, 40, 30)]; [playLayer addSubview:birdClotherView]; //初始化小鸟 birdImgView = [[UIImageView alloc] init]; birdImgView.frame = CGRectMake(0, 0, 40, 28); birdImgView.image = [UIImage imageNamed:@"bird"]; [birdClotherView addSubview:birdImgView]; UILabel *label = [[UILabel alloc]init]; label.frame = CGRectMake(20, 30, 100, 30); label.text = @"愤怒的小鸟"; [self.view addSubview:label]; } -(void)screenTap { //每点击一次 maxJumpTime = MaxTime; if (isStart == NO) { isStart = YES; gameState = GamePlaying; for (UIView *tmp in self.view.subviews) { [tmp removeFromSuperview]; } [self initBirdAndOther]; } CGAffineTransform transform = CGAffineTransformIdentity; birdImgView.transform = CGAffineTransformRotate(transform, -30 * M_PI / 180 ); } - (void) updateBird { maxJumpTime --; CGRect rect = birdClotherView.frame; if (maxJumpTime >= 0) { rect.origin.y = rect.origin.y - (MaxV - (MaxTime - maxJumpTime)*VG); } else { //俯角30度旋转 CGAffineTransform transform = CGAffineTransformIdentity; birdImgView.transform = CGAffineTransformRotate(transform, 30 * M_PI / 180 ); rect.origin.y = rect.origin.y - (maxJumpTime*VG); } birdClotherView.frame = rect; } @end
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u012701023/article/details/48108243