Flappy Bird, flappybird

Source: Internet
Author: User

Flappy Bird, flappybird

Angry Birds is a classic game that has been on fire for a long time.

We can implement it without using third-party libraries. Here we just do some simple implementation.

No collision detection is added. Some functions have been completed. We click the screen and the birds will fly.

If you do not click the bird, it will fall

The technology used here is also very simple. It uses user interaction and transform animation.

# Import "BirdViewController. h "// initial speed const float MaxTime = 30; // downward direction of acceleration const float VG = 0.05; // initial speed const float MaxV = 2.5; // initialize the total journey const float AllLength = 692; typedef enum {GameStart, GamePlaying, GameOver} GameState; @ interface BirdViewController () {NSTimer * birdTimer; // start the game switch BOOL isStart; // game status GameState gameState; // bird UIImageView * birdImgView; UIImageView * birdClotherView; // overall scenario UIView * playLayer; // float maxJumpTime ;} @ end @ implementation BirdViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor orangeColor]; [self initBirdAndOther]; birdTimer = [nst?scheduledtimerwithtimeinterval: 0.008 target: self selector: @ selector (update) userInfo: nil repeats: YES]; -(void) update {// judge if (isStart = YES & gameState = GamePlaying) {[self updateBird] ;}}-(void) initBirdAndOther {playLayer = [[UIView alloc] initWithFrame: self. view. bounds]; [self. view addSubview: playLayer]; // Add gesture UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (screenTap)]; [self. view addGestureRecognizer: tapGesture]; birdClotherView = (UIImageView *) [[UIView alloc] initWithFrame: CGRectMake (60,300, 40, 30)]; [playLayer addSubview: birdClotherView] // initialize birdImgView = [[UIImageView alloc] init]; birdImgView. frame = CGRectMake (0, 0, 40, 28); birdImgView. image = [UIImage imageNamed: @ "bird"]; [birdClotherView addSubview: birdImgView]; UILabel * label = [[UILabel alloc] init]; label. frame = CGRectMake (20, 30,100, 30); label. text = @ "Angry Birds"; [self. view addSubview: label];}-(void) screenTap {// each time you click maxJumpTime = MaxTime; if (isStart = NO) {isStart = YES; gameState = GamePlaying; for (UIView * tmp in self. view. subviews) {[tmp removeFromSuperview];} [self initBirdAndOther];} CGAffineTransform transform = CGAffineTransformIdentity; birdImgView. transform = CGAffineTransformRotate (transform,-30 * M_PI/180);}-(void) updateBird {maxJumpTime --; CGRect rect = birdClotherView. frame; if (maxJumpTime> = 0) {rect. origin. y = rect. origin. y-(MaxV-(MaxTime-maxJumpTime) * VG);} else {// 30 degree rotation CGAffineTransform transform = CGAffineTransformIdentity; birdImgView. transform = CGAffineTransformRotate (transform, 30 * M_PI/180); rect. origin. y = rect. origin. y-(maxJumpTime * VG);} birdClotherView. frame = rect;} @ end


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.