1 // 2 // ViewController.m 3 // Blocks 4 // 5 // Created by apple on 13-7-5. 6 // Copyright (c) 2013年 itcast. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import <QuartzCore/QuartzCore.h> 11 12 @interface ViewController () 13 { 14 // 游戏开始的标示 15 BOOL _isPlaying; 16 17 // 小球的速度 18 CGPoint _ballVelocity; 19 20 // 游戏时钟 21 CADisplayLink *_gameTimer; 22 23 // 水平位移 24 CGFloat _deltaX; 25 } 26 27 // 时钟触发方法 28 - (void)step:(CADisplayLink *)sender; 29 30 // 检测与屏幕的碰撞 31 - (BOOL)checkWithScreen; 32 // 检测与砖块的碰撞 33 - (BOOL)checkWithBlocks; 34 // 检测与挡板的碰撞 35 - (void)checkWithPaddle; 36 37 // 重置游戏状态,胜利、失败 38 - (void)resetGameStatusWithMessage:(NSString *)message; 39 40 @end 41 42 @implementation ViewController 43 44 - (void)resetGameStatusWithMessage:(NSString *)message 45 { 46 NSLog(@"%@", message); 47 48 [_gameTimer invalidate]; 49 50 // 将所有的砖头设置成可见 51 for (UIImageView *block in _blockImages) { 52 [block setHidden:NO]; 53 // 将透明度设置为0 54 [block setAlpha:0]; 55 [block setFrame:CGRectMake(block.center.x, block.center.y, 0, 0)]; 56 } 57 [UIView animateWithDuration:2.0 animations:^{ 58 for (UIImageView *block in _blockImages) { 59 // 将透明度设置为0 60 [block setAlpha:1]; 61 [block setFrame:CGRectMake(block.center.x - 32, block.center.y - 10, 64, 20)]; 62 63 } 64 } completion:^(BOOL finished) { 65 _isPlaying = NO; 66 67 [_ballImage setCenter:CGPointMake(160, 430)]; 68 [_paddleImage setCenter:CGPointMake(160, 450)]; 69 70 }]; 71 } 72 73 // 屏幕碰撞检测 74 - (BOOL)checkWithScreen 75 { 76 BOOL gameOver = NO; 77 78 // 左边 79 if ([_ballImage frame].origin.x <= 0) { 80 _ballVelocity.x = abs(_ballVelocity.x); 81 } 82 // 右边 83 if ([_ballImage frame].origin.x + [_ballImage frame].size.width >= self.view.frame.size.width) { 84 _ballVelocity.x = -1 * abs(_ballVelocity.x); 85 } 86 // 顶部 87 if ([_ballImage frame].origin.y <= 0) { 88 _ballVelocity.y = abs(_ballVelocity.y); 89 } 90 // 底部 91 if ([_ballImage frame].origin.y + [_ballImage frame].size.height >= self.view.frame.size.height) { 92 gameOver = YES; 93 } 94 95 return gameOver; 96 } 97 98 - (BOOL)checkWithBlocks 99 { 100 // 砖块的碰撞检测 101 for (UIImageView *block in _blockImages) { 102 // 如果与砖块碰撞,那么隐藏砖块 103 if (CGRectIntersectsRect([block frame], [_ballImage frame]) 104 && ![block isHidden]) { 105 106 [block setHidden:YES]; 107 108 _ballVelocity.y = abs(_ballVelocity.y); 109 110 break; 111 } 112 } 113 114 // 判断游戏胜利 115 BOOL gameWin = YES; 116 117 for (UIImageView *block in _blockImages) { 118 if (![block isHidden]) { 119 gameWin = NO; 120 break; 121 } 122 } 123 124 // int count = 0; 125 // for (UIImageView *block in _blockImages) { 126 // if ([block isHidden]) { 127 // count++; 128 // } 129 // } 130 // 131 // if (count == [_blockImages count] - 1) { 132 // gameWin = YES; 133 // } 134 135 return gameWin; 136 } 137 138 // 挡板的碰撞 139 - (void)checkWithPaddle 140 { 141 if (CGRectIntersectsRect([_ballImage frame], [_paddleImage frame])) { 142 _ballVelocity.y = -1 * abs(_ballVelocity.y); 143 _ballVelocity.x += _deltaX; 144 } 145 } 146 147 - (void)viewDidLoad 148 { 149 [super viewDidLoad]; 150 // Do any additional setup after loading the view, typically from a nib. 151 } 152 153 - (void)didReceiveMemoryWarning 154 { 155 [super didReceiveMemoryWarning]; 156 // Dispose of any resources that can be recreated. 157 } 158 159 // 时钟处理方法 160 - (void)step:(CADisplayLink *)sender 161 { 162 // NSLog(@"运行了!!!"); 163 // 检测屏幕碰撞 164 if ([self checkWithScreen]) { 165 [self resetGameStatusWithMessage:@"再来一次吧!"]; 166 } 167 // 砖块的碰撞 168 if ([self checkWithBlocks]) { 169 [self resetGameStatusWithMessage:@"你真棒!"]; 170 } 171 [self checkWithPaddle]; 172 173 // 让小球动起来 174 [_ballImage setCenter:CGPointMake(_ballImage.center.x + _ballVelocity.x, 175 _ballImage.center.y + _ballVelocity.y)]; 176 177 } 178 179 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 180 { 181 if (_isPlaying) { 182 UITouch *touch = [touches anyObject]; 183 184 // 计算手指在水平方向的移动距离 185 _deltaX = [touch locationInView:self.view].x - [touch previousLocationInView:self.view].x; 186 187 // 修改挡板位置 188 [_paddleImage setCenter:CGPointMake([_paddleImage center].x + _deltaX, [_paddleImage center].y)]; 189 } 190 } 191 192 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 193 { 194 // 如果游戏还没有开始 195 if (!_isPlaying) { 196 // 小球的初始状态 197 _ballVelocity = CGPointMake(0, -5); 198 199 // 定义时钟 200 _gameTimer = [CADisplayLink displayLinkWithTarget:self 201 selector:@selector(step:)]; 202 203 // 将当前时钟添加到应用循环 204 [_gameTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 205 206 // 修改游戏状态标示 207 _isPlaying = YES; 208 } else { 209 _deltaX = 0; 210 } 211 } 212 213 @end
时间: 2024-11-24 07:05:13