ios-UI-汤姆猫德游戏实现

//

//  ViewController.m

//  UI-猜拳游戏

//

//  Created by jzq_mac on 15/7/15.

//  Copyright (c) 2015年 jzq_mac. All rights reserved.

//

#import
"ViewController.h"

#define  TIME
61

@interface
ViewController ()

{

UILabel *timeLable;

UILabel *scoreLable;

UILabel *observeLable;

UIImageView *imgeView;

UIImageView *imgeView1;

UIButton *button;

int score;

int time;

int number;

NSTimer *timer;

NSArray *imageList;

NSArray *buttonImageList;

NSArray *faceList;

BOOL Start;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

//   
设置背景图

UIImageView *imageView = [[UIImageView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];

imageView.image = [UIImage
imageNamed:@"钱.png"];

[self.view
addSubview:imageView];

imageList =
@[@"石头.png",@"剪刀.png",@"布.png"];

buttonImageList =
@[@"石头1.png",@"剪刀1.png",@"布1.png"];

faceList =
@[@"哭.png",@"笑.png",@"挣扎.png"];

//创建笑脸视图

imgeView1 = [[UIImageView
alloc]initWithFrame:CGRectMake(135,
320,
100, 100)];

imgeView1.image = [UIImage
imageNamed:@"美女1.png"];

[self.view
addSubview:imgeView1];

time = TIME;

//  
调用视图

[self
creatView];

[self
flashView];

[self
creatButon];

//   
定时器

timer = [NSTimer
scheduledTimerWithTimeInterval:1 target:self selector:@selector(flashView) userInfo:
nil repeats:YES];

[[NSRunLoop
currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

timer.fireDate = [NSDate
distantFuture];

timeLable.text = [NSString
stringWithFormat:@"时间: %d
秒",time];

}

//创建视图

- (void)creatView

{

scoreLable = [[UILabel
alloc]initWithFrame:CGRectMake(0,
20,
100, 50)];

[self.view
addSubview:scoreLable];

scoreLable.text = [NSString
stringWithFormat:@"得分: %d
分",score];

scoreLable.textColor = [UIColor
blueColor];

scoreLable.backgroundColor = [UIColor
grayColor];

scoreLable.textAlignment =
NSTextAlignmentCenter;

scoreLable.alpha =
0.4;

timeLable = [[UILabel
alloc]initWithFrame:CGRectMake(275,
20,
100, 50)];

[self.view
addSubview:timeLable];

timeLable.backgroundColor = [UIColor
grayColor];

timeLable.textColor = [UIColor
blueColor];

timeLable.textAlignment =
NSTextAlignmentCenter;

timeLable.text = [NSString
stringWithFormat:@"时间: %d
秒",time];

timeLable.alpha =
0.4;

observeLable = [[UILabel
alloc]initWithFrame:CGRectMake(0,
450,
CGRectGetHeight([UIScreen
mainScreen].bounds),
50)];

[self.view
addSubview:observeLable];

observeLable.backgroundColor = [UIColor
grayColor];

observeLable.alpha =
0.6;

//    observeLable.textAlignment = NSTextAlignmentCenter;

observeLable.font = [UIFont
boldSystemFontOfSize:30];

observeLable.text =
@"    ~~~人
机 猜
拳 大
战~~~ ";

}

//闪烁图片

- (void)flashView

{

time--;

timeLable.text = [NSString
stringWithFormat:@"时间: %d
秒",time];

for (int i =
0; i < 1; i++) {

imgeView = [[UIImageView
alloc]initWithFrame:CGRectMake(135 +
100*i,
80, 100,
100)];

imgeView.image = [UIImage
imageNamed:@"美女.png"];

[self.view
addSubview:imgeView ];

int r = arc4random()%imageList.count;

number  = r;

imgeView.image = [UIImage
imageNamed:imageList[r]];

}

if (time ==
0) {

timer.fireDate = [NSDate
distantFuture];

UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"大家一起猜" message:@"时间到"
delegate:self cancelButtonTitle:nil otherButtonTitles:@"不服就再来啊",
nil];

[alert show];

}

}

//创建button

- (void)creatButon

{

for (int i =
0; i < 3; i++) {

button = [[UIButton
alloc]initWithFrame:CGRectMake(35 +
100*i,
200, 100,
100)];

[self.view
addSubview:button];

[button
setBackgroundImage:[UIImage
imageNamed:buttonImageList[i]] forState:UIControlStateNormal];

button.backgroundColor = [UIColor
redColor];

button.layer.cornerRadius =
50;

button.layer.masksToBounds =
YES;

button.alpha =
0.6;

button.tag = i +
1;

[button
addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];

}

UIButton *startButton = [[UIButton
alloc]initWithFrame:CGRectMake(160,
550,
80, 80)];

[self.view
addSubview:startButton];

[startButton
setBackgroundImage:[UIImage
imageNamed:@"開始.png"] forState:UIControlStateNormal];

[startButton
setTitle:@"開始" forState:UIControlStateNormal];

[startButton
setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

[startButton
addTarget:self action:@selector(startGame:) forControlEvents:UIControlEventTouchUpInside];

}

//開始游戏

- (void)startGame:(UIButton *)sender

{

if (sender.selected !=YES) {

timer.fireDate = [NSDate
distantPast];

[sender
setTitle:@"暂停" forState:UIControlStateNormal];

sender.selected =
YES;

Start = YES;

}else

{

timer.fireDate = [NSDate
distantFuture];

[sender
setTitle:@"開始" forState:UIControlStateNormal];

sender.selected =
NO;

Start = NO;

}

}

//选择button的触发事件

- (void)selectButton:(UIButton *)sender

{

if (Start !=
NO) {

//        sender.showsTouchWhenHighlighted = YES;

if (sender.tag ==
1) {

if (number ==
0) {

imgeView1.image = [UIImage
imageNamed:faceList[2]];

}else
if(number ==
1){

imgeView1.image = [UIImage
imageNamed:faceList[1]];

score ++;

scoreLable.text = [NSString
stringWithFormat:@"总分: %d
分",score];

}else
if (number ==
2){

imgeView1.image = [UIImage
imageNamed:faceList[0]];

}

}

if (sender.tag ==
2) {

if (number ==
0) {

imgeView1.image = [UIImage
imageNamed:faceList[0]];

}else
if(number ==
1){

imgeView1.image = [UIImage
imageNamed:faceList[2]];

}else
if (number ==
2){

imgeView1.image = [UIImage
imageNamed:faceList[1]];

score ++;

scoreLable.text = [NSString
stringWithFormat:@"总分: %d
分",score];

}

}

if (sender.tag ==
3) {

if (number ==
0) {

imgeView1.image = [UIImage
imageNamed:faceList[1]];

score ++;

scoreLable.text = [NSString
stringWithFormat:@"总分: %d
分",score];

}else
if(number ==
1){

imgeView1.image = [UIImage
imageNamed:faceList[0]];

}else
if (number ==
2){

imgeView1.image = [UIImage
imageNamed:faceList[2]];

}

}

}

}

//弹窗事件

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

if (buttonIndex ==
0) {

score = 0;

time = TIME;

scoreLable.text = [NSString
stringWithFormat:@"总分: %d
分",score];

timeLable.text = [NSString
stringWithFormat:@"时间:%d
秒",time];

timer.fireDate = [NSDate
distantPast];

}else{

timer.fireDate = [NSDate
distantPast];

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

时间: 2024-10-05 14:22:48

ios-UI-汤姆猫德游戏实现的相关文章

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 //

iOS学习 - 汤姆猫

// // YJViewController.m // 04-汤姆猫的理解 // // Created by JACKY-MAC on 15-5-31. // Copyright (c) 2015年 www.train.com. All rights reserved. // #import "YJViewController.h" @interface YJViewController () @property (weak, nonatomic) IBOutlet UIImageVi

IOS 开发笔记-基础 UI(7)汤姆猫(UIImageView 的序列帧动画、图片加载,方法重构、Bundle 图片素材)

使用UIImageView.UIButton实现一个综合小案例---汤姆猫 回忆:UIImageView 来自UIView,UIView 来自UIResponder,UIButton 来自UIControl,UIControl 来自UIView 单独看实现,代码实现其实比较简单,但是创意很难得,美工要求很高! 是一个了不起的游戏! 功能分析 (1)点击对应的按钮后,让汤姆猫展现对应的动画 步骤分析 (1)搭建UI界面,同时也是准备素材 (2)监听按钮点击 (3)根据点击的按钮执行对应的动画 说明

汤姆猫游戏核心代码 ios/objective-c

#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *tomImageView; @end @implementation ViewController //敲汤姆猫的头部.前提:一系列动画已经导入. (IBAction)btnKnockOutonclicked:(UIButton *)sender{ [self tomImate

IOS 学习04 汤姆猫开发

IOS开发,一个简单的汤姆猫开发,截图如下: 1.1以前一直觉得这个小游戏不错,很好玩,现在学习IOS才发现其实这个程序真心很简单,其实这个还不是个完整,还没有声音,以后再来完整. 1.2 创建项目,导入图片,在项目images.xcassets中保存PNG图片,这里只保存6个按键图片,图片文件直接拉进就可以,如下图: 1.3 相信大多数人都玩过这个小游戏,其实这就是一个动画,而动画就是由多个图片连在一起播放的,最后看起来就是动画了. 1.4 知道了动画的原理,那就好办了,我们再来导入按键相对应

一个不会说话的汤姆猫游戏源码

实现“会说话的汤姆猫” App 互动效果,除了不能发出声音,动画效果与原App十分类似.基本原理是,根据选择的不同动作,播放对应的图像序列.支持图片缓存. 游戏项目源码下载:http://code.662p.com/view/7150.html 详细说明:http://ios.662p.com/thread-1874-1-2.html

[UI基础][不会说话的汤姆猫]

会说话的汤姆猫这个APP层级风靡一时,其UI部分就是利用了序列动画的技术, 接下来 我们用汤姆猫来演示怎么制作序列动画. [要求]: 1.学会使用序列动画的方法 2.学会分析动画播放中内存占用高的问题,并解决 [实现] 1.搭建UI界面 1.1.设置模拟器的屏幕尺寸3.7,添加素材 我们的素材文件是是按照3.7Inch的大小,设置屏幕大小为3.7Inch,添 加动画的素材到工程. 2.2.添加控件 2.2.1.添加背景图片 我们用ImageView控件来实现动画,首先添加一个UIViewImag

[iOS基础控件 - 3.4] 汤姆猫

需求: 1.点击按钮播放相应动画 2.点击汤姆猫身体部分(头.脚.尾巴)显示动画 3.当前正在播放动画不可触发其他动画 4.优化缓存.内存管理 A.序列帧动画 #1. png格式的文件可以不带扩展名访问,而且可以放到”Images.scassets”的组中预览,jpg只能放到support file中了,而且访问的时候要写上扩展名 #2. 在ViewController中的属性栏可以设置storyboard模型的大小 #3. 拖入文件夹的时候要选择“Create Groups"才能正确引用,不要

声音变调算法PitchShift(模拟汤姆猫) 附完整C++算法实现代码

上周看到一个变调算法,挺有意思的,原本计划尝试用来润色TTS合成效果的. 实测感觉还需要进一步改进,待有空再思考改进方案. 算法细节原文,移步链接: http://blogs.zynaptiq.com/bernsee/pitch-shifting-using-the-ft/ C++开源的项目,比较老的一个项目了. 源码下载地址: http://blogs.zynaptiq.com/bernsee/download/ 本人对这份算法源码进行简单的优化调整. 稍微提升了一点性能. 修改后的完整代码: