iOS实现拼图小游戏

2022-10-07,,

本文实例为大家分享了ios实现拼图小游戏的具体代码,供大家参考,具体内容如下

首先找到这8张图片,还需要一张空白的图片,自己随便剪一张吧。

定义三个属性:button可变数组,图片可变数组,正确顺序的图片数组。

@property(retain, nonatomic)nsmutablearray *buttonarray;
@property(retain, nonatomic)nsmutablearray *a;
@property(retain, nonatomic)nsarray        *aa;

铺好拼图界面

//图片数组a,用来储存每个图片名称,并且用于后来的打乱
self.a = [nsmutablearray arraywithobjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg", nil];
//备份一个正确顺序的图片数组,用于判断游戏是否过关
self.aa = [nsarray arraywitharray:self.a];
//重新开始按钮
uibutton *star = [[uibutton alloc] initwithframe:cgrectmake(120, 400, 100, 40)];
[star settitle:@"重新开始" forstate:uicontrolstatenormal];
[star settitlecolor:[uicolor bluecolor] forstate:uicontrolstatenormal];
star.layer.cornerradius = 6.6f;
star.layer.backgroundcolor = [[uicolor colorwithred:0.922 green:0.925 blue:0.929 alpha:1]cgcolor];
//添加点击事件
[star addtarget:self action:@selector(kaishi) forcontrolevents:uicontroleventtouchupinside];
[self.view addsubview:star];
[star release];

//铺出9个button
    self.buttonarray = [nsmutablearray array];
    nsinteger count = 0;
    nsinteger wight = 351 / 3;
    nsinteger higth = 351 / 3;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            uibutton *button = [[uibutton alloc] initwithframe:cgrectmake(j * (wight+2) + 10, i * (higth + 2) + 20, wight, higth)];
            button.backgroundcolor = [uicolor blackcolor];
            //给每个button上图片
            [button setimage:[uiimage imagenamed:self.a[count]] forstate:uicontrolstatenormal];
            [self.view addsubview:button];
            //给每个button添加点击事件
            [button addtarget:self action:@selector(change:) forcontrolevents:uicontroleventtouchupinside];
            //把button放入数组
            [self.buttonarray addobject:button];
            button.tag = count;
            [button release];
            count++;
        }
}

实现button的点击事件

- (void)change:(uibutton *)sender{
    nsinteger flag = 0;
    int p = 0;
    //“9.jpg”是空白的那个,打乱后,得在图片数组里找到所在下标,用flag存在来
    for (nsinteger i = 0; i < 9; i++) {
        if ([self.a[i] isequaltostring:@"9.jpg"]) {
            flag = i;
        }
    }

    //如果所点击的button的上下左右其中有一个是空白图片的话,就跟空白图片交换在图片数组的位置
    if (sender.tag - flag == 3 || sender.tag - flag == -3 || sender.tag - flag == 1 || sender.tag - flag == -1) {
        [self.a exchangeobjectatindex:flag withobjectatindex:sender.tag];
    }
    //重新给每个button上图片
    for (int i = 0; i < 9; i++) {
        [self.buttonarray[i] setimage:[uiimage imagenamed:self.a[i]] forstate:uicontrolstatenormal];
    }

    //判断是否拼图成功,每对应了一张图片p就加一,如果p最后等于9说明游戏通关
    for (int i = 0; i < 9 ; i++) {
        if ([self.a[i] isequaltostring:self.aa[i]]) {
            p++;
        }else{
            break;
        }
    }
    if (p == 9) {
        nslog(@"%d",p);
        uialertview *a = [[uialertview alloc] initwithtitle:@"恭喜!" message:@"已通关" delegate:self cancelbuttontitle:@"确定" otherbuttontitles:nil];
        [a show];
        [a release];
    }
}

打乱所有图片

- (void)kaishi{
//产生0到8两个随机数,通过下标交换图片数组中的两张图片
    for (int i = 0; i < 10; i++) {
        [self.a exchangeobjectatindex:(arc4random() % 9)
                    withobjectatindex:(arc4random() % 9)];
    }
    //给每个button上图片
    for (int i = 0; i < 9; i++) {
        [self.buttonarray[i] setimage:[uiimage imagenamed:self.a[i]] forstate:uicontrolstatenormal];
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《iOS实现拼图小游戏.doc》

下载本文的Word格式文档,以方便收藏与打印。