Cat and cat learning IOS (15th) UI-once hot brick-hitting games, ios bricks

Source: Internet
Author: User

Cat and cat learning IOS (15th) UI-once hot brick-hitting games, ios bricks

CAT/CAT sharing, must be excellent

Material address: http://blog.csdn.net/u013357243/article/details/44814523
Original address: http://blog.csdn.net/u013357243? Viewmode = contents

! The material code contains all the code I have written and has complete annotations for easy learning.

First look

Code
// Ps: new iOS communication learning group: 304570962 can be added to cat qq: 1764541256 or znycat. Let's study hard together. Http://blog.csdn.net/u013357243? Viewmode = contents // ViewController. m // strike 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; // ball Image view @ property (weak, nonatomic) IBOutlet UIImageView * ballImageView; // baffle Image view @ property (weak, nonatomic) IBOutlet UIImageView * padd LeImageView; // message tag @ property (weak, nonatomic) IBOutlet UILabel * messageLabel; // click the screen Gesture Recognition @ property (strong, nonatomic) IBOutlet UITapGestureRecognizer * tapGesure; // click the screen to start the game-(IBAction) tapScreen :( id) sender; // drag the bezel-(IBAction) dragPaddle :( UIPanGestureRecognizer *) sender; @ end # import <QuartzCore/QuartzCore. h> @ interface ViewController () {// initial position of the ball CGPoint _ originBallCenter; // initial position of the baffle CGPoint _ origi NPaddleCenter; // Game clock CADisplayLink * _ gameTimer; // speed of the ball CGPoint _ ballVelocity; // horizontal speed of the baffle CGFloat _ paddleVelocityX;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // record the initial center position of the ball _ originBallCenter = _ ballImageView. center; // record the initial center position of the baffle _ originPaddleCenter = _ paddleImageView. center;}-(void) didR EceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} # pragma mark-Collision Detection Method // Collision Detection with the screen-(void) intersectWithScreen {// Collision Detection with 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 (CGRectGet MaxX (_ ballImageView. frame)> = self. view. bounds. size. width) {_ ballVelocity. x =-ABS (_ ballVelocity. x);} // drop out from the bottom of the screen and end the game if (CGRectGetMinY (_ ballImageView. frame)> = self. view. bounds. size. height) {NSLog (@ "You lost! "); // Turn off the clock [_ gameTimer invalidate]; // The system prompts the user to lose [_ messageLabel setHidden: NO]; [_ messageLabel setText: @" ~~~ "]; // Enable click screen gesture recognition [_ tapGesure setEnabled: YES] ;}// Collision Detection with bricks-(void) intersectWithBlocks {for (UIImageView * block in _ blockImages) {// cyclically checks whether the bricks collide with the ball. if a collision occurs, the speed of the flipped ball if (CGRectIntersectsRect (block. frame, _ ballImageView. frame )&&! [Block isHidden]) {// hide the bricks. [block setHidden: YES]; // flip the ball to Y. Speed _ ballVelocity. y * =-1 ;}// all bricks are hidden, indicating that the game wins BOOL win = YES; for (UIImageView * block in _ blockImages) {if (! [Block isHidden]) {win = NO; break ;}// handle game victory if (win) {// disable the clock [_ gameTimer invalidate]; // prompt the user to lose [_ messageLabel setHidden: NO]; [_ messageLabel setText: @ "ouye ~~~ "]; // Enable click screen gesture recognition [_ tapGesure setEnabled: YES] ;}}// Collision Detection with bezel-(void) intersectWithPaddle {if (CGRectIntersectsRect (_ paddleImageView. frame, _ ballImageView. frame) {// The Y-Direction Speed of the ball to flip _ ballVelocity. y =-ABS (_ ballVelocity. y); // increase the horizontal speed of the ball and modify the horizontal speed of the ball. x + = _ paddleVelocityX/120.0;} // method to be executed when the screen is refreshed-(void) step {NSLog (@ "screen refreshed"); [self intersectWithScreen]; [self intersectWithBlocks]; [self in TersectWithPaddle]; // update the ball position [_ ballImageView setCenter: CGPointMake (_ ballImageView. center. x + _ ballVelocity. x, _ ballImageView. center. y + _ ballVelocity. y)];} // click the screen to start the game-(IBAction) tapScreen :( id) sender {NSLog (@ "click the screen! "); // Disable click screen gesture recognition [_ tapGesure setEnabled: NO]; // hide the message tag [_ messageLabel setHidden: YES]; // ball [_ ballImageView setCenter: _ originBallCenter]; // baffle [_ paddleImageView setCenter: _ originPaddleCenter]; // bricks restore hidden bricks for (UIImageView * block in _ blockImages) {[block setHidden: NO];} // set the initial speed for the ball _ ballVelocity = CGPointMake (0.0,-5.0); // define the Game clock _ gameTimer = [CADisplayLink displayLinkWithTarget: self selector: @ selector (step)]; // Add the Game clock to the main running loop [_ gameTimer addToRunLoop: [nsunloop mainRunLoop] forMode: NSDefaultRunLoopMode];} // drag the bezel-(IBAction) dragPaddle :( UIPanGestureRecognizer *) sender {// change the position of the baffle when dragging the finger // you need to determine whether the finger is dragging if (UIGestureRecognizerStateChanged = sender. state) {// the position where the finger is moved to CGPoint location = [sender locationInView: self. view]; // set the horizontal position of the baffle to the position of the finger [_ paddleImageView setCenter: CGPointMake (location. x, _ paddleImageView. center. y)]; // record the horizontal movement speed of the baffle _ paddleVelocityX = [sender velocityInView: self. view]. x; NSLog (@ "% f", _ paddleVelocityX);} else if (UIGestureRecognizerStateEnded = sender. state) {// restore finger movement speed _ paddleVelocityX = 0 ;}}@ end
Game Rules

1-four lines of bricks are displayed on the screen.
2-click the screen to start the game
3-at the beginning of the game, the primary ball went up to the primary node
4-The impact of small balls and bricks can break bricks and rebound
5-the collision between the small ball and the top, right, and left of the screen will rebound
6-the collision between the small ball and the baffle will rebound
7-move your finger around to move the baffle plate
8-"the game ends when the player drops the ball from the bottom of the screen.

Development procedure

1-new project category
2-build a community
Obtain the interface objects and listen to screen events (the storyboard setting interface is disconnected)
3-"code implementation
Click the screen to let the ghost ball move (Game clock, refresh location)
Brick collision detection (hidden after collision)
Drag the baffle plate to change the direction of the slider (get the drag gesture speed and change the ball speed)
Determine the outcome and reset the game (the minimum value of y in the position of the ball is greater than the maximum value of y on the screen)

Ps: Create an iOS communication learning group: 304570962
You can add a cat QQ: 1764541256 or znycat
Let's study hard together.
Http://blog.csdn.net/u013357243? Viewmode = contents

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.