《30天精通iPhone手机编程》-Day30-打砖块游戏

        世界上的第一款打砖块的游戏的设计者就是创立苹果的史蒂夫 乔布斯,最后一天的demo就是完成一个功能简单的打砖块的游戏。

BoardView.m

//触摸开始时调用
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	//定义图形位置变量对象的坐标,locationInView表明视图中触摸点的坐标
	startLocation= [[touches anyObject] locationInView:self];
	//把BoardView子视图放在最前面
	[[self superview] bringSubviewToFront:self];
}
//获取手指的触摸移动事件方法
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
	//定义图形位置变量对象的坐标,locationInView表明视图中触摸点的坐标
	CGPoint pt=[[touches anyObject] locationInView:self];
	//创建图形框架尺寸变量的对象
	CGRect frame=[self frame];
	//定义图形框架位置变量对象的x坐标值,pt.x表明图形位置变量对象的x坐标值,startLocation.x表明触摸点的x坐标
	frame.origin.x = frame.origin.x + (pt.x-startLocation.x);
	//设定图形框架位置变量数值
	[self setFrame:frame];
}

MainViewController.m

-(void)playSound:(NSString*)soundAct{
	
	NSString *path = [NSString stringWithFormat:@"%@%@",
					  [[NSBundle mainBundle] resourcePath],
					  soundAct];
	
	NSLog(@"%@\n", path);
	
	SystemSoundID soundID;
	
	NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
	
	AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
	
	AudioServicesPlaySystemSound(soundID);
	
	[filePath release];
	
}

- (IBAction)onLeft:(id)sender {
	
	[UIImageView beginAnimations:@"animLeft" context:NULL];
	
	[UIImageView setAnimationDuration:0.2];
	
	board.center=CGPointMake(board.center.x-20, board.center.y);
	
	[UIImageView commitAnimations];
	
	soundFile = [NSString stringWithFormat:@"/button_press.caf"];
	
	[self playSound: soundFile];  	
	
}

- (IBAction)onRight:(id)sender {
	
	[UIImageView beginAnimations:@"animRight" context:NULL];
	
	[UIImageView setAnimationDuration:0.2];
	
	board.center=CGPointMake(board.center.x+20, board.center.y);
	
	[UIImageView commitAnimations];
	
	soundFile = [NSString stringWithFormat:@"/button_press.caf"];
	
	[self playSound: soundFile];
	
}

- (IBAction)onStart:(id)sender {
	
	if(!timer){
		timer=[NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
		//球开始时的坐标
		ball.frame=CGRectMake(160, 328, 32, 32);
		[self.view addSubview:ball];
		//弹板开始时的坐标
		board.frame=CGRectMake(160, 360, 48, 10);
	}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	//球的动态移动距离
	moveDis=CGPointMake(-3, -3);
	speed=0.03;
	board=[[BoardView alloc] initWithImage:[UIImage imageNamed:@"board.png"]];
	//定义图像的用户交互作用属性值
	[board setUserInteractionEnabled:YES];
	//定义弹板的坐标,尺寸
	board.frame=CGRectMake(160, 360, BOARDWIDTH, BOARDHIGHT);
	ball=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];
	[self.view addSubview:board];
	level=1; score=0; highest=0;
	levelLabel.text=[NSString stringWithFormat:@"游戏级别: %i",level];
	scoreLabel.text=[NSString stringWithFormat:@"现时得分: %i",score];
	highestLabel.text=[NSString stringWithFormat:@"最高成绩: %i",highest];
	//调用显示游戏级数方法
	[self levelMap:level];
}
//显示游戏级别方法
- (void)levelMap:(int)inlevel {
	UIImageView *brick;
	switch (inlevel) {
		case 1:
			bricks=[NSMutableArray arrayWithCapacity:20];
			//砖块数量
			numOfBricks=20;
			for (int i=0; i<3; i++) {
				for (int j=0;j<6;j++) {
					brick=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brick.png"]];
                    //定义砖块图像视图开始时的坐标位置和尺寸大小
					brick.frame=CGRectMake(20+j*BRICKWIDTH+j*5, TOP+10+BRICKHIGHT*i+5*i, BRICKWIDTH, BRICKHIGHT);
					[self.view addSubview:brick];
					[bricks addObject:brick];
				}
			}
			brick=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brick.png"]];
			//定义砖块图像视图开始时的坐标位置和尺寸大小
			brick.frame=CGRectMake(20, TOP+10+20*2+5*4, BRICKWIDTH, BRICKHIGHT);
			
			[self.view addSubview:brick];
			
			[bricks addObject:brick];
			
			brick=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brick.png"]];
			
			brick.frame=CGRectMake(20+5*BRICKWIDTH+5*5, TOP+10+20*2+5*4, BRICKWIDTH, BRICKHIGHT);
			
			[self.view addSubview:brick];
			
			[bricks addObject:brick];
			
			[bricks retain];
			
			break;
			
		case 2:
			
			bricks=[NSMutableArray arrayWithCapacity:28];
			
			numOfBricks=20;
			
			for (int i=0; i<7; i++) {
				
				for (int j=0;j<2;j++) {
					
					brick=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brick.png"]];
					
					brick.frame=CGRectMake(20+j*BRICKWIDTH+j*5, TOP+10+BRICKHIGHT*i+5*i, BRICKWIDTH, BRICKHIGHT);
					
					[self.view addSubview:brick];
					
					[bricks addObject:brick];
					
				}
			}
			
			for (int i=0; i<7; i++) {
				
				for (int j=0;j<2;j++) {
					
					brick=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brick.png"]];
					brick.frame=CGRectMake(20+j*BRICKWIDTH+j*5+180, TOP+10+BRICKHIGHT*i+5*i, BRICKWIDTH, BRICKHIGHT);
					[self.view addSubview:brick];
					[bricks addObject:brick];
					
				}
			}
			
			[bricks retain];
			
			break;
			
		default:
			break;
	}
}

