飞机大战-(线程学习)

请输入图片描述

请输入图片描述

请输入图片描述

//
// ViewController.m
// fly_boow
//
// Created by 郭瞾阳 on 14-7-31.
// Copyright (c) 2014年 gzy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

UIImage * fly_0;
UIImage * fly_1;
UIImage * fly_2;
UIImage * fly_3;
UIImage * boow;
CALayer * bg_1;
CALayer * bg_2;
CALayer * efly;
CATextLayer * socrelayer;

UIImage * zidan;
NSTimer * timer;
NSMutableArray * difly;
NSMutableArray * fly_dan;
NSInteger  intx;
NSInteger inty;
NSInteger score;
NSInteger sum;
- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect rect = [[UIScreen mainScreen]bounds];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reback:) name:UIApplicationDidEnterBackgroundNotification object:[UIApplication sharedApplication] ];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(rego:) name:UIApplicationWillEnterForegroundNotification object:[UIApplication sharedApplication] ];

    intx = rect.size.width;
    inty = rect.size.height;
    fly_0 = [UIImage imageNamed:@"fly_0.png"];
    fly_1 = [UIImage imageNamed:@"fly_1.png"];
    fly_2 = [UIImage imageNamed:@"fly_2.png"];
    fly_3 = [UIImage imageNamed:@"fly_3.png"];
    difly = [[NSMutableArray alloc]init];
    fly_dan = [[NSMutableArray alloc]init];
    efly = [CALayer layer];
    efly.frame = CGRectMake((intx-56)/2, inty-60, 60, 60);
    [self.view.layer addSublayer:efly];
    socrelayer = [CATextLayer layer];
    socrelayer.frame = CGRectMake(10, 15, 120, 30);
    socrelayer.fontSize = 16;
    socrelayer.foregroundColor = (__bridge CGColorRef)([UIColor blackColor]);

       NSNumber * socre_1;
    if((socre_1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"score"]))
    {
        score = socre_1.integerValue;
    }
    socrelayer.string =[NSString stringWithFormat:@"我已打%d次飞机了,好厉害",score];
    [self.view.layer addSublayer:socrelayer];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(move) userInfo:nil repeats:YES];

}
-(void)move
{
    efly.contents = (id)[fly_0 CGImage];
    for(int i = 0;i<fly_dan.count;i+=3)
    {
        CALayer * bullt = [fly_dan objectAtIndex:i];
        bullt.frame = CGRectOffset(bullt.frame,0 ,-15);
        CALayer * bullt_1 = [fly_dan objectAtIndex:i+1];
        bullt_1.frame = CGRectOffset(bullt_1.frame, -7.5,-7.5 );
        CALayer * bullt_2 = [fly_dan objectAtIndex:i+2];
        bullt_2.frame = CGRectOffset(bullt_2.frame, +7.5,-7.5 );
        if(bullt.position.y<-10)
        {
            [bullt removeFromSuperlayer];
            [bullt_1 removeFromSuperlayer];
            [bullt_2 removeFromSuperlayer];

            [fly_dan removeObject:bullt];
            [fly_dan removeObject:bullt_1];
            [fly_dan removeObject:bullt_2];
       }   
    }
    for(int i = 0;i<difly.count;i++)
    {
        CALayer * fly  = [difly objectAtIndex:i];
        fly.frame = CGRectOffset(fly.frame,0 ,15);
        if(fly.position.y>inty+50)
        {
            [fly removeFromSuperlayer];
            [difly removeObject:fly];
        }
    }
    for(int i = 0;i<fly_dan.count;i++)
    {
        CALayer * bullt = [fly_dan objectAtIndex:i];
        CGPoint bullt_position = bullt.position;
        for(int j = 0;j<difly.count;j++)
        {
            CALayer * diji = [difly objectAtIndex:j];
            if(fabs(efly.position.x-diji.position.x)<20&&fabs(efly.position.y-diji.position.y)<20)
            {
                [efly removeFromSuperlayer];
                [diji removeFromSuperlayer];
                [difly removeObject:diji];
                UIAlertView * nima = [[UIAlertView alloc]initWithTitle:@"打飞机" message:[NSString stringWithFormat:@"你已打%d次飞机了,好厉害",score] delegate:self cancelButtonTitle:@"不打了,太累了" otherButtonTitles:@"继续打飞机", nil ];
                score = 0;
                [timer invalidate];
                CALayer * boow = [CALayer layer];
                boow.frame = CGRectMake(diji.position.x-40, diji.position.y-20, 70, 70);
                boow.contents = (id)[[UIImage imageNamed:@"boow.png"] CGImage];
                [self.view.layer addSublayer:boow];

                [nima show];
            }
            if(CGRectContainsPoint(diji.frame, bullt_position))
            {
                CALayer * boow = [CALayer layer];
                boow.frame = CGRectMake(diji.position.x-40, diji.position.y-20, 70, 70);
                boow.contents = (id)[[UIImage imageNamed:@"boow.png"] CGImage];
                [self.view.layer addSublayer:boow];

                [self performSelector:@selector(hehe:) withObject:boow afterDelay:0.6];
                [bullt removeFromSuperlayer];
                //[fly_dan removeObject:bullt];
                [diji removeFromSuperlayer];
                [difly removeObject:diji];
                score+=1;
                socrelayer.string = [NSString stringWithFormat:@"我已打%d次飞机了,好厉害",score];                break;
            }
        }
    }
           sum++;
    if(sum%5 == 0)
    {
        CALayer *zidan   = [CALayer layer];
        CALayer *zidan_2 = [CALayer layer];
        CALayer *zidan_3 = [CALayer layer];
        zidan.frame = CGRectMake(efly.position.x-12,efly.position.y-50 ,20 ,20 );
        zidan.contents = (id)[[UIImage imageNamed:@"zidan.png"]CGImage];
        zidan_2.frame = CGRectMake(efly.position.x-12,efly.position.y-50 ,20 ,20 );
        zidan_2.contents = (id)[[UIImage imageNamed:@"zidan.png"]CGImage];
        zidan_3.frame = CGRectMake(efly.position.x-12,efly.position.y-50 ,20 ,20 );
        zidan_3.contents = (id)[[UIImage imageNamed:@"zidan.png"]CGImage];
        [self.view.layer addSublayer:zidan];
        [self.view.layer addSublayer:zidan_2];
        [self.view.layer addSublayer:zidan_3];
        [fly_dan addObject:zidan];
        [fly_dan addObject:zidan_2];
        [fly_dan addObject:zidan_3];
    }
    if(sum%10 == 0)
    {
       // int x = random()%3;
        int x = arc4random()%3;
        int wx = arc4random()%(intx-50);
        CALayer * feiji =[CALayer layer] ;
        feiji.frame = CGRectMake(wx,0,60,60);
        switch(x)
        {
            case 0:
                feiji.contents = (id)[fly_1 CGImage];
                break;
            case 1:
                feiji.contents = (id)[fly_2 CGImage];
                break;
            case 2:
                feiji.contents = (id)[fly_3 CGImage];
                break;
         }
        [self.view.layer addSublayer:feiji];
        [difly addObject:feiji];
    }  
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    CGPoint touchid = [touch locationInView:self.view];
    if(touchid.y<inty/2)touchid.y = inty/2;
    efly.position = CGPointMake(touchid.x,touchid.y);

}


-(void)hehe:(CALayer*) nima
{
    [nima removeFromSuperlayer];   
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:    (NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {   
    }
    if(buttonIndex == 1)
    {
    efly.frame = CGRectMake((intx-50)/2, inty-50, 60, 60);
    efly.contents = (id)[UIImage imageNamed:@"fly_0.png"];
    [self.view.layer addSublayer:efly];
    socrelayer.string = [NSString stringWithFormat:@"我已打%d次飞机了,好厉害",score];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(move) userInfo:nil repeats:YES];

    }
}


-(void)reback:(NSNotification*)nima
{
    efly.contents = nil;
    [[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"score"];
}


-(void)rego:(NSNotification*)niam
{
    efly.contents = (id)[[UIImage imageNamed:@"fly_0.png"] CGImage];
    NSNumber * nb;
    if((nb = [[NSUserDefaults standardUserDefaults]objectForKey:@"score"]))
    {
        score = nb.integerValue;
    } 
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

酷行僧
13 声望0 粉丝