//
// ViewController.m
// 自动版别踩白块
//
// Created by 晚起的蚂蚁 on 2016/11/8.
// Copyright © 2016年 ViewContronller. All rights reserved.
//
//UILabel继承自UIView,UILabel具有UIView的属性和方法
#import "ViewController.h"
@interface ViewController ()
@property(retain)NSTimer* timer;//图滑效果
@property(retain)NSTimer* timer2;//进度
@property bool jishi;
@property (nonatomic)UILabel* JiShi;//计时栏
@property float time;//
@property (assign)float speed;
@property NSInteger a;
@property NSInteger b;
@property bool isStop;//是否结束
@property(assign)NSInteger count;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self Start];
_speed=1;
_jishi=true;
_timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeFrame)userInfo:nil repeats:YES];
_timer2=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeFunc)userInfo:nil repeats:YES];
}
//加速
-(void)timeFunc{
_speed+=0.01;
if (_jishi) {
_time+=0.01;
_JiShi.text=[NSString stringWithFormat:@"%f",_time];
}
}
//游戏结束a
-(void)gameOver{
_isStop=true;
_jishi=false;
[_timer invalidate];//停止
for (NSInteger i=1; i<=28; i++){
if(i==28){
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor grayColor];
[(UIButton*)[self.view viewWithTag:i] setTitle:@"new" forState:UIControlStateNormal];
}else if(i==22){
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor grayColor];
[(UIButton*)[self.view viewWithTag:i] setTitle:@"得分" forState:UIControlStateNormal];
}else if(i==23){
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor grayColor];
[(UIButton*)[self.view viewWithTag:i] setTitle:[NSString stringWithFormat:@"%ld",_count] forState:UIControlStateNormal];
}else
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor whiteColor];
}
for (NSInteger j=0; j<7; j++){
for (NSInteger i=0; i<4; i++) {
((UIButton*)[self.view viewWithTag:j*4+i+1]).frame=CGRectMake(i*_a/4+2.5, j*_b/6+20-_b/6+1, _a/4-1, _b/6-1);
}
}
}
//新游戏
-(void)newGame{
_isStop=false;
_time=0;
_jishi=true;
_speed=1;
_count=0;
_timer=[NSTimer scheduledTimerWithTimeInterval:_time target:self selector:@selector(changeFrame) userInfo:nil repeats:YES];
[self suiji];
[(UIButton*)[self.view viewWithTag:22] setTitle:@"" forState:UIControlStateNormal];
[(UIButton*)[self.view viewWithTag:23] setTitle:@"" forState:UIControlStateNormal];
[(UIButton*)[self.view viewWithTag:28] setTitle:@"" forState:UIControlStateNormal];
}
//随时间方法
-(void)changeFrame{
if (((UIButton*)[self.view viewWithTag:1]).frame.origin.y<=20) {
for (NSInteger j=0; j<7; j++){
for (NSInteger i=1; i<5; i++) {
CGFloat y=((UIButton*)[self.view viewWithTag:j*4+i]).frame.origin.y;
CGFloat x=((UIButton*)[self.view viewWithTag:j*4+i]).frame.origin.x;
((UIButton*)[self.view viewWithTag:j*4+i]).frame=CGRectMake(x,y+=_speed,_a/4-1, _b/6-1);
if (j>5&&y+_a/4-1>=_b+20) {
if ([[NSString stringWithFormat:@"%@",[UIColor blackColor]] isEqualToString:[NSString stringWithFormat:@"%@",((UIButton*)[self.view viewWithTag:j*4+i]).backgroundColor]]) {
[self gameOver];
}
}
}
}
}else{
for (NSInteger j=0; j<7; j++){
for (NSInteger i=0; i<4; i++) {
((UIButton*)[self.view viewWithTag:j*4+i+1]).frame=CGRectMake(i*_a/4+2.5, j*_b/6+20-_b/6+1, _a/4-1, _b/6-1);
}
}
[self paixu];
[self suiji];
}
}
//重新排序
-(void)paixu{
for (NSInteger i=7; i>0; i--)
for (NSInteger j=1; j<5; j++)
((UIButton*)[self.view viewWithTag:4*i+j]).backgroundColor=((UIButton*)[self.view viewWithTag:4*(i-1)+j]).backgroundColor;
}
//开始
-(void)Start{
self.view.backgroundColor=[UIColor brownColor];
CGFloat width=[UIScreen mainScreen].bounds.size.width;
CGFloat height=[UIScreen mainScreen].bounds.size.height;
//计时的label
_JiShi=[[UILabel alloc]initWithFrame:CGRectMake(width/2-40,20, 80, 20)];
_JiShi.textColor=[UIColor blueColor];
[self.view addSubview:_JiShi];
_a=width-5;
_b=height-30;
UIView* diancilan=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 20)];
diancilan.backgroundColor=[UIColor grayColor];
[self.view addSubview:diancilan];
UIView* wei=[[UIView alloc]initWithFrame:CGRectMake(0, height-10, width, 10)];
wei.backgroundColor=[UIColor blackColor];
[self.view addSubview:wei];
NSInteger tag=0;
for (NSInteger j=0; j<7; j++){
for (NSInteger i=0; i<4; i++) {
UIButton* button=[[UIButton alloc]initWithFrame:CGRectMake(i*_a/4+2.5, j*_b/6+20-_b/6+1, _a/4-1, _b/6-1)];
button.backgroundColor=[UIColor whiteColor];
button.tag = ++tag;
[button addTarget:self action:@selector(bitButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
[self.view bringSubviewToFront:_JiShi];
[self.view bringSubviewToFront:diancilan];
[self.view bringSubviewToFront:wei];
}
//点击事件
-(void)bitButton:(UIButton*)button{
if (!_isStop) {
button.showsTouchWhenHighlighted=YES;
if ([[NSString stringWithFormat:@"%@",[UIColor blackColor]] isEqualToString:[NSString stringWithFormat:@"%@",button.backgroundColor]]) {
button.backgroundColor=[UIColor grayColor];
_count++;
}else{
[self gameOver];
}
}else{
if (button.tag==28) {
[self newGame];
}
}
}
//随机事件
-(void)suiji{
for (NSInteger i=1; i<5; i++)
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor whiteColor];
UIButton* button;
NSInteger number;
//找出空白格并加入字符
while (1) {
number = arc4random()%4+1;
button=(UIButton*)[self.view viewWithTag:number];
if ([[NSString stringWithFormat:@"%@",[UIColor whiteColor]]isEqualToString:[NSString stringWithFormat:@"%@",button.backgroundColor]]) {
((UIButton*)[self.view viewWithTag:number]).backgroundColor=[UIColor blackColor];
break;
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
// ViewController.m
// 自动版别踩白块
//
// Created by 晚起的蚂蚁 on 2016/11/8.
// Copyright © 2016年 ViewContronller. All rights reserved.
//
//UILabel继承自UIView,UILabel具有UIView的属性和方法
#import "ViewController.h"
@interface ViewController ()
@property(retain)NSTimer* timer;//图滑效果
@property(retain)NSTimer* timer2;//进度
@property bool jishi;
@property (nonatomic)UILabel* JiShi;//计时栏
@property float time;//
@property (assign)float speed;
@property NSInteger a;
@property NSInteger b;
@property bool isStop;//是否结束
@property(assign)NSInteger count;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self Start];
_speed=1;
_jishi=true;
_timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeFrame)userInfo:nil repeats:YES];
_timer2=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeFunc)userInfo:nil repeats:YES];
}
//加速
-(void)timeFunc{
_speed+=0.01;
if (_jishi) {
_time+=0.01;
_JiShi.text=[NSString stringWithFormat:@"%f",_time];
}
}
//游戏结束a
-(void)gameOver{
_isStop=true;
_jishi=false;
[_timer invalidate];//停止
for (NSInteger i=1; i<=28; i++){
if(i==28){
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor grayColor];
[(UIButton*)[self.view viewWithTag:i] setTitle:@"new" forState:UIControlStateNormal];
}else if(i==22){
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor grayColor];
[(UIButton*)[self.view viewWithTag:i] setTitle:@"得分" forState:UIControlStateNormal];
}else if(i==23){
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor grayColor];
[(UIButton*)[self.view viewWithTag:i] setTitle:[NSString stringWithFormat:@"%ld",_count] forState:UIControlStateNormal];
}else
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor whiteColor];
}
for (NSInteger j=0; j<7; j++){
for (NSInteger i=0; i<4; i++) {
((UIButton*)[self.view viewWithTag:j*4+i+1]).frame=CGRectMake(i*_a/4+2.5, j*_b/6+20-_b/6+1, _a/4-1, _b/6-1);
}
}
}
//新游戏
-(void)newGame{
_isStop=false;
_time=0;
_jishi=true;
_speed=1;
_count=0;
_timer=[NSTimer scheduledTimerWithTimeInterval:_time target:self selector:@selector(changeFrame) userInfo:nil repeats:YES];
[self suiji];
[(UIButton*)[self.view viewWithTag:22] setTitle:@"" forState:UIControlStateNormal];
[(UIButton*)[self.view viewWithTag:23] setTitle:@"" forState:UIControlStateNormal];
[(UIButton*)[self.view viewWithTag:28] setTitle:@"" forState:UIControlStateNormal];
}
//随时间方法
-(void)changeFrame{
if (((UIButton*)[self.view viewWithTag:1]).frame.origin.y<=20) {
for (NSInteger j=0; j<7; j++){
for (NSInteger i=1; i<5; i++) {
CGFloat y=((UIButton*)[self.view viewWithTag:j*4+i]).frame.origin.y;
CGFloat x=((UIButton*)[self.view viewWithTag:j*4+i]).frame.origin.x;
((UIButton*)[self.view viewWithTag:j*4+i]).frame=CGRectMake(x,y+=_speed,_a/4-1, _b/6-1);
if (j>5&&y+_a/4-1>=_b+20) {
if ([[NSString stringWithFormat:@"%@",[UIColor blackColor]] isEqualToString:[NSString stringWithFormat:@"%@",((UIButton*)[self.view viewWithTag:j*4+i]).backgroundColor]]) {
[self gameOver];
}
}
}
}
}else{
for (NSInteger j=0; j<7; j++){
for (NSInteger i=0; i<4; i++) {
((UIButton*)[self.view viewWithTag:j*4+i+1]).frame=CGRectMake(i*_a/4+2.5, j*_b/6+20-_b/6+1, _a/4-1, _b/6-1);
}
}
[self paixu];
[self suiji];
}
}
//重新排序
-(void)paixu{
for (NSInteger i=7; i>0; i--)
for (NSInteger j=1; j<5; j++)
((UIButton*)[self.view viewWithTag:4*i+j]).backgroundColor=((UIButton*)[self.view viewWithTag:4*(i-1)+j]).backgroundColor;
}
//开始
-(void)Start{
self.view.backgroundColor=[UIColor brownColor];
CGFloat width=[UIScreen mainScreen].bounds.size.width;
CGFloat height=[UIScreen mainScreen].bounds.size.height;
//计时的label
_JiShi=[[UILabel alloc]initWithFrame:CGRectMake(width/2-40,20, 80, 20)];
_JiShi.textColor=[UIColor blueColor];
[self.view addSubview:_JiShi];
_a=width-5;
_b=height-30;
UIView* diancilan=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 20)];
diancilan.backgroundColor=[UIColor grayColor];
[self.view addSubview:diancilan];
UIView* wei=[[UIView alloc]initWithFrame:CGRectMake(0, height-10, width, 10)];
wei.backgroundColor=[UIColor blackColor];
[self.view addSubview:wei];
NSInteger tag=0;
for (NSInteger j=0; j<7; j++){
for (NSInteger i=0; i<4; i++) {
UIButton* button=[[UIButton alloc]initWithFrame:CGRectMake(i*_a/4+2.5, j*_b/6+20-_b/6+1, _a/4-1, _b/6-1)];
button.backgroundColor=[UIColor whiteColor];
button.tag = ++tag;
[button addTarget:self action:@selector(bitButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
[self.view bringSubviewToFront:_JiShi];
[self.view bringSubviewToFront:diancilan];
[self.view bringSubviewToFront:wei];
}
//点击事件
-(void)bitButton:(UIButton*)button{
if (!_isStop) {
button.showsTouchWhenHighlighted=YES;
if ([[NSString stringWithFormat:@"%@",[UIColor blackColor]] isEqualToString:[NSString stringWithFormat:@"%@",button.backgroundColor]]) {
button.backgroundColor=[UIColor grayColor];
_count++;
}else{
[self gameOver];
}
}else{
if (button.tag==28) {
[self newGame];
}
}
}
//随机事件
-(void)suiji{
for (NSInteger i=1; i<5; i++)
((UIButton*)[self.view viewWithTag:i]).backgroundColor=[UIColor whiteColor];
UIButton* button;
NSInteger number;
//找出空白格并加入字符
while (1) {
number = arc4random()%4+1;
button=(UIButton*)[self.view viewWithTag:number];
if ([[NSString stringWithFormat:@"%@",[UIColor whiteColor]]isEqualToString:[NSString stringWithFormat:@"%@",button.backgroundColor]]) {
((UIButton*)[self.view viewWithTag:number]).backgroundColor=[UIColor blackColor];
break;
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end