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.