Cat Learn iOS (15) UI ever hot brick games

Source: Internet
Author: User

Cat Share, must boutique

Material code Address: http://blog.csdn.net/u013357243/article/details/44814523
Original address: Http://blog.csdn.net/u013357243?viewmode=contents

! I wrote all the code inside the material code, the comments are complete, easy to learn

Look first.

Code
//ps: New iOS Exchange Learning Group: 304570962Can add the cat cat QQ:1764541256Or Znycat Let's study hard together. Original: http://blog.csdn.net/u013357243?viewmode=contents//VIEWCONTROLLER.M//Hit Bricks////Created by Liufan on 13-8-17.//Copyright (c) 2013 itcast. All rights reserved.// @interface viewcontroller : uiviewcontroller //Brick image array@property(Strong,nonatomic) Iboutletcollection (Uiimageview)Nsarray*blockimages;//small ball image View@property(Weak,nonatomic)Iboutlet Uiimageview*ballimageview;//Bezel image View@property(Weak,nonatomic)Iboutlet Uiimageview*paddleimageview;//Message Labels@property(Weak,nonatomic)Iboutlet UILabel*messagelabel;//TAP screen gesture recognition@property(Strong,nonatomic)IboutletUITapGestureRecognizer *tapgesure;//Tap the screen to start the game- (ibaction) Tapscreen: (ID) sender;//Pull the bezel- (ibaction) Dragpaddle: (Uipangesturerecognizer *) sender;@end#import <QuartzCore/QuartzCore.h>  @interface viewcontroller (){//The initial position of the ball    Cgpoint_originballcenter;//The initial position of the bezel    Cgpoint_originpaddlecenter;//Game clockCadisplaylink *_gametimer;//The speed of the ball    Cgpoint_ballvelocity;//The horizontal speed of the bezel    CGFloat_paddlevelocityx;}@end @implementation viewcontroller - (void) viewdidload{[SuperViewdidload];additional setup after loading the view, typically from a nib.    //Record Ball Initial center point position_originballcenter = _ballimageview. Center;//Record Bezel Initial center point position_originpaddlecenter = _paddleimageview. Center;} - (void) didreceivememorywarning{[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}#pragma mark-Collision detection Method//Collision detection with the screen- (void) intersectwithscreen{//Collision detection at the top of the screen    if(Cgrectgetminy (_ballimageview. Frame) <=0) {_ballvelocity. Y= ABS (_ballvelocity. Y); }//Collision detection to the left of the screen    if(Cgrectgetminx (_ballimageview. Frame) <=0) {_ballvelocity. x= ABS (_ballvelocity. x); }//Collision detection with the right side of the screen    if(Cgrectgetmaxx (_ballimageview. Frame) >= Self. View. Bounds. Size. Width) {_ballvelocity. x=-abs (_ballvelocity. x); }//Fall out from the bottom of the screen, the game is over    if(Cgrectgetminy (_ballimageview. Frame) >= Self. View. Bounds. Size. Height) {NSLog(@"you lost!");//Turn off the clock[_gametimer invalidate];//Prompt the user to lose[_messagelabel Sethidden:NO]; [_messagelabel settext:@"you lost!"];//Enable tap-screen gesture recognition[_tapgesure setenabled:YES]; }}//Collision detection with Bricks- (void) intersectwithblocks{ for(Uiimageview*block in _blockimages) {//Cycle check whether the bricks collide with the ball, if a collision occurs, the speed of the ball is flipped        if(Cgrectintersectsrect (Block. Frame, _ballimageview. Frame) &&! [Block Ishidden]) {//Hide the bricks.[Block Sethidden:YES];//Flip ball y-direction speed_ballvelocity. Y*= -1; }    }//All the bricks are hidden, which means the game wins.    BOOLWin =YES; for(Uiimageview*block in _blockimages) {if(! [Block Ishidden]) {win =NO; Break; }    }//The game wins the deal    if(Win) {//Turn off the clock[_gametimer invalidate];//Prompt the user to lose[_messagelabel Sethidden:NO]; [_messagelabel settext:@"Oh yes"];//Enable tap-screen gesture recognition[_tapgesure setenabled:YES]; }}//Collision detection with bezel- (void) intersectwithpaddle{if(Cgrectintersectsrect (_paddleimageview. Frame, _ballimageview. Frame)) {//Ball y-direction speed flip_ballvelocity. Y=-abs (_ballvelocity. Y);//Increase the speed of the ball in the horizontal direction, simple correction of the ball's horizontal speed_ballvelocity. x+ = _paddlevelocityx/120.0; }}//The method to be executed when the screen refreshes- (void) step{NSLog(@"The screen is refreshed."); [ SelfIntersectwithscreen]; [ SelfIntersectwithblocks]; [ SelfIntersectwithpaddle];//update ball position[_ballimageview Setcenter:cgpointmake (_ballimageview. Center. x+ _ballvelocity. x, _ballimageview. Center. Y+ _ballvelocity. Y)];}//Tap the screen to start the game- (ibaction) Tapscreen: (ID) sender{NSLog(@"tap the screen!");//Disable tap-screen gesture recognition[_tapgesure setenabled:NO];//Message label hidden[_messagelabel Sethidden:YES];//small ball[_ballimageview Setcenter:_originballcenter];//Bezel[_paddleimageview Setcenter:_originpaddlecenter];//Bricks, restore the hidden bricks     for(Uiimageview*block in _blockimages) {[Block Sethidden:NO]; }//Set the initial speed for the ball_ballvelocity = Cgpointmake (0.0, -5.0);//define Game clock_gametimer = [Cadisplaylink displaylinkwithtarget: SelfSelector@selector(step)];//Add the game clock to the main running loop[_gametimer Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode];}//Pull the bezel- (ibaction) Dragpaddle: (Uipangesturerecognizer *) sender{//drag and drop your finger to change the position of the bezel    //need to determine if the finger is dragging    if(uigesturerecognizerstatechanged = = Sender. State) {//Remove the position where the finger is moved        CgpointLocation = [Sender Locationinview: Self. View];//Set the horizontal position of the bezel to the position of the finger[_paddleimageview Setcenter:cgpointmake (location. x, _paddleimageview. Center. Y)];//Record the horizontal movement speed of the bezel_paddlevelocityx = [Sender Velocityinview: Self. View]. x;NSLog(@"%f", _paddlevelocityx); }Else if(uigesturerecognizerstateended = = Sender. State) {//Recover finger movement speed_paddlevelocityx =0; }}@end
Game rules

1-"On the screen?" There are four rows of bricks.
2-"Tap the screen to start the game
3-"When the game starts, the ball goes up?"
4-"ball and brick impact can smash bricks and rebound
5-"ball with the top of the screen, the right side, the left side of the collision will rebound
6-"ball and baffle collision will rebound
7-"Moving around?" the finger can move the bezel.
8-"small ball from the bottom of the screen to fall out of the game end

Development steps

1-"New Item"
2-"Building an Interface"
Get the boundary? Face like and monitor screen events (Storyboard Setup interface, off-line)
3-"Code implementation
Tap the screen to make the ball move (game clock, refresh position)
Brick Collision Detection (hidden after collision)
Drag the bezel to change the ball direction (get the drag gesture speed, change the ball speed)
Winning and losing judgment and resetting the game (the minimum value of y in the position of the ball is greater than the screen y maximum)

PS: New iOS Exchange Learning Group: 304570962
You can add a cat qq:1764541256 or Znycat.
Let's study hard together.
Original: Http://blog.csdn.net/u013357243?viewmode=contents

Cat Learn iOS (15) UI ever hot brick games

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.