该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>
 
@interfaceUIViewController
{
NSDate *startDate;   
//IBOutlet UIImageView: 把信号灯转换的图片对象告诉Interface Builder 
IBOutletUIImageView *stopLight;
}
@propertynonatomic,copy)NSDate *startDate; 
//油门按钮对象 点击事件
-(IBAction)gasPedalPressed:(id)sender; 
@end
(2)  在xCode打开 ViewController.m 文件,加入下面代码  
#import "ViewController.h"
@interfaceViewController () 
@end 
@implementation ViewController 
@synthesize startDate; 
int0; 
-(void)awakeFromNib
{
UIAlertViewUIAlertViewalloc]initWithTitle:@"Reaction Time: Ready to Play"message:@"当绿灯亮时以最快的速度按下脚踏板."delegate:selfcancelButtonTitle:@"游戏开始"otherButtonTitles:nil];    
show];
}
 
- (void)alertView:(UIAlertViewNSInteger)buttonIndex
{
stopLight.imageUIImageimageNamed:@"yellowLightSmall.png"];//指示灯的图片定制为黄灯图片
greenLightOn0;//数值为0,绿灯关闭  
NSTimerscheduledTimerWithTimeInterval:(3.0) target:selfselector:@selector(onYellowLightTimer) userInfo:nilrepeats:NO];
}
 
- (void)onYellowLightTimer //游戏中红灯的时间长度定制随机数(random)运用
{
stopLight.imageUIImageimageNamed:@"redLightSmall.png"];   
intint) (random() % 7) + 1);    
NSTimerscheduledTimerWithTimeInterval:(3.0target:selfselector:@selector(onRedLightTimer) userInfo:nilrepeats:NO];
}
 
- (void)onRedLightTimer //游戏中绿灯的时间长度定制
{
stopLight.imageUIImageimageNamed:@"greenLightSmall.png"];
// stopLight.image:指示灯的图片定制为绿灯图片    
greenLightOn1;//绿灯打开    
self.startDateNSDatedate]; //反应时间导入,开始计算时间
}
 
- (IBAction)gasPedalPressed:(id)sender
{
doubledouble) [self.startDatetimeIntervalSinceNow] * -1000;
    
NSStringNSStringalloc] initWithFormat:@"好样的! 你的响应速度是毫秒. 再来一次,创造更好的成绩...", noSeconds];
    
if(greenLightOn0)
@"请不要急. 要等到绿灯亮时才按脚踏板, 再来一次";
    
UIAlertViewUIAlertViewalloc] initWithTitle:@"Reaction Time"message:reactionTime
delegate:selfcancelButtonTitle:@"确定"otherButtonTitles: nil];
show];
} 
- (void)viewDidLoad {
superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} 
- (void)didReceiveMemoryWarning {
superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

(3) 导入下面图片文件 

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

 

ios自己做游戏 ios怎么做游戏_ios自己做游戏

ios自己做游戏 ios怎么做游戏_游戏_02

ios自己做游戏 ios怎么做游戏_xcode_03

ios自己做游戏 ios怎么做游戏_ios自己做游戏_04

     

ios自己做游戏 ios怎么做游戏_xcode_05

 图片名称分别为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)模拟器效果图

ios自己做游戏 ios怎么做游戏_控件_06

  

ios自己做游戏 ios怎么做游戏_游戏_07