您的位置:首页 > 移动开发 > IOS开发

ios 自己编写的一个弹球小游戏

2014-02-25 17:22 363 查看
.h文件

//

//  Created by hoperun. on 14-2-25.

//  Copyright (c) 2014年 ding_qiang. All rights reserved.

//

#import <Foundation/Foundation.h>

#import <CoreMotion/CoreMotion.h>

@interface firstview_controller : UIViewController<UIAccelerometerDelegate,UIGestureRecognizerDelegate,UIApplicationDelegate>

{

    UIImageView *m_imageview;

    UILabel *m_imageview1;

    UIImage *m_image;

    UIImage *m_image1;

    int score;

    CGPoint ballmovement;

    

    float touchoffset;

    UIAccelerometer *theaccel;

    NSTimer *timer;

    BOOL end;

    

    int lives;

    UILabel *livesLabel;

    UILabel *messagelabel;

    UILabel *scorelabel;

    bool isplaying;

}

@end

.m文件

//  Copyright (c) 2014年 ding_qiang. All rights reserved.

#import "firstview_controller.h"

@implementation firstview_controller

-(id)init

{

    self = [super init];

    

    m_image = [UIImage imageNamed:@"1.jpg"];

    

    m_imageview = [[UIImageView alloc]initWithImage:m_image];

    [m_imageview setFrame:CGRectMake(100, 100, 50, 50)];

   

//    m_image1 = [UIImage imageNamed:@"2.jpg"];

    m_imageview1 = [[UILabel alloc]init];

    

    [m_imageview1 setBackgroundColor:[UIColor blackColor]];

    [m_imageview1 setFrame:CGRectMake(100, 400, 50, 10)];

   

//    UISwipeGestureRecognizer *recognizer;

//     UISwipeGestureRecognizer *recognizer1;

    

//    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];

//      recognizer1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];

//    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];

//    [m_imageview1 addGestureRecognizer:recognizer];

//    [recognizer1 setDirection:UISwipeGestureRecognizerDirectionRight];

//    [m_imageview1 addGestureRecognizer:recognizer1];

    

    

    livesLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 100, 20)];

    

    

    messagelabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 350, 200, 50)];

    

    scorelabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 40, 100, 20)];;

 

    

    

//    ballmovement = CGPointMake(4, 2);

    [self.view addSubview:messagelabel];

  [self.view addSubview:scorelabel];

    [self.view addSubview:m_imageview1];

[self.view addSubview:livesLabel];//添加生命的label

    [self.view addSubview:m_imageview];

    self.view.backgroundColor = [UIColor whiteColor];

 

    

    return  self;

}

-(void)viewDidLoad

{

    m_imageview1.userInteractionEnabled =YES;//uiimageview 默认的响应事件是关闭的必须手动开启

    theaccel = [UIAccelerometer sharedAccelerometer];//使用屏幕左右移动改变滑条位置(现在版本不建议使用这个函数)

    theaccel.updateInterval = 1.0f/30;

    theaccel.delegate = self;

    [super viewDidLoad];

    [self startplaing];

}

-(void)applicationWillTerminate:(UIApplication *)application

{

    

NSLog(@"applicationWillTerminate");

}

-(void)applicationWillResignActive:(UIApplication *)application

{

    NSLog(@"xxxx");

}

-(void)startplaing

{

    if(!lives)

    {

        lives = 1;

        score = 0;

    }

//    NSUserDefaults   可以使用作为按home键的时候保存临时数据使用

//    使用方法:

//    [[NSUserDefaults standardUserDefaults]setInteger:lives forKey:@"xxxx"];

//    [[NSUserDefaults standardUserDefaults]integerForKey:@"xxxx"];

    

    

    

    livesLabel.text = [NSString stringWithFormat:@"剩余生命%d",lives];

    scorelabel.text = [NSString stringWithFormat:@"得分%d",score];

    m_imageview.center = CGPointMake(200, 100);

    ballmovement = CGPointMake(6, 6);

    messagelabel.text = @"GAME OVER";

    messagelabel.hidden = YES;

    isplaying = YES;

    if(arc4random()%100 <50)

    {

        ballmovement.x = - ballmovement.x;

    }

     [self initializeTimer];

}

