实例iPhone编程入门教程-第六天

考反映小游戏
今天大家一起来建立一个 iPhone app,给你的iPhone 制作一个简单的考反应游戏。



纲要:
-在程序显示前运行代码;
-UIButton, UILabel, UIImageView 的运用;
-利用rendom增加游戏可玩性;
-关于iPhone的“Utility Application”。

首先运行以安装好的 xCode

选择: File->New Project.

从 "New Project" 窗口
 




选择 : iPhone OS ->Applications-> Utility Application
命名 : 我这里命名为 “ReactionTime”

指示灯的反应程序编写

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


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

创建时间对象(NSDate)和信号灯图像对象(IBOutLet UIImageView)

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView {

 

  NSData *startDate;
      //NSData:单一点的时间数值类的对象

  IBOutlet UIImageView *stopLight;

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

//---------------------------------------

@property (nonatomic, copy) NSDate *startDate;

     // 日子选取器@property(nonatomic, copy) NSDate *date)

     // nonatomic:使用单线程机制减少系统资源使用
     // copy: 建立时间开始数值为文字类型(NSString)的对象 - (IBAction)gasPedalPressed;
     //油门按钮动作对象(IBAction);IBAction: 连接Interface Builder中把方法(method)作为行为(Action) 
@end 
 

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

#import "MainView.h"

@implementation MainView

@synthesize startDate; 

    // Synthesize指令为程序自动生成startDate设置函数(setters)获得函数(getter)

int greenLightOn = 0;

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

      //当Interface Builder的档案读取后,准备好所有的项目(Object)

      //它使游戏刚运行就可以读取显示消息框   

      //(UIAlertView)内定义好的游戏开始的信息和按钮。

      //UIAlertView *alert: 定义消息框内容

      //UIAlertView alloc : 配置消息框内容

      //initWithTitle : 消息框为“准备开始游戏”                      

      //message : 消息框内详细消息为“绿灯亮时以最快的速度按下脚踏板”      

      //delegate:self;代表自己

      //cancelButtonTitle: 取消消息框按钮为“戏开始

      //otherButtonTitles: nil; 没有其他按钮

      //[alert show]; 显示消息框

//------------------------------------------------------------------------------------------

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonInde x:(NSInteger)buttonIndex
        // 在点击消息框(UIAlertView)内的按钮,消息框将消失并运行此程序里的内容,
{
      stopLight.image = [UIImage imageNamed:@"yellowLightSmall.png"];// 指示灯的图片定制为黄灯图片
      greenLightOn = 0; //数值为0,绿灯关闭
     
      [NSTimer scheduledTimerWithTimeIn terval:(3.0) target:self selector:@selector(onYellowLightTimer) userInfo:nil repeats:NO];
}

     //stopLight.image:

     //NSTimer 黄灯的时间定制

     //scheduledTimerWithTimeInterval:(3.0) 预定的时间为3,时间间隔为0  

     //target:self  对象为自己

     //selector:@selector(onYellowLightTimer) 选择者为黄灯时间

     //userInfo:nil 没有用户信息

     //repeat:no 不重复运行

//---------------------------------------------------------------------------------------------

- (void)onYellowLightTimer // 游戏中红灯的时间长度定制随机数(random)运用
{
      stopLight.image = [UIImage imageNamed:@"redLightSmall.png"];
     
      int delay = ((int) (random() % 7) + 1);
     
      [NSTimer scheduledTimerWithTimeIn terval:(3.0 + delay) target:self selector:@selector(onRedLightTimer) userInfo:nil repeats:NO];
}
            // int delay = ((int) (random() % 7) + 1); 定制数字变量为随机数字除以7加1
       //
NSTimer

       //scheduledTimerWithTimeInterval:(3.0delay) 预定的时间为3,时间间隔为随机

       //target:self  对象为自己

       //selector:@selector(onRedLightTimer) 选择者为红灯时间

       //userInfo:nil 没有用户信息

       //repeat:no 不重复运行

//------------------------------------------------------------------------------------------------
- (void)onRedLightTimer // 游戏中绿灯的时间长度定制
{
        stopLight.image = [UIImage imageNamed:@"greenLightSmall.png"];
       //
stopLight.image: 指示灯的图片定制为绿灯图片
     
      greenLightOn = 1; // 绿灯打开
     
      self.startDate = [NSDate date]; // 反应时间导入,开始计算时间
}
//-------------------------------------------------------------------------------------------------
- (IBAction)gasPedalPressed
{
      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];     
}

    //double noSeconds : 定制带小数点的数值

     //self.startDate timeIntervalSinceNow : 时间开始数值为按下按钮后

     //*-1000 : 以毫秒显示

     //NSString *reactionTime : 定制文字字符窜值

     //[NSString alloc] :向文字字符窜值配置(alloc)文字

     //initWithFormat :初始化格式文字导入

     //if(greenLightOn == 0) : 如果在绿灯关闭的时候按下按钮

//---------------------------------------------------------------------------------
@end

(3) 导入下面图片文件
  下载下面图片,放入 ReactionTime 文件夹内并命名为下面名称
gasPedalSmall.png
 
greenLightSmall.png
 
yellowLightSmall.png
 
redLightSmall.png
 
road.png
 

在xCode下右键点击ReactionTime->Add->Existing Files; 在Reaction文件夹内,选择下载好的图片,
按 Add
 

(4) UIView 界面设置

双击文件: "main.xib" ;
然后 "Interface Builder"  会自动打开,在这里我们可以编辑改变界面

(5) 加入 Add Button

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Button 到 Main View
在主视窗口或文件窗口;点击 Button

选择: Tools -> Connection Inspector

移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "gasPedalPressed"; 选上它。
选择: Tools -> Attributes Inspector
在 Type 下选择 custom; 在 Background 下选择 gasPedalSmall.png
选择: Tools -> Size Inspector
调整 Size & Position

(6) 加入 UIimageView , 交通灯图片
选择: Tools -> Library ;从Library显示菜单中拖拉一个 imageView 到 Main View;
在主视窗口或文件窗口;点击 imageView
选择: Tools ->  Inspector; 在image下选择 road.png
 

选择: Tools -> Connection Inspector
移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "stopLight"; 选上它。
 

(7) 加入 UIimageView , 背景图案
选择: Tools -> Library ;从Library显示菜单中拖拉一个 imageView 到 Main View; 调整到满屏
在主视窗口或文件窗口;点击 imageView
选择: Tools ->  Inspector; 在image下选择 road.png
选择: Tools ->  Layout -> Send To Back;  图片设为背景

最后在 xCode 选择 Build->Build and Go; Save All.

 

 

下载今天程序文件: ReactionTime.zip


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值