Cat learning iOS (15) UI previous hot play bricks game

Source: Internet
Author: User

Cats to share. Must boutique

Material code Address: http://blog.csdn.net/u013357243/article/details/44814523
Original address:

Viewmode=contents ">http://blog.csdn.net/u013357243?viewmode=contents

The code contains all the code I've written, and I've got a full stare. Easy to learn

Look first.

Code
//ps:新建iOS交流学习群:304570962 能够加猫猫QQ:1764541256 或则znycat 让我们一起努力学习吧。 原文: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 with the left side 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. Game 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) {//cyclic detection of whether bricks collide with small balls, assuming collisions occur. Flip the speed of the ball 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. Description Game Win 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);//Add 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. To 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 execution 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 infer 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 upward?"
4-"ball and brick impact can smash bricks and bounce back
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
Click on the screen to make the ball move up (game clock. Refresh location)
Collision detection of bricks (hidden after collision)
Drag the bezel to change the ball direction (get the drag gesture speed, change the ball speed)
Winning and losing inference 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
Can add a cat qq:1764541256 or Znycat
Let's study hard together.


Original: Http://blog.csdn.net/u013357243?viewmode=contents

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Cat learning iOS (15) UI previous hot play bricks game

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.