- (void)onTimer{
	
	float posx,posy;
	//球的中心坐标
	posx=ball.center.x; posy=ball.center.y;
	
	ball.center = CGPointMake(posx+moveDis.x, posy+moveDis.y);
	
	if (ball.center.x>305 || ball.center.x<15 ) {
		
		moveDis.x=-moveDis.x;
		
	}
	
	if ( ball.center.y<TOP ) {
		
		moveDis.y=-moveDis.y;
		
	}
	
	int j=[bricks count];
	
	for (int i=0; i<j; i++) {
		
		UIImageView *brick=(UIImageView *)[bricks objectAtIndex:i];
		
		if (CGRectIntersectsRect(ball.frame, brick.frame)&&[brick superview]) {
			
			soundFile = [NSString stringWithFormat:@"/Brick_move.caf"];
			
			[self playSound: soundFile];
			
			score+=100;
			
			[brick removeFromSuperview];
			
			if (rand()%5==0) {
				
				UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shorten.png"]];
				
				imageView.frame = CGRectMake(brick.frame.origin.x, brick.frame.origin.y, 48, 48);
				
				[self.view addSubview:imageView];
				
				[UIView beginAnimations:nil context:imageView];
				
				[UIView setAnimationDuration:5.0];
				
				[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
				
				imageView.frame = CGRectMake(brick.frame.origin.x, 380, 40, 40);
				
				[UIView setAnimationDelegate:self];
				
				[UIView setAnimationDidStopSelector:@selector(removeSmoke:finished:context:)];
				
				[UIView commitAnimations];
				
			}
			
			numOfBricks--;
			
			if ((ball.center.y-16<brick.frame.origin.y+BRICKHIGHT || ball.center.y+16>brick.frame.origin.y) 
				&& ball.center.x>brick.frame.origin.x && ball.center.x<brick.frame.origin.x+BRICKWIDTH) {
				
				moveDis.y=-moveDis.y;
				
			}else if (ball.center.y>brick.frame.origin.y && ball.center.y<brick.frame.origin.y+BRICKHIGHT 
					  && (ball.center.x+16>brick.frame.origin.x || ball.center.x-16<brick.frame.origin.x+BRICKWIDTH)){
				
				moveDis.x=-moveDis.x;
				
			}else{
				
				moveDis.x=-moveDis.x;
				moveDis.y=-moveDis.y;
			}
			
			break;
			
		}
	}
	
	if (numOfBricks==0) {
		
		if (level<2) {
			
			[ball removeFromSuperview];
			
			[timer invalidate];
			
			level++; speed=speed-0.003;
			
			levelLabel.text=[NSString stringWithFormat:@"Level %i",level];
			
			[self levelMap:level];
			
		}else{
			
			UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"K.O." 
														  message:@"恭喜!你赢了"
														 delegate:self cancelButtonTitle:@"OK" 
												otherButtonTitles:nil];
			
			[alert show];
			
			soundFile = [NSString stringWithFormat:@"/Win.caf"];
			
			[self playSound: soundFile];
			
		}
	}
	
	if (CGRectIntersectsRect(ball.frame, board.frame)) {
		
		soundFile = [NSString stringWithFormat:@"/Kick.caf"];
		
		[self playSound: soundFile];		
		
		if (ball.center.x>board.frame.origin.x&&ball.center.x<board.frame.origin.x+BOARDWIDTH) {
			
			moveDis.y=-moveDis.y;
			
		}else {
			
			moveDis.x=-moveDis.x;
			moveDis.y=-moveDis.y;
			
		}
		
	}else{
		
		if (ball.center.y>380){
			
			[ball removeFromSuperview];
			
			[timer invalidate];
			
			timer=NULL;
			
			UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Game over"
														  message:@"你输了,继续获取更好的成绩..."
														 delegate:self 
												cancelButtonTitle:@"确定"
												otherButtonTitles:nil];
			[alert show];
			
			soundFile = [NSString stringWithFormat:@"/Lose.caf"];
			
			[self playSound: soundFile];			
			
		}
		
	}
	
	scoreLabel.text=[NSString stringWithFormat:@"现时得分: %i",score];
}




你可能感兴趣的:(编程,timer,iPhone,手机,UIView,图形)