该app为应用的功能为一个简单的考反应游戏

纲要:
-UIButton, UILabel, UIImageView 的运用;
-利用rendom增加游戏可玩性;

游戏说明:

在按下开始游戏后,分为三盏的指示灯按照“黄”、“红”、“绿”的先后顺序各自读取相应的指示灯颜色图片文件,当黄灯亮后游戏使用随机变量产生没有规律的红灯持续时间,因为这样的红灯使玩家没法预计绿灯什么时候亮,所以按下油门的时间要看玩家的反应速度,油门按下后,游戏会把从绿灯亮起直到玩家按下油门之间所使用的时间显示新窗口,并且提示玩家时候挑战更好的成绩。

现版本 SDK 8.4 Xcode

运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 ReactionTime

(1)  在xCode打开 ViewController.h 文件,加入下面代码

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{

NSDate *startDate;

//IBOutlet UIImageView: 把信号灯转换的图片对象告诉Interface Builder

IBOutlet UIImageView *stopLight;

}

@property (nonatomic,copy)NSDate *startDate;

//油门按钮对象 点击事件

-(IBAction)gasPedalPressed:(id)sender;

@end

(2)  在xCode打开 ViewController.m 文件,加入下面代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize startDate;

int greenLightOn = 0;

-(void)awakeFromNib

{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Reaction Time: Ready to Play" message:@"当绿灯亮时以最快的速度按下脚踏板." delegate:self cancelButtonTitle:@"游戏开始" otherButtonTitles:nil];

[alert show];

}

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

{

stopLight.image = [UIImage imageNamed:@"yellowLightSmall.png"];//指示灯的图片定制为黄灯图片

greenLightOn = 0;//数值为0,绿灯关闭

[NSTimer scheduledTimerWithTimeInterval:(3.0) target:self selector:@selector(onYellowLightTimer) userInfo:nil repeats:NO];

}

- (void)onYellowLightTimer //游戏中红灯的时间长度定制随机数(random)运用

{

stopLight.image = [UIImage imageNamed:@"redLightSmall.png"];

int delay = ((int) (random() % 7) + 1);

[NSTimer scheduledTimerWithTimeInterval:(3.0 + delay) target:self selector:@selector(onRedLightTimer) userInfo:nil repeats:NO];

}

- (void)onRedLightTimer //游戏中绿灯的时间长度定制

{

stopLight.image = [UIImage imageNamed:@"greenLightSmall.png"];

// stopLight.image: 指示灯的图片定制为绿灯图片

greenLightOn = 1;//绿灯打开

self.startDate = [NSDate date]; //反应时间导入,开始计算时间

}

- (IBAction)gasPedalPressed:(id)sender

{

double noSeconds = (double) [self.startDate timeIntervalSinceNow] * -1000;

NSString *reactionTime= [[NSString alloc] initWithFormat:@"好样的! 你的响应速度是 %1.0f 毫秒. 再来一次,创造更好的成绩...", noSeconds];

if(greenLightOn == 0)

reactionTime = @"请不要急. 要等到绿灯亮时才按脚踏板, 再来一次";

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reaction Time" message:reactionTime

delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

[alert show];

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

(3) 导入下面图片文件

将下面的图片导入到项目文件夹Supporting Files中(此为原博客中图片)

     

图片名称分别为greenLightSmall.png,redLightSmall.png,yellowLightSmall.png,road.png,gasPedalSmall.png

(4)UIView 界面设置

切换到main.storyboard

加入 Button

选择: Object Library 中拖拉一个 Button 到 View Controller Scene

鼠标右击Button控件,鼠标移动到"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "gasPedalPressed"; 选上它。

属性设置切换到Attributes上在 Type 下选择 custom; 在 Background 下选择 gasPedalSmall.png

选中控件 点击上方菜单栏Editor->Size to Fit Content

加入 UIimageView , 交通灯图片

选择: Tools -> Library ;从Library显示菜单中拖拉一个 imageView 到 View Controller Scene
在主视窗口或文件窗口;点击 imageView
选择: Tools ->  Inspector; 在image下选择 redLightSmall.png

选择: Tools -> Connection Inspector
移动鼠标在"New Referencing Outlet" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "stopLight"; 选上它。

加入 UIimageView , 背景图案

选择: Tools -> Library ;从Library显示菜单中拖拉一个 imageView 到 Main View; 调整到满屏
在主视窗口或文件窗口;点击 imageView
选择: Tools ->  Inspector; 在image下选择 road.png
选择: 选中控件 点击上方菜单栏Editor->Arrange->Send To Back;  图片设为背景

选择: File -> Save

最后在 xCode 选择 Build and then Running

(5)模拟器效果图

  

本文源于网上博客,经过本人修改和测试。原blog地址 http://blog.sina.com.cn/s/blog_5fae23350100dyg4.html

七、考反映小游戏《苹果iOS实例编程入门教程》的更多相关文章

  1. 六、雪花《苹果iOS实例编程入门教程》

    该app为应用的功能为制作一场雪景 现版本 SDK 8.4 Xcode 纲要:- UIImageView 的运用- onTimer 代码运用- onAnimation 代码运用 运行Xcode 选择 ...

  2. 五、点数器《苹果iOS实例编程入门教程》

    该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...

  3. 四、卫星定位《苹果iOS实例编程入门教程》

    该app为应用的功能为用iPhone 显示你现在的位置 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...

  4. 三、图像移动《苹果iOS实例编程入门教程》

    该app为应用的功能为动态移动动画 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applicati ...

  5. 二 、打开地图《苹果iOS实例编程入门教程》

    该app为应用的功能为给你的iPhone打开google地图有效地址连接 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Si ...

  6. 一、午夜倒数《苹果iOS实例编程入门教程》

    该app为应用的功能为计算离午夜12:00点的剩余时间 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...

  7. 【C语言C++编程学习笔记】基础语法,第一个简单的实例编程入门教程!

    C语言/C++编程学习:一个简单的实例 让我们来看一个简单的C语言程序.从下面的程序可以看出编写C语言程序的一些基本特征.   如果你能知道该程序将会在显示器上显示一些内容,那说明你还是知道一些的! ...

  8. iOS开发 ReactiveCocoa入门教程 第二部分

    ReactiveCocoa 是一个框架,它允许你在你的iOS程序中使用函数响应式(FRP)技术.加上第一部分的讲解,你将会学会如何使用信号量(对事件发出数据流)如何替代标准的动作和事件处理逻辑.你也会 ...

  9. TeeChart .NET for iOS图表开发入门教程

    去年,TeeChart 为iOS图表开发专门发布了TeeChart NET for iOS(包含在TeeChart Mobile中),相信很多人都对其感兴趣.慧都为大家制作了TeeChart NET ...

随机推荐

  1. hdu 4288 线段树 暴力 **

    题意: 维护一个有序数列{An},有三种操作: 1.添加一个元素. 2.删除一个元素. 3.求数列中下标%5 = 3的值的和. 解题思路: 看的各种题解,今天终于弄懂了. 由于线段树中不支持添加.删除 ...

  2. 注解:【有连接表的】Hibernate单向N->N关联

    Person与Address关联:单向N->N,[有连接表的] #和单向1->N关联代码完全相同,控制关系的一端需要增加一个set类型的属性,被关联的持久化实例以集合形式存在. #N-&g ...

  3. 用DOM4J解析XML文件案例

    用DOM4J解析XML文件案例,由于DOM4J不像JAXP属于JAVASE里,所以如果要使用DOM4J,则必须额外引入jar包,如图:

  4. MPAndroidChart饼图属性及相关设置

    公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图: 布局文件: &l ...

  5. python重载四则运算符及输出格式设置

    数学运算 Python 提供的基本数据类型 int.float 可以做整数和浮点的四则运算以及乘方等运算. 但是,四则运算不局限于int和float,还可以是有理数.矩阵等. 要表示有理数,可以用一个 ...

  6. HTML5实践 -- 使用CSS3 Media Queries实现响应式设计

    CSS3 Media用法介绍:http://www.w3cplus.com/content/css3-media-queries 转载请注明原创地址:http://www.cnblogs.com/so ...

  7. rem单位在手机网站中的使用

    em单位是相对于父节点的font-size,会有一些组合的问题,而rem是相对于根节点(或者是html节点),也就是说你可以在html节点定义一个单独的字体大小,然后所有其他元素使用rem相对于这个字 ...

  8. delphi override、overload、reintroduce的区别-0613.txt

    http://blog.csdn.net/honglixx/article/details/3624934 1.override overload reintroduce的中文叫法是什么? overr ...

  9. MFC 定义和调用全局变量的一种方法

    在CTestApp.h中声明一个int x;然后调用的方式如下: CTestApp *app = (CTestApp *)AfxGetApp(); //生成指向应用程序类的指针,Test处改为对应的项 ...

  10. 错误修改/etc/fstab,导致系统无法开机

    enter password or type control-D to continue 系统提示你输入root密码,而输入以后系统的所有文件是只读的,你无法修改 看下你的/etc/fstab这个目录 ...