Seven, the test reflects the small game "Apple iOS example programming introductory tutorial"

Source: Internet
Author: User
Tags uikit

The app is a simple test response game for the app's function

Outline:
-uibutton, UILabel, the use of Uiimageview;
-Use rendom to increase game playability;

Game Description:

After the start of the game, divided into three lights according to the "Yellow", "red", "green" in order to read the corresponding light color picture file, when the yellow light after the game using random variables to generate irregular red light duration, because such a red light so that the player can not predict when the green light, So the time to press the throttle to see the player's reaction speed, the throttle press, the game will be lit from the green until the player presses the throttle between the time used to display a new window, and prompts the player to challenge better results.

Current version of SDK 8.4 Xcode

Run Xcode Select Create a new Xcode project->single View application named Reactiontime

(1) Open the ViewController.h file in Xcode and add the following code

#import <UIKit/UIKit.h>

@interface viewcontroller: uiviewcontroller

{

nsdate *startdate;    

Iboutlet Uiimageview: Tell interface Builder the picture object that the signal is converted to

iboutlet uiimageview *stoplight;

}

@property (nonatomic,copy)nsdate *startdate;  

//Throttle button Object Click event

-(ibaction) gaspedalpressed: (ID) sender;  

@end

(2) Open the viewcontroller.m file in Xcode and add the following code

#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:@" Press the foot pedal at the fastest speed when the green light is on . " delegate:self cancelbuttontitle:@ " game start " Otherbuttontitles:nil];     

[Alert show];

}

-(void) Alertview: (uialertview *) Alertview diddismisswithbuttonindex: (nsinteger) Buttonindex

{

stoplight. Image = [UIImage imagenamed:@ "Yellowlightsmall.png"]; the picture of the indicator is customized for the yellow light picture

Greenlighton = 0; value is 0, Green is off

[nstimer scheduledtimerwithtimeinterval:(3.0) target:self Selector :@selector(onyellowlighttimer) userInfo:nil repeats:NO];

}

-(void) Onyellowlighttimer //The time of the red light in the game custom random number use

{

stoplight. Image = [UIImage imagenamed:@ "Redlightsmall.png"];    

    int Delay = ((int) (random ()% 7< Span class= "S1") + 1     

[nstimer scheduledtimerwithtimeinterval:(3.0 + delay) target: Self Selector:@selector(onredlighttimer) userInfo:nil repeats:NO ];

}

-(void) Onredlighttimer // Time-duration customization of the green light in the game

{

stoplight. Image = [UIImage imagenamed:@ "Greenlightsmall.png"];

///Stoplight.image: picture of LED is customized for green light picture     

Greenlighton = 1; Green Open

self. StartDate = [nsdate date]; // reaction time import, start calculation time

}

-(ibaction) gaspedalpressed: (ID) sender

{

double noseconds = (double) [self. StartDate Timeintervalsincenow] *-;

nsstring *reactiontime= [[nsstring alloc] initwithformat:@ " good ! your response speed is %1.0f milliseconds . One more time, to create better results ... ", noseconds];

if(greenlighton = = 0)

Reactiontime = @ " please don't worry ." do not press the pedal until the green light is on , again ";

uialertview *alert = [[uialertview alloc] initwithtitle:@ "Reaction time " message: Reactiontime

delegate:self cancelbuttontitle:@ " OK " otherbuttontitles: Nil];

[Alert show];

-(void) viewdidload {

[Super viewdidload];

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

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

//Dispose of any resources, can be recreated.

}

@end

(3) Import the following image file

Import the following picture into the project folder supporting files (this is the original blog image)

Picture names are greenlightsmall.png,redlightsmall.png,yellowlightsmall.png,road.png,gaspedalsmall.png, respectively

(4) UIView Interface Settings

Switch to Main.storyboard

Add Button

Select: Drag a Button to the View Controller Scene in the Object Library

Right-click the button control and move the mouse over the circle behind "Touch up Inside"; The circle becomes (+); Drag to a straight line to connect to the "view Controller";
Release the mouse Select button appears "gaspedalpressed"; Pick it up.

property settings switch to attributes on the select Custom under Type; Select Gaspedalsmall.png under the Background

Select Controls click the top menu bar editor->size to Fit Content

Join Uiimageview, traffic lights pictures

Select: Tools--Library; Drag a imageView from the Library display menu to the View Controller Scene
In the main viewport or File window; click ImageView
Select: Tools-Inspector; Select Redlightsmall.png under Image

Selection: Tools, Connection Inspector
Move the mouse over the circle behind "New referencing Outlet"; The circle becomes (+); Drag to a straight line to connect to the "view Controller";
Release the mouse Select button appears "Stoplight"; Pick it up.

Add Uiimageview, background pattern

Select: Tools--Library; Drag a imageView from the Library display menu to Main View; Adjust to full screen
In the main viewport or File window; click ImageView
Select: Tools-Inspector; Select Road.png under Image
Select: Select the control click on the top menu bar editor->arrange->send to back; Picture set as Background

Select: File--Save


Finally, in XCode, choose Build and then Running

(5) Simulator

This article originates from the online blog, after I revise and test. Original Blog Address http://blog.sina.com.cn/s/blog_5fae23350100dyg4.html

Seven, the test reflects the small game "Apple iOS example programming introductory tutorial"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.