iOS Lighting Games

Source: Internet
Author: User

The third day of learning Iosui mastered some view-related skills, and then thought of doing a little app to solidify it.

I did this little game of lighting at night.


Information about the lighting game reference Baidu Encyclopedia.

Here is my implementation step:

First step: Material preparation

Two pictures are prepared as two states of the lights in the game, one named Red.jpg (for the light off) and the other as: Blue.jpg (on the Light).

Step two: Make the game layout

The game layout is a n*n square and I used the UIButton as a light to make a n*n game layout through the loop.

At first I thought about the layout with a two-layer for loop, but after thinking it over, we realized it with a layer for loop. The implementation code is as follows:

for (int i=0; i<self. Shuliang; i++) {        UIButton *btn=[[uibutton alloc]  initwithframe:                       cgrectmake ((i%line) *btnwidth+ (i%line+1) *JX, ((i/ Line) +1) *jx+ (i/line) *btnwidth+100, Btnwidth, Btnwidth)];        Btn.backgroundcolor=[uicolor Redcolor];        btn.tag=i+1;        [Btn setimage:[uiimage imagenamed:@ "red.jpg"] forstate:uicontrolstatenormal];                [Btn addtarget:self Action: @selector (calculate:) forcontrolevents:uicontroleventtouchupinside];        [Self.view addsubview:btn];            }

where self. Shuliang is the number of lights in each level and I set the number of squares for the level. Line is the number of each row, which is the level.

The loop will eventually draw a line*line game layout on the screen.

Add a button to each button at the same time set a click Method, that is, the Calculate method:

-(void) Calculate: (uibutton*) clickedbutton{    [self showstepcount];    Get the clicked button    NSLog (@ "%ld", Clickedbutton.tag);    Long Index=clickedbutton.tag    ;    [Self togglelight:index];    if ((index-1)%self. level!=0) {        [self togglelight:index-1];    }    if (index%self. level!=0) {        [self togglelight:index+1];    }    if (index+self. Level) <=pow (self. Level, 2)} {        [self togglelight:index+self. Level];    }    if (index-self. Level) (>0) {        [self togglelight:index-self. Level];    }    if ([self Iswin]) {        Uialertview *alert=[[uialertview alloc]initwithtitle:@ "Congratulations" message:@ "you pass!" Delegate:self cancelbuttontitle:@ "Next Off" otherbuttontitles:@ "Replay", @ "Exit", nil];        [Alert show];            } ;            }

The function of the Calculate method is to judge and change the state of the clicked button and the surrounding button. It's simple.


Here's how to change the button state:

Change state-(void) Togglelight: (nsinteger) index{    if ([Self.view viewwithtag:index].backgroundcolor==[uicolor Redcolor])    {                [self.view viewwithtag: (index)].backgroundcolor=[uicolor Bluecolor];        [((uibutton*) [Self.view viewwithtag: (index)]) setimage:[uiimage imagenamed:@ "Blue.jpg"] forstate: UIControlStateNormal];    }    else    {                [self.view viewwithtag: (index)].backgroundcolor=[uicolor Redcolor];        [((uibutton*) [Self.view viewwithtag: (index)]) setimage:[uiimage imagenamed:@ "Red.jpg"] forstate: UIControlStateNormal];}    }

Judging button all lit (clearance)

-(BOOL) iswin{    int j=0;    for (int i=0;i<self. shuliang;i++)    {        if ([Self.view viewwithtag:i+1].backgroundcolor==[uicolor Bluecolor]) {            j + +;        }    }    if (j==self. Shuliang) {        return YES;    }    else    {        return NO;}    }
The above is the approximate implementation of the steps, for beginners is still helpful. Haha run out to entertain oneself or is quite interesting.

Attached:


iOS Lighting Games

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.