網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了iOS實現(xiàn)拼圖小游戲的具體代碼,供大家參考,具體內(nèi)容如下
首先找到這8張圖片,還需要一張空白的圖片,自己隨便剪一張吧。
定義三個屬性:button可變數(shù)組,圖片可變數(shù)組,正確順序的圖片數(shù)組。
@property(retain, nonatomic)NSMutableArray *buttonArray; @property(retain, nonatomic)NSMutableArray *a; @property(retain, nonatomic)NSArray ? ? ? ?*aa;
鋪好拼圖界面
//圖片數(shù)組a,用來儲存每個圖片名稱,并且用于后來的打亂 self.a = [NSMutableArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg", nil]; //備份一個正確順序的圖片數(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]; //添加點擊事件 [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放入數(shù)組 ? ? ? ? ? ? [self.buttonArray addObject:button]; ? ? ? ? ? ? button.tag = count; ? ? ? ? ? ? [button release]; ? ? ? ? ? ? count++; ? ? ? ? } }
實現(xiàn)button的點擊事件
- (void)change:(UIButton *)sender{ ? ? NSInteger flag = 0; ? ? int p = 0; ? ? //“9.jpg”是空白的那個,打亂后,得在圖片數(shù)組里找到所在下標(biāo),用flag存在來 ? ? for (NSInteger i = 0; i < 9; i++) { ? ? ? ? if ([self.a[i] isEqualToString:@"9.jpg"]) { ? ? ? ? ? ? flag = i; ? ? ? ? } ? ? } ? ? //如果所點擊的button的上下左右其中有一個是空白圖片的話,就跟空白圖片交換在圖片數(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]; ? ? } ? ? //重新給每個button上圖片 ? ? for (int i = 0; i < 9; i++) { ? ? ? ? [self.buttonArray[i] setImage:[UIImage imageNamed:self.a[i]] forState:UIControlStateNormal]; ? ? } ? ? //判斷是否拼圖成功,每對應(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兩個隨機數(shù),通過下標(biāo)交換圖片數(shù)組中的兩張圖片 ? ? 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]; ? ? } }
原文鏈接:https://blog.csdn.net/qq_18399879/article/details/49964651
相關(guān)推薦
- 2022-07-12 計算SHA-1摘要值,并轉(zhuǎn)為16進制字符串
- 2022-03-17 Golang動態(tài)調(diào)用方法小結(jié)_Golang
- 2022-08-28 Go通道channel通過通信共享內(nèi)存_Golang
- 2022-02-19 SharDingJDBC-4.0.0-RC1按月水平分表
- 2022-05-11 解決GitLab中使用SSH的git clone總是提示輸入密碼且任何密碼都不對
- 2022-07-06 Nginx的mirror指令示例配置_nginx
- 2022-02-27 Redux及React-redux的簡單使用
- 2023-11-23 python的相對路徑表示方式
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支