iOS pseudo-Implementation mouse game

Source: Internet
Author: User
Tags gopher

The hamster is a game that can be realized with iOS knowledge. The core technique is to play the animation through ImageView, and click on button to stop the animation of the mouse that is currently playing. Don't say much about the code directly.

This is to add the current background picture, then add the ImageView to the background image, and add a button on each ImageView to perform the attack on the gopher.

- (void) viewdidload {[Super viewdidload]; //Create a background picture of the underlying displayUiimageview *imageview =[[Uiimageview alloc] initWithFrame:self.view.frame]; Imageview.image= [UIImage imagenamed:@"Back.png"]; //Open the interaction of the picture (ImageView is off by default)imageview.userinteractionenabled =YES;  for(inti =0; I <7; i++) {         for(intj =0; J <7; J + +) {            //to add a picture of a gopher hole_imagev = [[Uiimageview alloc] Initwithframe:cgrectmake ( -+ -(In -+ -*j, -, -)]; _imagev.image= [UIImage imagenamed:@"Emptyhole.png"]; _imagev.tag=149+i*7+j+1; //Create a Click event for each hole_button =[UIButton Buttonwithtype:uibuttontypesystem]; _button.frame= CGRectMake (0,0, -, -);            [_button addtarget:self Action: @selector (Hit:) forcontrolevents:uicontroleventtouchupinside]; //add a Tag value to each button to determine which button you clicked._button.tag = -+i*7+j+1;            [_imagev Addsubview:_button]; _imagev.userinteractionenabled=YES;        [ImageView Addsubview:_imagev]; }    }    //Specify a background picture of the current realitySelf.view =ImageView; //Adding Timers[self addtimer]; //additional setup after loading the view, typically from a nib.}

Add a timer after the determination is complete to achieve the presence of hamsters at different points in different time periods.

// Add a timer to start the playback of the picture -(void) addtimer{    = [Nstimer scheduledtimerwithtimeinterval:1  target:self selector: @selector (startgame) Userinfo:nil repeats:yes];}

The method of implementing the timer to realize the animation of the hamster hole

//animation that initiates a hole in the hamster- (void) startgame{//define a mutable array to hold the picture you are playingNsmutablearray *arr =[Nsmutablearray array]; //a picture of a hamster hole     for(inti =1; I <7; i++) {UIImage*image = [UIImage imagenamed:[nsstring stringWithFormat:@"T%d.png", I]];    [Arr Addobject:image]; }    //a picture of a hamster back hole     for(inti =1; I <7; i++) {UIImage*image = [UIImage imagenamed:[nsstring stringWithFormat:@"T%d.png",7-I]];    [Arr Addobject:image]; }    //get random values to allow the hamster to appear at random locationsNsinteger num = arc4random ()% the+ Max; //ImageView of random realistic hamster images based on tag values_imagev = (Uiimageview *) [Self.view Viewwithtag:num]; //an array of ImageView for the specified image_imagev.animationimages =arr; //Specify the number of plays_imagev.animationrepeatcount =1; //Specify the playback time_imagev.animationduration =1; //Turn on playback[_imagev startanimating];}

Implements the click time of the button to perform the effect of the hamster being hit.

- (void) Hit: (UIButton *) sender{//gets the current Imagev based on the tag value of the click buttonNsinteger num =Sender.tag; //based on the tag value of the button to get the current click is that ImageView_imagev = (Uiimageview *) [Self.view viewwithtag:num+ the]; //determine the location of the button to perform a click event_button = (UIButton *) [Self.view Viewwithtag:num]; //determines whether the currently clicked ImageView is playing the animation    if(_imagev.isanimating = =YES) {        //stop playing an animation[_imagev stopanimating]; //define a mutable array to store the picture when the hamster is hitNsmutablearray *imagearr =[Nsmutablearray array]; //a picture of a gopher being hitUIImage *image = [UIImage imagenamed:@"Hit.png"]; //empty picture.UIImage *image1 = [UIImage imagenamed:@"Emptyhole.png"]; //tell two pictures to add to a mutable array[Imagearr Addobject:image];        [Imagearr Addobject:image1]; //specify an array to play the animation_imagev.animationimages =Imagearr; //Number of brake plays (0 for Infinite loop playback)_imagev.animationrepeatcount =1; //specify when to play_imagev.animationduration =1; //start playing animations[_imagev startanimating]; }Else{       //if the clicked location does not play the method in the animation execution.    }}

After writing this code you can complete a game of gopher.

Finally attach the required picture, above the picture for the picture in the project name (before the colon is the name of the picture please do not copy the colon too!!).

Back

Enptyhole:

Hit

T1:

T2:

T3:

T4:

T5:

T6:

iOS pseudo-Implementation mouse 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.