UICollection 找不同

Wesley13
• 阅读 506
//关数类的游戏
//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
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
APP界面常用的五种颜色搭配
众所周知,每一种颜色带给用户的视觉感受也是不同的。现在人们对手机的依赖程度,就能看到手机中APP的发展前景,那今天就跟大家聊聊如何通过颜色搭配的不同来进行移动端APP界面的布局和排版设计。移动端UI界面中通常是需要选取主色,标准色,点晴色。它们各自代表的含义:  主色虽然是决定了画面风格的色彩但是往往不会被大面积的使用。通常在导航栏/部分按钮/i
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
暗箭伤人 暗箭伤人
7个月前
【www.ithunter.club】 20230922下午
不容易的2023年,我们一起努力【www.ithunter.club】(2023092208:00:00.8872062023092216:00:00.887206)1.人事招聘专员数名(可选远程或入职)2.招聘向坐标东京Yahoo、Shift、L
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这