Flappy Bird

愤怒的小鸟这个游戏非常经典,火了很长一段时间

我们可以不借助第三方库进行实现,这里只是进行了一些简单的实现

没有添加碰撞检测  完成了一些功能 我们点击屏幕小鸟会飞

如果不点击小鸟会落下 

这里用到的技术也很简单,用到了用户交互,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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值