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
//
// 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
相关文章推荐
- 自己动手写一个 iOS 网络请求库(二)——封装接口
- 自己动手编写一个VS插件(二)——理解OnConnection函数
- 自己编写一个struts
- 在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理。 例如: //实例化一个NSDateFormatter对象 NSDateFor
- 资源监控_自己编写一个nmon
- IOS 自定义控件 非教程~只是记录自己写的一个简单控件
- ucos ii的特点 1.ucos ii是由Labrosse先生编写的一个开放式内核,最主要的特点就是源码公开。这一点对于用户来说可谓利弊各半,好处在于,一方面它是免费的,另一方面用户可以根据自己的需
- 一个自己编写的xml和JAVA对象之间的转换工具,xml和对象轻松转换(simple-xml2object)
- 自己动手编写一个网络图片爬虫
- 自己动手编写一个VS插件(五)
- 自己动手编写一个简单的U盘杀毒程序
- 自己编写的一个感染文件夹的小病毒
- 为一个 iOS 应用编写一个简单的 Node.js/MongoDB Web 服务
- 编写一个自己的Servlet(一)
- 自己动手编写一个VS插件(一)
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC
- 让我们自己编写一个操作系统(OS)(一)
- 以一个初学者的眼光看自己编写的第一个j2EE程序
- 自己动手编写一个VS插件(四)——创建工具栏之二
- 自己动手写一个 iOS 网络请求库(五)——设置 SSL 钢钉