//关数类的游戏
//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
UICollection 找不同
点赞
收藏