//關數類的遊戲 //1.開始按鈕,按鈕移除 //2.遊戲的初始化(1.普通色塊的顏色。2.特殊色塊的位置。3.特殊色塊的顏色。) //3.collectView刷新。 //cell方法: 設置cell背景色 //indexPath.row == 特殊色塊的位置 //設置特殊顏色 //else ...設置普通顏色 //4.遊戲結束的時候(計時器==0,alert->代碼塊->遊戲調成無法玩的狀態(開始按鈕顯示)) #import "ViewController.h" @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> { CGFloat red,green,blue;//普通色塊 CGFloat redS,greenS,blueS;//特殊色塊 int differentCell;//記錄特殊色塊位置 int GameTime;//記錄遊戲總時間(方便修改) NSTimer *timer; int timeCountG;//記錄當前遊戲時間 int color_offset;//顏色誤差 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; color_offset = 20; GameTime = 10; // Do any additional setup after loading the view, typically from a nib. } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 60; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; //色塊顏色設置 if (indexPath.row == differentCell) { cell.backgroundColor = [UIColor colorWithRed:redS/255.0 green:greenS/255.0 blue:blueS/255.0 alpha:1]; } else cell.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1]; return cell; } #pragma mark cell點擊事件 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == differentCell) { NSInteger a = [_level.text integerValue]; _level.text = [NSString stringWithFormat:@"%ld", ++a]; [self reloadCollection]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //點擊開始按鈕,遊戲初始化 - (IBAction)play:(UIButton *)sender { timeCountG = GameTime; _timeCount.text = [NSString stringWithFormat:@"%d", timeCountG]; _level.text = @"1"; _button.hidden = YES; timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; [self reloadCollection];//刷新collectionView; } //遊戲開始後 -(void)runTimer { timeCountG--; _timeCount.text = [NSString stringWithFormat:@"%d", timeCountG]; if (timeCountG == 0) { [timer invalidate]; timer = nil; _button.hidden = NO; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Game Over" message:[NSString stringWithFormat:@"恭喜闖到%@關", _level.text] preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){ //遊戲數據清空 differentCell = -1; red = 0; green = 0; blue = 0; redS = 0; greenS = 0; blueS = 0; [_collectionView reloadData]; }]]; [self presentViewController:alert animated:YES completion:nil]; } } //加載新的一關,最後刷新reloadData -(void)reloadCollection { //正常色塊的顏色 red = arc4random() % 256; green = arc4random() % 256; blue = arc4random() % 256; //特殊色塊的顏色 int change = arc4random() % 3; switch (change) { case 0://只改變紅色 if (red + color_offset > 255) { redS = red - color_offset; } else redS = red + color_offset; greenS = green; blueS = blue; break; case 1://只改變綠色 redS = red; if (green + color_offset > 255) { greenS = green - color_offset; } else greenS = green + color_offset; blueS = blue; break; case 2://只改變藍色 redS = red; greenS = green; if (blue + color_offset > 255) { blueS = blue - color_offset; } else blueS = blue + color_offset; break; default: break; } //特殊色塊的位置? differentCell = arc4random() % 60; [_collectionView reloadData]; } @end