IOS开发—找色块游戏

@interface NextViewController ()

{

int r ;

int m;

int nn;

UIView *view;

NSArray *color;

NSMutableArray *arry;

UIImageView *baview;

UILabel *textfiled;

}

@end

@implementation NextViewController

- (void)viewDidLoad {

//    赋初值 建立最开始的模型

[super viewDidLoad];

self.title = @"找色块";

r = 2;

m = 1;

self.view.backgroundColor = [UIColor whiteColor];

[self color];

[self loadData];

[self textfiled];

}

//颜色库

-(void)color{

color = @[[UIColor colorWithRed:0.494 green:0.905 blue:0.493 alpha:1.000],[UIColor greenColor],[UIColor colorWithRed:0.000 green:0.091 blue:0.998 alpha:1.000],[UIColor cyanColor],[UIColor yellowColor],[UIColor purpleColor],[UIColor lightGrayColor],[UIColor darkGrayColor],[UIColor blackColor],[UIColor colorWithRed:0.868 green:0.336 blue:0.760 alpha:1.000],[UIColor magentaColor],[UIColor orangeColor],[UIColor brownColor]];}

//--------------------创建色块-------------------

-(void)loadData{

int index = arc4random()%(r*r)+1;

int ii = arc4random()%color.count;

baview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 120, CGRectGetWidth(self.view.frame), CGRectGetWidth(self.view.frame))];

baview.backgroundColor = [UIColor whiteColor];

//    使用UIImageView必须要使用下面语句可以使用手势

baview.userInteractionEnabled = YES;

[self.view addSubview: baview];

for (int n =0; n<r; n++)

{

for (int i = 0; i<r; i++)

{

//            算出一个矩形的宽

//            CGFloat k = (CGRectGetWidth(self.view.frame)-40-(r-1)*5)/r;

CGFloat k = CGRectGetWidth(self.view.frame);

//            view = [[UIView alloc]initWithFrame:CGRectMake(20+(k+5)*n, 20+(k+5)*i, k-2, k-2)];

view = [[UIView alloc]initWithFrame:CGRectMake(20+(k-40)/r*i, 20+(k-40)/r*n, (k-60)/r, (k-60)/r)];

view.layer.cornerRadius = 10;

view.layer.masksToBounds = YES;

view.backgroundColor = color[ii];

view.tag = r*i+n+1;

//任意选择一个模块添加手势

if (index == view.tag)

{

UITapGestureRecognizer *tapge = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tanp:)];

tapge.numberOfTapsRequired = 1;

view.alpha = 0.5;

//    添加手势

[view addGestureRecognizer:tapge];

}

[baview addSubview:view];

}

}

}

// ----------------随机取出一个色块添加手势--------------------

-(void)tanp :(UITapGestureRecognizer*)sender{

r++;

m++;

[self loadData];

[self text];//最下面文字显示

[self textfiled];//最上端文字显示

}

//文字显示模块

-(void)text{

if (r>5&&r<7) {

UILabel *textfile = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];

textfile.tag = 100;

textfile.textAlignment = YES;

textfile.text = @"你的视力很好哦!";

[self.view addSubview:textfile];

}

if (r>7&&r<11) {

UILabel *textfile = (UILabel*)[self.view viewWithTag:100];

textfile.hidden = YES;

}

if (r>20&&r<23) {

UILabel *textfile = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];

textfile.tag = 100;

textfile.textAlignment = YES;

textfile.text = @"你真牛!!!";

[self.view addSubview:textfile];

}

if (r>35&&r<40) {

UILabel *textfile = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];

textfile.tag = 100;

textfile.textAlignment = YES;

textfile.text = @"大神,顶礼膜拜!!!!!!";

[self.view addSubview:textfile];

}

if (r>40) {

UILabel *textfile = (UILabel*)[self.view viewWithTag:100];

textfile.hidden = YES;

}

}

//数字块

-(void)textfiled{

textfiled = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2-250, 150, 50)];

textfiled.backgroundColor = [UIColor whiteColor];

textfiled.tag = m+500;

arry = [NSMutableArray array];

[arry addObject:@(textfiled.tag)];

textfiled.textAlignment = YES;

NSString *te = [NSString stringWithFormat:@"现在是第%d层",r-1];

textfiled.text = te;

textfiled.textColor = [UIColor blackColor];

[self.view addSubview:textfiled];

//    if (textfiled.tag+1 == m+300) {

//        textfiled.hidden = YES;

//    }

[self remove];

}

//移除出现过的视图,以免发生重叠

-(void)remove{

UITextField *textf = (UITextField *)[self.view viewWithTag:textfiled.tag - 1 ];

[textf removeFromSuperview];

//    textf.hidden = YES;

}

