1//关数类的游戏 2//1.开始按钮,按钮移除 3//2.游戏的初始化(1.普通色块的颜色。2.特殊色块的位置。3.特殊色块的颜色。) 4//3.collectView刷新。 5 //cell方法: 设置cell背景色 6 //indexPath.row == 特殊色块的位置 7 //设置特殊颜色 8 //else ...设置普通颜色 9//4.游戏结束的时候(计时器==0,alert->代码块->游戏调成没法玩的状态(开始按钮显示)) 10#import "ViewController.h" 11 12@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> 13{ 14 CGFloat red,green,blue;//普通色块 15 CGFloat redS,greenS,blueS;//特殊色块 16 int differentCell;//记录特殊色块位置 17 int GameTime;//记录游戏总时间(方便修改) 18 NSTimer *timer; 19 int timeCountG;//记录当前游戏时间 20 int color_offset;//颜色偏差 21} 22 23@end 24 25@implementation ViewController 26 27- (void)viewDidLoad { 28 [super viewDidLoad]; 29 color_offset = 20; 30 GameTime = 10; 31 32 // Do any additional setup after loading the view, typically from a nib. 33} 34 35-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 36{ 37 return 60; 38} 39 40-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 41{ 42 UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 43 //色块颜色设置 44 if (indexPath.row == differentCell) { 45 cell.backgroundColor = [UIColor colorWithRed:redS/255.0 green:greenS/255.0 blue:blueS/255.0 alpha:1]; 46 } 47 else 48 cell.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1]; 49 50 return cell; 51} 52 53#pragma mark cell点击事件 54-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 55{ 56 if (indexPath.row == differentCell) { 57 NSInteger a = [_level.text integerValue]; 58 _level.text = [NSString stringWithFormat:@"%ld", ++a]; 59 [self reloadCollection]; 60 } 61} 62 63 64 65- (void)didReceiveMemoryWarning { 66 [super didReceiveMemoryWarning]; 67 // Dispose of any resources that can be recreated. 68} 69//点击开始按钮,游戏初始化 70- (IBAction)play:(UIButton *)sender { 71 timeCountG = GameTime; 72 _timeCount.text = [NSString stringWithFormat:@"%d", timeCountG]; 73 _level.text = @"1"; 74 _button.hidden = YES; 75 timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; 76 [self reloadCollection];//刷新collectionView; 77} 78//游戏开始后 79-(void)runTimer 80{ 81 timeCountG--; 82 _timeCount.text = [NSString stringWithFormat:@"%d", timeCountG]; 83 if (timeCountG == 0) { 84 [timer invalidate]; 85 timer = nil; 86 _button.hidden = NO; 87 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Game Over" message:[NSString stringWithFormat:@"恭喜闯到%@关", _level.text] preferredStyle:UIAlertControllerStyleAlert]; 88 [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){ 89 //游戏数据清空 90 differentCell = -1; 91 red = 0; 92 green = 0; 93 blue = 0; 94 redS = 0; 95 greenS = 0; 96 blueS = 0; 97 [_collectionView reloadData]; 98 }]]; 99 [self presentViewController:alert animated:YES completion:nil]; 100 } 101} 102 103//加载新的一关,最后刷新reloadData 104-(void)reloadCollection 105{ 106 //正常色块的颜色 107 red = arc4random() % 256; 108 green = arc4random() % 256; 109 blue = arc4random() % 256; 110 //特殊色块的颜色 111 int change = arc4random() % 3; 112 switch (change) { 113 case 0://只改变红色 114 if (red + color_offset > 255) { 115 redS = red - color_offset; 116 } 117 else 118 redS = red + color_offset; 119 greenS = green; 120 blueS = blue; 121 break; 122 case 1://只改变绿色 123 redS = red; 124 if (green + color_offset > 255) { 125 greenS = green - color_offset; 126 } 127 else 128 greenS = green + color_offset; 129 blueS = blue; 130 break; 131 case 2://只改变蓝色 132 redS = red; 133 greenS = green; 134 if (blue + color_offset > 255) { 135 blueS = blue - color_offset; 136 } 137 else 138 blueS = blue + color_offset; 139 break; 140 141 default: 142 break; 143 } 144 //特殊色块的位置? 145 differentCell = arc4random() % 60; 146 [_collectionView reloadData]; 147 148 149 150 151 152 153 154 155} 156@end
UICollection 找不同
Wesley13
2021-10-11
1265 0 0
点赞
收藏
评论区
加载中...