首页 > 移动平台 > 详细

#在蓝懿学习iOS的日子#小游戏吃豆人

时间:2015-10-20 11:39:20      阅读:278      评论:0      收藏:0      [点我收藏+]

#在蓝懿学习iOS的日子#由于初学,我们都没有掌握什么高级技巧,需要用比较繁琐的的代码l来表示一个小小的东西,那么我们就来回忆一下吃豆人的不周吧

第一,我们要插入一张图片为吃豆人(即:吃货)和四个按钮,并使其能上下左右的移动

例://设置按钮

- (IBAction)top:(id)sender {

    x = 0;

    y = -1.5;

,还要设置吃豆人的运动范围

例:chihuo.center = CGPointMake(chihuo.center.x+x, chihuo.center.y+y);

    if (chihuo.center.x>=300) {

        chihuo.center = CGPointMake(300, chihuo.center.y);

    }

第二,设置插入一张图片(即:food)

出现食物的间隔

 //设置食物出现的间隔

    [self addfood];

    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(addfood) userInfo:nil repeats:YES];

}

-(void)addfood{

//设置食物

  food = [[UIImageView alloc]initWithFrame:CGRectMake(arc4random()%280, arc4random()%528, 40, 40)];

    food.image = [UIImage imageNamed:@"a.jpg"];

    [self.view addSubview:food];

//设置食物出现的时间

    [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(missFood) userInfo:nil repeats:NO];

}

-(void)missFood{

//食物移出屏幕

    food.center = CGPointMake(1000, 0);

//判断吃货是否有吃到食物     判断吃货是否在疯狂状态

    if (CGRectIntersectsRect(chihuo.frame, food.frame)&&chihuo. tag == 0) {

        food.center = CGPointMake(1000, 0);

        chihuo.image = [UIImage imageNamed:@"b.jpg"];

        chihuo. tag = 1;

        [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(moveAction) userInfo:nil repeats:NO];

第三,设置小鬼(即:插入图片)的出现运动轨迹,和吃豆人的相撞

 //判断chihuo和小鬼的碰撞

    if (CGRectIntersectsRect(chihuo.frame, topiv.frame)) {

        if (chihuo.tag==0) {

            //数字和文本的转换,叠加死亡的次数

            int count =diedcount.text. intValue;

            diedcount.text =[@(count+1)stringValue];

            //chihuo碰到小鬼死亡,回到中心点

            chihuo.center = CGPointMake(160, 568/2);

            //chihuo在中心点,保持不动

            x= 0;

            y= 0;

        }else{

            //吃货在吃掉食物后,变强大,小鬼消失

            topiv.center = CGPointMake(1000, 0);

        }

第四,设置吃豆人丢失生命的次数(用label)来记录,

if (CGRectIntersectsRect(chihuo.frame,bottomiv.frame)) {

        if (chihuo.tag==0) {

            //数字和文本的转换,叠加死亡的次数

            int count =diedcount.text. intValue;

            diedcount.text =[@(count+1)stringValue];

            //chihuo碰到小鬼死亡,回到中心点

            chihuo.center = CGPointMake(160, 568/2);

            //chihuo在中心点,保持不动

            x= 0;

            y= 0;

        }else{

            //吃货在吃掉食物后,变强大,小鬼消失

            bottomiv.center = CGPointMake(1000, 0);

}

大家,要一起共勉,要一起加油啊,技术分享

#在蓝懿学习iOS的日子#小游戏吃豆人

原文:http://www.cnblogs.com/odileye/p/4894173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!