Tag:uiview game
3.0 initial speed needs 60 seconds reduced to 0const float MaxTime = 50;//acceleration, direction down const FLOAT VG = 0.05;//initial speed const FLOAT MAXV = 2.5;//Initialize total distance const float Alllength = 692;typedef enum {gamenostart, gameplaying, Gameover} gamestate; @interface Birdflyviewcontroller () {Nstimer *birdtimer; Start game switch BOOL Isstart; Game state gamestate GameState; Total Scene UIView *playlayer; Bird Uiimageview *birdimgview; Bird's shell UIView *birdclotherview; Jumping time float maxjumptime; Scrolling background UIView *bgview1; UIView *bgview2; Pipe UIView *PILEIMGVIEW11; UIView *pileimgview12; UIView *pileimgview21; UIView *pileimgview22; Score UILabel *scorelabel; Nsinteger score;} @end @implementation birdflyviewcontroller-(void) dealloc{[Birdtimer invalidate]; Playlayer = nil; PILEIMGVIEW11 = nil; PileImgView12 = nil; PileImgView21 = nil; PileImgView22 = nil;} -(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (nsbundle *) nibbundleornil{SELF = [Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(void) viewdidload{[Super Viewdidload]; [Self initbirdandother]; Birdtimer = [Nstimer scheduledtimerwithtimeinterval:0.008 target:self Selector: @selector (update) Userinf O:nil Repeats:yes];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} #pragma mark-#pragma mark-screen//support only portait, cannot rotate:-(BOOL) shouldautorotate{return NO; -(Nsuinteger) supportedinterfaceorientations{return uiinterfaceorientationmaskportrait;} #pragma mark-#pragma mark-frequency refresh-(void) update{//judgment if (Isstart = = YES && gamestate = = gameplaying) { [Self updatebird]; [Self updateBG]; [Self setpiles]; [Self checkbirdrect]; }} #pragma mark-#pragma mark-initialize-(void) initbirdandother{//Initialize total scene playlayer = [[UIView alloc] Initwithframe:self . View.bounds]; [Self.view Addsubview:playlayer]; UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (SCREENTAP) ]; [Self.view Addgesturerecognizer:tapgesture]; Initialize obstacle bgView1 = [[UIView alloc] initWithFrame:self.view.bounds]; BgView2 = [[UIView alloc] Initwithframe:cgrectmake (0, Self.view.bounds.size.width, self.view.bounds.size.height) ]; [Playlayer Addsubview:bgview1]; [Playlayer Addsubview:bgview2]; Middle pipe PILEIMGVIEW11 = [self addpilesatx:0]; [BgView1 ADDSUBVIEW:PILEIMGVIEW11]; Pileimgview11.hidden = YES; PileImgView21 = [self addpilesatx:0]; [BgView2 addsubview:pileimgview21]; PileImgView12 = [self addpilesatx:160]; [BgView1 Addsubview:pileimgview12]; Pileimgview12.hidden = YES; PileImgView22 = [self addpilesatx:160]; [BgView2 Addsubview:pileimgview22]; Bottom grass Uiimageview *buttomimg1 = [[Uiimageview alloc] Initwithframe:cgrectmake (0, [[UIScreen Mainscreen] bounds].size.he IGHT-53, 320, 53)]; Buttomimg1.image = [UIImage imagenamed:@ "ground"]; [BgView1 ADDSUBVIEW:BUTTOMIMG1]; Uiimageview *BUTTOMIMG2 = [[Uiimageview alloc] Initwithframe:cgrectmake (0, [[UIScreen mainscreen] Bounds].size.height- 53, 320, 53)]; Buttomimg2.image = [UIImage imagenamed:@ "ground"]; [BgView2 Addsubview:buttomimg2]; Bird's Dress Birdclotherview = [[UIView alloc] Initwithframe:cgrectmake ([[UIScreen mainscreen] BOUNDS].SIZE.HEIGHT/2 + 8 0, 40, 30)]; [Playlayer Addsubview:birdclotherview]; Initialize Bird Birdimgview = [[Uiimageview alloc] init]; Birdimgview.frame = CGRectMake (0, 0, 40, 28); Birdimgview.image = [UIImage imagenamed:@ "Bird"]; [Birdclotherview Addsubview:birdimgview]; Score Scorelabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 40, 300, 30)]; Scorelabel.textalignment = Nstextalignmentright; [Playlayer Addsubview:scorelabel]; Scorelabel.text = @ "score:0";} #pragma mark-#pragma mark-tap screen to start and jump-(void) screentap{//Not click once maxjumptime = MaxTime; if (Isstart = = NO) {Isstart = YES; GameState = gameplaying; For (UIView *tmpview in self.view.subviews) {[Tmpview Removefromsuperview]; } [self initbirdandother]; }//Elevation 30 degrees rotation Cgaffinetransform transform = cgaffinetransformidentity; Birdimgview.transform = Cgaffinetransformrotate (transform, -30 * m_pi/180);} #pragma mark-#pragma mark-Set pipe-(void) setpiles{Nsinteger tmpy = rand ()%120; if (bgview1.frame.origin.x = =-53) {//Set first pipe cgrect rect = pileimgview11.frame; RECT.ORIGIN.Y = tmpY-120; Pileimgview11.frame = rect; if (Pileimgview11.hidden = = YES) {Pileimgview11.hidden= NO; }} else if (bgview1.frame.origin.x = =-213) {//Set a second pipe cgrect rect = pileimgview12.frame; RECT.ORIGIN.Y = tmpY-120; Pileimgview12.frame = rect; if (Pileimgview12.hidden = = YES) {Pileimgview12.hidden = NO; }} else if (bgview2.frame.origin.x = =-53) {//Set a third pipe cgrect rect = pileimgview21.frame; RECT.ORIGIN.Y = tmpY-120; Pileimgview21.frame = rect; } else if (bgview2.frame.origin.x = =-213) {//Set fourth pipe cgrect rect = pileimgview22.frame; RECT.ORIGIN.Y = tmpY-120; Pileimgview22.frame = rect; }} #pragma mark-#pragma mark-collision detection-(void) checkbirdrect{//Collision detection float BIRDRIGHTX = birdclotherview.frame.origin.x + 34; Float birdbuttomy = birdclotherview.frame.origin.y + 24; float birdupy = BIRDCLOTHERVIEW.FRAME.ORIGIN.Y; Calculate the obstructions//columns below and the column above to detect if (birdRightX-0 = = bgview1.frame.origin.x && BirduPY >= 319 + (PILEIMGVIEW11.FRAME.ORIGIN.Y +)) {if (Pileimgview11.hidden = = NO) {[sel F Gameover]; }} else if (birdRightX-0 = = bgview1.frame.origin.x && birdbuttomy <= 199 + (PileImgView11.frame.origin . Y + +) {if (Pileimgview11.hidden = = NO) {[Self gameover]; }} if ((birdRightX-160 = = bgview1.frame.origin.x && birdupy >= 319 + (PILEIMGVIEW12.FRAME.ORIGIN.Y +)) {if (Pileimgview12.hidden = = NO) {[Self gameover]; }} else if (birdRightX-160 = = bgview1.frame.origin.x && birdbuttomy <= 199 + (PileImgView12.frame.orig IN.Y + +) {if (Pileimgview12.hidden = = NO) {[Self gameover]; }} if ((birdRightX-0 = = bgview2.frame.origin.x && birdupy >= 319 + (PILEIMGVIEW21.FRAME.ORIGIN.Y + )) {[Self gameover]; } else if (birdRightX-0 = =bgview2.frame.origin.x && birdbuttomy <= 199 + (PILEIMGVIEW21.FRAME.ORIGIN.Y +)) {[Self Gameover ]; } if ((birdRightX-160 = = bgview2.frame.origin.x && birdupy >= 319 + (PILEIMGVIEW22.FRAME.ORIGIN.Y + 12 0)) {[Self gameover]; } else if (birdRightX-160 = = bgview2.frame.origin.x && birdbuttomy <= 199 + (PILEIMGVIEW22.FRAME.ORIGIN.Y + +) {[Self gameover]; }//Up and down column cross-section detection if (birdRightX-0 > bgview1.frame.origin.x && bgview1.frame.origin.x >= birdrightx -0-52-34 && (birdupy <= 199 + (PILEIMGVIEW11.FRAME.ORIGIN.Y + +) | | birdbuttomy >= 319 + (Pileimgview 11.FRAME.ORIGIN.Y + +)) {if (Pileimgview11.hidden = = NO) {[Self gameover]; }} if (birdRightX-160 > bgview1.frame.origin.x && bgview1.frame.origin.x >= birdRightX-160- 52-34 && (birdupy <= 199 + (PILEIMGVIEW12.FRAME.ORIGIN.Y+ 120) | | Birdbuttomy >= 319 + (PILEIMGVIEW12.FRAME.ORIGIN.Y +)) {if (Pileimgview12.hidden = = NO) { [Self gameover]; }} if (BirdRightX-0 > bgview2.frame.origin.x && bgview2.frame.origin.x >= birdRightX-0-52- && (birdupy <= 199 + (PILEIMGVIEW21.FRAME.ORIGIN.Y +) | | birdbuttomy >= 319 + (PILEIMGVIEW21.FRAME.O RIGIN.Y + +)) {if (Pileimgview12.hidden = = NO) {[Self gameover]; }} if (birdRightX-160 > bgview2.frame.origin.x && bgview2.frame.origin.x >= birdRightX-160- 52-34 && (birdupy <= 199 + (PILEIMGVIEW22.FRAME.ORIGIN.Y + +) | | birdbuttomy >= 319 + (Pileimgview22.fra ME.ORIGIN.Y + +)) {[Self gameover]; }//Judgment Horizon if (birdbuttomy >= [[UIScreen mainscreen] bounds].size.height-53) {[Self gameover]; }} #pragma mark-#pragma mark-Collision handling-(void) gameover{Isstart = NO; GameState = Gameover; Score = 0;} #pragma mark-#pragma mark-Update the position of the bird's coordinates-(void) updatebird{maxjumptime--; CGRect rect = birdclotherview.frame; if (maxjumptime >= 0) {rect.origin.y = RECT.ORIGIN.Y-(MAXV-(maxtime-maxjumptime) *VG); } else {//Angle 30 degree rotation cgaffinetransform transform = cgaffinetransformidentity; Birdimgview.transform = Cgaffinetransformrotate (transform, m_pi/180); RECT.ORIGIN.Y = RECT.ORIGIN.Y-(MAXJUMPTIME*VG); } birdclotherview.frame = rect;} -(void) updatebg{cgrect rect1 = bgview1.frame; CGRect rect2 = bgview2.frame; if (rect1.origin.x <= -320) {rect1.origin.x = 320; } if (rect2.origin.x <= -320) {rect2.origin.x = 320; } rect1.origin.x = rect1.origin.x-1; rect2.origin.x = rect2.origin.x-1; Bgview1.frame = Rect1; Bgview2.frame = Rect2; if (rect1.origin.x = = 0 | | rect1.origin.x = = -160 | | rect2.origin.x = = 0 | | rect2.origin.x = = -160) {if (Pileimgview11.hidden = = yes | | pileimgview12.hidden = = yes) {RE Turn } score + = 1; Scorelabel.text = [NSString stringwithformat:@ "score:%ld", (long) score]; }} #pragma mark-#pragma mark-creating obstacles-(UIView *) Addpilesatx: (float) originx{Nsinteger tmpy = rand ()%120; 0 position and 160 of the unknown Yamashita interval 120px UIView *pileview = [[UIView alloc] Initwithframe:cgrectmake (Originx, tmpY-120, 52, 758)]; Uiimageview *PILEIMG1 = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 0, 52, 319)]; Pileimg1.image = [UIImage imagenamed:@ "Up_bar"]; [Pileview ADDSUBVIEW:PILEIMG1]; Uiimageview *PILEIMG2 = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 439, 52, 319)]; Pileimg2.image = [UIImage imagenamed:@ "Down_bar"]; [Pileview Addsubview:pileimg2]; return pileview; }
Through the above code, you can implement a small game.
Flappy Bird mini-game implementation