首页 > 代码库 > iOS开发:纯代码实现汤姆猫小游戏

iOS开发:纯代码实现汤姆猫小游戏

演示效果如下:

技术分享

代码如下:

  1 //  2 //  CKViewController.m  3 //  纯代码实现汤姆猫  4 //  5 //  Created by FrankChen on 14-12-10.  6 //  Copyright (c) 2014年 diaozhatian. All rights reserved.  7 //  8   9 #import "CKViewController.h" 10  11 @interface CKViewController () 12 { 13     // 保存所有图片的个数的数据字典 14     NSDictionary *_imageDict; 15 } 16 // 汤姆猫view 17 @property (nonatomic, weak) UIImageView *tomcat; 18  19 @end 20  21 @implementation CKViewController 22  23  24 - (void)viewDidLoad 25 { 26     [super viewDidLoad]; 27      28     // 装载图片字典 29     NSBundle *bundle = [NSBundle mainBundle]; 30     NSString *path = [bundle pathForResource:@"tomcat" ofType:@"plist"]; 31     _imageDict = [NSDictionary dictionaryWithContentsOfFile:path]; 32  33     // 创建汤姆猫的主界面显示图片 34     UIImageView *mainImage = [[UIImageView alloc] init]; 35     [self.view addSubview:mainImage]; 36     self.tomcat = mainImage; 37     CGFloat viewW = self.view.frame.size.width; 38     CGFloat viewH = self.view.frame.size.height; 39     mainImage.frame = CGRectMake(0, 0, viewW, viewH); 40     mainImage.image = [UIImage imageNamed:@"angry_00.jpg"]; 41      42     // 创建头部按钮 43     UIButton *headBtn = [[UIButton alloc] init]; 44     [self.view addSubview:headBtn]; 45     headBtn.frame = CGRectMake(56, 72, 208, 178); 46     [headBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; 47     [headBtn setTitle:@"knockout" forState:UIControlStateNormal]; 48     [headBtn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 49     [headBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted]; 50      51     // 创建左脚部按钮 52     UIButton *leftFoot = [[UIButton alloc] init]; 53     [self.view addSubview:leftFoot]; 54     leftFoot.frame = CGRectMake(162, 430, 46, 30); 55     [leftFoot addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; 56     [leftFoot setTitle:@"footLeft" forState:UIControlStateNormal]; 57     [leftFoot setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 58     [leftFoot setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted]; 59      60     // 创建右脚部按钮 61     UIButton *rightFoot = [[UIButton alloc] init]; 62     [self.view addSubview:rightFoot]; 63     rightFoot.frame = CGRectMake(108, 430, 46, 30); 64     [rightFoot addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; 65     [rightFoot setTitle:@"footRight" forState:UIControlStateNormal]; 66     [rightFoot setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 67     [rightFoot setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted]; 68     // 创建肚子部按钮 69     UIButton *stomach = [[UIButton alloc] init]; 70     [self.view addSubview:stomach]; 71     stomach.frame = CGRectMake(106, 280, 104, 120); 72     [stomach addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; 73     [stomach setTitle:@"stomach" forState:UIControlStateNormal]; 74     [stomach setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 75     [stomach setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted]; 76     // 创建尾巴部按钮 77     UIButton *angry = [[UIButton alloc] init]; 78     [self.view addSubview:angry]; 79     angry.frame = CGRectMake(218, 390, 34, 50); 80     [angry addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; 81     [angry setTitle:@"angry" forState:UIControlStateNormal]; 82     [angry setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 83     [angry setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted]; 84      85     // 采用循环的方式创建6个动画按钮 86     CGFloat btnCount = 6; 87     for (int index = 0; index < btnCount; index++) { 88         NSArray *array = @[@"fart",@"eat",@"cymbal",@"pie",@"drink",@"scratch"]; 89         UIButton *btn = [[UIButton alloc] init]; 90         [self.view addSubview:btn]; 91         // 设置按钮的位置 92         int totalColumns = 2; // 总列数 93         // 1.图片的尺寸 94         CGFloat imageW = 60; 95         CGFloat imageH = 60; 96         // 3.3.设置frame 97         int row = index / totalColumns; 98         int col = index % totalColumns; 99         CGFloat marginX = viewW - 2 * imageW;100         CGFloat marginY = 10;101         // 计算x和y102         CGFloat imageX = col * (imageW + marginX);103         CGFloat imageY = 264 + row * (imageH + marginY);104         btn.frame = CGRectMake(imageX, imageY, imageW, imageH);105         // 给按钮设置title106         [btn setTitle:array[index] forState:UIControlStateNormal];107         // 让文字不可见108         [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];109         [btn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];110         // 设置背景图片111         UIImage *image = [UIImage imageNamed:array[index]];112         [btn setBackgroundImage:image forState:UIControlStateNormal];113         // 监听点击按钮事件114         [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];115     }116 }117 118 /**119  *  播放动画120  *121  *  @param count 图片数122  *  @param name  传入的图片前缀名123  */124 -(void)runAnimationWithCount:(int)count name:(NSString *)name{125     if (self.tomcat.isAnimating) return;126     // 1.加载所有的动画图片127     NSMutableArray *images = [NSMutableArray array];128     129     for (int i = 0; i < count; i++) {130         // 计算文件名131         NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg",name, i];132         // 加载图片133         NSBundle *bundle = [NSBundle mainBundle];134         NSString *path = [bundle pathForResource:filename ofType:nil];135         UIImage *image = [UIImage imageWithContentsOfFile:path];136         // 添加图片到数组中137         [images addObject:image];138     }139     self.tomcat.animationImages = images;140     // 2.设置播放次数141     self.tomcat.animationRepeatCount = 1;142     // 3.设置播放时间143     self.tomcat.animationDuration = images.count * 0.06;144     // 4.开始播放145     [self.tomcat startAnimating];146     // 5.动画播放完毕1秒后将图片从内存中清除147     CGFloat del = self.tomcat.animationDuration + 0.5;148     [self.tomcat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:del];149 }150 /**151  *  监听所有按钮点击事件152  */153 - (void)clickBtn:(UIButton *)sender{154     // 取出按钮文字155     NSString *title = [sender titleForState:UIControlStateNormal];156     // 获得图片数量157     int count = [_imageDict[title] intValue];158     // 播放动画159     [self runAnimationWithCount:count name:title];160 }161 162 @end

 

iOS开发:纯代码实现汤姆猫小游戏