網站首頁 編程語言 正文
本文實例為大家分享了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]; ? ? } }
原文鏈接:https://blog.csdn.net/qq_18399879/article/details/49964651
相關推薦
- 2022-06-09 Qt中QPainter與坐標的使用_C 語言
- 2023-07-09 SQL Server中的NULL值處理:判斷與解決方案
- 2023-11-15 LaTeX調整圖片大小的方法;自動調整合適的大小
- 2022-09-26 利用C++實現?然連接操作算法_C 語言
- 2022-06-27 Android音視頻開發之MediaCodec的使用教程_Android
- 2023-08-13 基于TP5框架的excel導入功能
- 2022-11-15 python重用父類功能的兩種方式實例詳解_python
- 2022-09-21 關于reduce的介紹及用法說明_基礎知識
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支