欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)拼圖小游戲

 更新時(shí)間:2022年03月24日 10:57:30   作者:疾風(fēng)哥哥  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)拼圖小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)拼圖小游戲的具體代碼,供大家參考,具體內(nèi)容如下

首先找到這8張圖片,還需要一張空白的圖片,自己隨便剪一張吧。

定義三個(gè)屬性:button可變數(shù)組,圖片可變數(shù)組,正確順序的圖片數(shù)組。

@property(retain, nonatomic)NSMutableArray *buttonArray;
@property(retain, nonatomic)NSMutableArray *a;
@property(retain, nonatomic)NSArray ? ? ? ?*aa;

鋪好拼圖界面

//圖片數(shù)組a,用來儲(chǔ)存每個(gè)圖片名稱,并且用于后來的打亂
self.a = [NSMutableArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg", nil];
//備份一個(gè)正確順序的圖片數(shù)組,用于判斷游戲是否過關(guān)
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];
//添加點(diǎn)擊事件
[star addTarget:self action:@selector(kaishi) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:star];
[star release];

//鋪出9個(gè)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];
? ? ? ? ? ? //給每個(gè)button上圖片
? ? ? ? ? ? [button setImage:[UIImage imageNamed:self.a[count]] forState:UIControlStateNormal];
? ? ? ? ? ? [self.view addSubview:button];
? ? ? ? ? ? //給每個(gè)button添加點(diǎn)擊事件
? ? ? ? ? ? [button addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? ? ? //把button放入數(shù)組
? ? ? ? ? ? [self.buttonArray addObject:button];
? ? ? ? ? ? button.tag = count;
? ? ? ? ? ? [button release];
? ? ? ? ? ? count++;
? ? ? ? }
}

實(shí)現(xiàn)button的點(diǎn)擊事件

- (void)change:(UIButton *)sender{
? ? NSInteger flag = 0;
? ? int p = 0;
? ? //“9.jpg”是空白的那個(gè),打亂后,得在圖片數(shù)組里找到所在下標(biāo),用flag存在來
? ? for (NSInteger i = 0; i < 9; i++) {
? ? ? ? if ([self.a[i] isEqualToString:@"9.jpg"]) {
? ? ? ? ? ? flag = i;
? ? ? ? }
? ? }

? ? //如果所點(diǎn)擊的button的上下左右其中有一個(gè)是空白圖片的話,就跟空白圖片交換在圖片數(shù)組的位置
? ? if (sender.tag - flag == 3 || sender.tag - flag == -3 || sender.tag - flag == 1 || sender.tag - flag == -1) {
? ? ? ? [self.a exchangeObjectAtIndex:flag withObjectAtIndex:sender.tag];
? ? }
? ? //重新給每個(gè)button上圖片
? ? for (int i = 0; i < 9; i++) {
? ? ? ? [self.buttonArray[i] setImage:[UIImage imageNamed:self.a[i]] forState:UIControlStateNormal];
? ? }

? ? //判斷是否拼圖成功,每對(duì)應(yīng)了一張圖片p就加一,如果p最后等于9說明游戲通關(guān)
? ? 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:@"已通關(guān)" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
? ? ? ? [a show];
? ? ? ? [a release];
? ? }
}

打亂所有圖片

- (void)kaishi{
//產(chǎn)生0到8兩個(gè)隨機(jī)數(shù),通過下標(biāo)交換圖片數(shù)組中的兩張圖片
? ? for (int i = 0; i < 10; i++) {
? ? ? ? [self.a exchangeObjectAtIndex:(arc4random() % 9)
? ? ? ? ? ? ? ? ? ? withObjectAtIndex:(arc4random() % 9)];
? ? }
? ? //給每個(gè)button上圖片
? ? for (int i = 0; i < 9; i++) {
? ? ? ? [self.buttonArray[i] setImage:[UIImage imageNamed:self.a[i]] forState:UIControlStateNormal];
? ? }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論