-(void)initializeTimer

{

    float theInterval = 1.0/30;

    timer = [NSTimer scheduledTimerWithTimeInterval:theInterval target:self selector:@selector(animateBall:) userInfo:Nil repeats:YES];

    

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [[event allTouches]anyObject];

    touchoffset = m_imageview1.center.x-[touch locationInView:touch.view].x;

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//使用uitouch进行滑条移动

{

    

    if(isplaying)

    {

    

        UITouch *touch = [[event allTouches]anyObject];

        float distance = [touch locationInView:touch.view].x+touchoffset-m_imageview1.center.x;

        float newx = m_imageview1.center.x+distance;

        if(newx>50 && newx <270)

        {

            m_imageview1 .center = CGPointMake(newx, m_imageview1.center.y);

        }

    }

    

    

    

    

    

    

    

    

//    UITouch *touch = [[event allTouches]anyObject];

//    float distanceMoved = ([touch locationInView:touch.view].x+touchoffset)-m_imageview1.center.x;

//    float newx = m_imageview1.center.x+distanceMoved;

//    if(newx>30 && newx <290)

//    {

//        m_imageview1.center=CGPointMake(newx, m_imageview1.center.y);

//    }

//    if(newx>290)

//    {

//        m_imageview1.center = CGPointMake(290, m_imageview1.center.y);

//    }

//    if(newx<30)

//    {

//        m_imageview1.center = CGPointMake(30, m_imagevi
4000
ew1.center.y);

//    }

}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)swipe//用于UISwipeGestureRecognizer的时候左右拖动的功能,但这个反应交慢

{

    if(swipe.direction==UISwipeGestureRecognizerDirectionLeft) {

        

        NSLog(@"swipe left");

        m_imageview1.center = CGPointMake(m_imageview1.center.x-10, m_imageview1.center.y);

        

        

    }

    

    if(swipe.direction==UISwipeGestureRecognizerDirectionRight) {

        

        NSLog(@"swipe right");

         m_imageview1.center = CGPointMake(m_imageview1.center.x+10, m_imageview1.center.y);

        

    }

}

-(void)pasegame

{

//

    [self relod];

   

    

}

-(void)relod

{

    [timer invalidate];

    timer = nil;

    [self viewDidLoad];

}

-(void)animateBall:(NSTimer *)theTimer

{

    m_imageview.center = CGPointMake(m_imageview.center.x+ballmovement.x, m_imageview.center.y+ballmovement.y);

//    if(m_imageview.center.y >=m_imageview1.center.y-30 && m_imageview.center.y<= m_imageview1.center.y+30&&

//       m_imageview.center.x >=m_imageview1.center.x-25 && m_imageview.center.x<= m_imageview1.center.x+25)

    

    

    //CGRectIntersectsRect 用于表示两个图像是否相交

    if(CGRectIntersectsRect(m_imageview1.frame, m_imageview.frame))

    {

        ballmovement.y = -ballmovement.y;

        score++;

        scorelabel.text = [NSString stringWithFormat:@"得分%d",score];

    }

    

    if(m_imageview.center.x>300||m_imageview.center.x<20)

    {

        ballmovement.x = -ballmovement.x;

    }

    if(m_imageview.center.y>460||m_imageview.center.y<20)

    {

        ballmovement.y = -ballmovement.y;

    }

    

    if(m_imageview.center.y<25)

    {

        ballmovement.y = -ballmovement.y;

    }

    

    if(m_imageview.center.y>(400+8-25))

    {

        

        isplaying = NO;

        lives--;

        livesLabel.text = [NSString stringWithFormat:@"%d",lives];

        if(!lives)

        {

            messagelabel.text = @"GAME OVER";

        }

        else

        {

            messagelabel.text = @"BALL OUT OF BOUNDS";

        }

        messagelabel.hidden=NO;

        [self pasegame];

    }

    

}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

{

    float newx = m_imageview1.center.x+(acceleration.x*12);

    if(newx>30||newx<290)

    {

        m_imageview1.center = CGPointMake(newx, m_imageview1.center.y);

    }

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