UICollection 找不同

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
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

mysql设置时区

mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0

Android蓝牙连接汽车OBD设备

//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");

UICollection 找不同 - HelloWorld