//

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

时间: 2024-08-02 06:52:31

IOS开发—找色块游戏的相关文章

17、Cocos2dx 3.0游戏开发找小三之内置的常用层:三剑客LayerColor、LayerGradient、Menu

重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30477587 为了方便游戏开发者,Cocos2d-x 内置了 3 种特殊的 Layer: 具体如下所示: LayerColor:一个单纯的实心色块. LayerGradient:一个色块,但可以设置两种颜色的渐变效果. Menu:十分常用的游戏菜单. LayerColor 与 与 LayerGradient 这两个层十分简单,都仅仅包含一个色块. 不同

7、Cocos2dx 3.0游戏开发找小三之3.0版本的代码风格

重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27691337 Cocos2d-x代码风格 前面我们已经多次提到 Cocos2d-x 源自于 Cocos2d-iPhone.Cocos2d-iPhone 是一个十分出色的游戏引擎,许多优秀的 iOS平面游戏都基于 Cocos2d-iPhone 开发,而它的实现语言是 Objective-C.因此,Cocos2d-x 也就沿袭了 Objective-C 的

8、Cocos2dx 3.0游戏开发找小三之3.0版本的内存管理

重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27693365 复杂的内存管理 移动设备上的硬件资源十分有限,内存尤为宝贵,开发者必须十分慎重地利用内存,避免不必要的消耗,更要防止内存泄漏. 基于 Cocos2d-iPhone 的 Objective-C风格的内存管理是 Cocos2d-x 的一个特色. 把 Objective-C 的内存管理方式引入 C++,使得游戏开发的内存管理难度下降了个层次.

4、Cocos2dx 3.0游戏开发找小三之Hello World 分析

尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27186557 Hello World 分析 打开新建的"findmistress"项目,可以看到项目文件是由多个代码文件及文件夹组成的,其中 Hello World 的代码文件直接存放于该项目文件夹中.下面我们来详细介绍一下项目的文件组成. 1."resource" 该文件夹主要用于存放游戏中需要的图片.音频和配置等资源

1、Cocos2dx 3.0游戏开发找小三之前言篇

尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27094663 前言 Cocos2d-x 是一个通用平面游戏引擎,基于一个同样十分著名的游戏引擎 Cocos2d-iPhone 设计. 它继承了 Cocos2d 系列引擎一贯的特点:使用简单,运行高效.灵活,且功能强大. 与 Cocos2d-iPhone 不同的是,Cocos2d-x 还拥有强大的跨平台能力,只需要编写一次代码, 就可以无缝地部署在包括

2、Cocos2dx 3.0游戏开发找小三之引擎简介

尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27094663 引擎简介 Cocos2d-x 的原型是 Cocos2d,一个最早来源于几位 Python 开发者在 PyWeek 竞赛中的作品, 目的是封装底层绘图代码,简 化 2D 游戏的开发过程,避免每次都"重新发明轮子". 有了 Cocos2d,开发者就可以把全部精力集中在游戏开发上,而不必关心绘图的细节. 这个 Python 版本的引

14、Cocos2dx 3.0游戏开发找小三之Scene and Layer:一场游戏一场梦

重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30474393 Scene :场景 了解了Director 之后,接下来介绍 Scene 这个与它紧密相关的游戏组件. 通过之前的学习,我们已经了解了场景以及它在流程控制中的地位. 在 Cocos2d-x 中,Scene 定义了一个场景.场景只是层的容器,包含了所有需要显示的游戏元素. 因此相对于其他游戏元素,Scene 并没有提供什么特别的功能,就是一

15、Cocos2dx 3.0游戏开发找小三之Sprite:每个精灵都是上辈子折翼的天使

重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30475395 Sprite Sprite 可以说是游戏中最重要的组成元素: 它描述了游戏中的精灵,是 Node 的一个最重要也最灵活的子类. Sprite 很重要,它代表了游戏中一个最小的可见单位, 同时Sprite 也很灵活,它装载了一个平面纹理,具有丰富的表现力,而且 可以通过多种方式加载. 如果说 Scene 和 Layer 代表了宏观的游戏元素

16、Cocos2dx 3.0游戏开发找小三之Node:父节点、子节点、傻傻分不清楚

重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30476133 Cocos2d-x 采用了场景.层.精灵的层次结构来组织游戏元素, 与此同时,这个层次结构还对应了游戏的渲染层次,因此游戏元素可以组织成树形结构,称作渲染树. Cocos2d-x 把渲染树上的每一个游戏元素抽象为一个节点,即 Node. 一切游戏元素都继承自 Node,因此它们 都具有 Node 所提供的特性. Node 定义了一个可绘制