zoukankan      html  css  js  c++  java
  • #在蓝懿学习iOS的日子#Day10

    #在蓝懿学习iOS的日子#Day10今天做了一个大的游戏,涵盖了这段时间学习的知识,页面的切换字符串,还有可变数组和遍历等知识点。

    一、界面的背景
    1、显示搭建HomeViewController添加背景,创建一个SelectedViewController,添加一个button点击进入下一个SelectedViewController;
    2、在SelectedViewController搭建视图添加背景,创建一个英雄hero类,把不同的英雄连接进同一个button,设置tag用以区分hero类;创建一个游戏ViewController在点击button进入游戏页面,
    3、在LevelOneViewController添加移动的背景视图,//第二张图片放在第一张的上面,y:-self.view.frame.size.height

    -(void)initBG{

        //添加背景图片

        self.bgIV1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];

    //第二张图片放在第一张的上面,y:-self.view.frame.size.height

        self.bgIV2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, -self.view.frame.size.height,self.view.frame.size.width, self.view.frame.size.height)];

        self.bgIV1.image = [UIImage imageNamed:@"img_bg_level_1.jpg"];

        self.bgIV2.image = [UIImage imageNamed:@"img_bg_level_1.jpg"];

        //插入子控件到某个位置,把背景图片放最下边

        [self.view insertSubview:self.bgIV1 atIndex:0];

        [self.view insertSubview:self.bgIV2 atIndex:0];

        

        //给 背景的移动设置TImer

        [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(moveBG) userInfo:nilrepeats:YES];
       }

    -(void)moveBG{

        //设置图片往下移动y+1

        self.bgIV1.center = CGPointMake(self.bgIV1.center.x, self.bgIV1.center.y+1);

        self.bgIV2.center = CGPointMake(self.bgIV2.center.x, self.bgIV2.center.y+1);

        

        //两张图片的衔接

        if (self.bgIV1.center.y>=self.view.frame.size.height*1.5) {

            

            self.bgIV1.center = CGPointMake(self.bgIV1.center.x, -self.view.frame.size.height/2);

        }

        if (self.bgIV2.center.y>=self.view.frame.size.height*1.5) {

              self.bgIV2.center = CGPointMake(self.bgIV2.center.x, -self.view.frame.size.height/2);
        }
    二、选择飞机、添加飞机
    1、选择飞机:创建一个英雄hero类,把不同的英雄连接进同一个button,设置tag用以区分hero类;
     vc.heroID = sender.tag;
    创建一个LevelOneViewController在点击button进入游戏页面,hero类在运用创建工厂方法创建Hero
    (1)英雄hero类.h添加一个有参数的工厂方法
    //声明方法
    +(Hero*)heroWithID:(long)heroID andFrame:(CGRect)frame;
    (2)英雄hero类.m使用有参数的工厂方法
    +(Hero *)heroWithID:(long)heroID andFrame:(CGRect)frame{
        Hero *h = [[Hero alloc]initWithFrame:frame];
       //用字符串对象变量图片
        NSString *name = [NSString stringWithFormat:@"hero%ld.png",heroID];
        h.image = [UIImage imageNamed:name];
        return h;
    }
    (3)在LevelOneViewController.m使用有参数的工厂方法添加的对象
    -(void)initHero{
        //添加hero
        self.hero = [Hero heroWithID:self.heroID andFrame:CGRectMake(150, 550, 80, 100)];
       
        [self.view addSubview:self.hero];
    }
    //手指触摸屏幕的时候会调用此方法 从此方法中得到触摸屏幕的坐标
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        //得到触摸的点
        UITouch *t = [touches anyObject];
        CGPoint p = [t locationInView:self.view];
       
        self.hero.center = p;
    }
    三、加子弹
    (1)英雄hero类.h添加
    //声明可变数组
    @property (nonatomic, strong)NSMutableArray *bullets;
    2)英雄hero类.m
     //调用开火的方法
        [h beginFire];
    -(void)beginFire{
        //子弹可变数组初始化
        self.bullets = [NSMutableArray array];
        //给子弹的出现添加timer
        [NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(addbulletAction) userInfo:nil repeats:YES];
        //开启timer 移动子弹
        [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(moveBullet) userInfo:nil repeats:YES];
    }
    -(void)moveBullet{
       //遍历数组self.bullets
        for (UIImageView *b in self.bullets) {
            b.center = CGPointMake(b.center.x, b.center.y-5);
            //当子弹飞出界面
            if (b.center.y<0) {
                [b removeFromSuperview];
                [self.bullets removeObject:b];
                break;
            }
           
        }
       
    }
    -(void)addbulletAction{
        //添加子弹图片
        UIImageView *bulletIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 20)];
        bulletIV.image = [UIImage imageNamed:@"myb_2.png"];
        //移动子弹
        bulletIV.center = CGPointMake(self.center.x, self.center.y-20);
         //将子弹图片添加进视图
        [self.superview addSubview:bulletIV];
        [self.bullets addObject:bulletIV];
       
    }
    四、添加敌机(Enemy):创建Enemy的父类,并且创建四个子类,再创建一个敌机的大Boss继承父类 ,Enemy作为B类反向传值给A类的LevelOneViewController
    (1)敌机(Enemy)类.h添加相关的变量,
    //速度
    @property (nonatomic)float speed;
    //图片
    @property (nonatomic, copy)NSString *name;
    //timer
    @property (nonatomic, strong)NSTimer *timer;
    //在B类.h中声明一个A类型的属性delegate为weak
    @property (nonatomic, weak)LevelOneViewController *delegate;
    (2)敌机(Enemy)类.m添加timer控制敌机的出现和移动,并到子类里给相关的变量赋值
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
           self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(moveAction) userInfo:nil repeats:YES];

        }
        return self;
    }
    -(void)moveAction{
       
        self.center = CGPointMake(self.center.x, self.center.y+self.speed);
        //离开页面的时候
        if (self.center.y>667) {
           
            [self.timer invalidate];
            [self removeFromSuperview];
            //在B类中通过self.delegate去调用A类中的方法
            [self.delegate removeEnemy:self];
           
        }
       
    }

    -(void)dead{
       
        self.image = [UIImage imageNamed:@"dead_1.png"];
     
       
        //让控件的透明度为0 并且以动画的形式展现
       
        [UIView animateWithDuration:.5 animations:^{
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
       
    }
     
    (3)在LevelOneViewController.m添加Enemy和Boss的对象
    -(void)addEnemy{
       
        int type = arc4random()%4;
       
        Enemy *e = nil;
        switch (type) {
            case 0:
                e = [[EnemyA alloc]initWithFrame:CGRectMake(arc4random()%(int)(self.view.bounds.size.width-100), -80, 60, 80)];
                break;
            case 1:
                e = [[EnemyB alloc]initWithFrame:CGRectMake(arc4random()%(int)(self.view.bounds.size.width-100), -80, 60, 80)];
               
                break;
            case 2:
                e = [[EnemyC alloc]initWithFrame:CGRectMake(arc4random()%(int)(self.view.bounds.size.width-100), -80, 60, 80)];
                break;
            case 3:
                e = [[EnemyD alloc]initWithFrame:CGRectMake(arc4random()%(int)(self.view.bounds.size.width-100), -80, 60, 80)];
                break;
        }
        //在A类.m中创建B类的时候给B类的delegate赋值为self
        e.delegate = self;
        [self.view addSubview:e];
        [self.enemys addObject:e];
       
    }
    //在A类.h中声明一个要调用的方法的使用
    -(void)removeEnemy:(Enemy *)enemy{
       
        [self.enemys removeObject:enemy];
       
    }
    //添加Boss
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(addBoss) userInfo:nil repeats:NO];
    -(void)addBoss{
    //boss对象
        Boss *boss = [[Boss alloc]initWithFrame:CGRectMake(100, -200, 200, 200)];
        [self.view addSubview:boss];
      
        [self.enemys addObject:boss];
    五、子弹和敌机碰撞
     
    六、Boss和得分
     
    七、游戏结束和飞机碰撞 
  • 相关阅读:
    Linux基本命令-chmod
    Linux操作系统启动流程
    博客园背景美化
    Windows安装python3.x后,pip list警告!DEPRECATION: The default format will switch to columns in the future.
    Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
    .Net程序员学用Oracle系列(23):视图理论、物化视图
    .Net程序员学用Oracle系列(22):分析函数(OVER)
    .Net程序员学用Oracle系列(21):分组查询(GROUP BY)
    .Net程序员学用Oracle系列(20):层次查询(CONNECT BY)
    .Net程序员学用Oracle系列(19):导出、导入(备份、还原)
  • 原文地址:https://www.cnblogs.com/odileye/p/4931433.html
Copyright © 2011-2022 走看看