關燈小游戲的基本代碼實現


#import <UIKit/UIKit.h>

@interface LTView : UIView
{
UILabel
*_label;
UITextField
*_textField;
}
-(id)initWithFrame:(CGRect)frame text:(NSString *)text background:(UIColor *)background fontSize:(int)size alignment:(enum NSTextAlignment)alignment textColor:(UIColor *)textColor percent:(float)percent interval:(float)interval textField:(NSString *)textField borderStyle:(enum UITextBorderStyle)borderStyle returnKeyType:(enum UIReturnKeyType)returnKeyType keyboardType:(enum UIKeyboardType)keyboardType placeholder:(NSString *)placeholder secureTextEntry:(BOOL)secureTextEntry clearButtonMode:(enum UITextFieldViewMode)clearButtonMode clearsOnBeginEditing:(BOOL)cleardOnBeginEditing delegate:(id)delegate;

@end
 1 @implementation LTView
2
3 -(id)initWithFrame:(CGRect)frame text:(NSString *)text background:(UIColor *)background fontSize:(int)size alignment:(enum NSTextAlignment)alignment textColor:(UIColor *)textColor percent:(float)percent interval:(float)interval textField:(NSString *)textField borderStyle:(enum UITextBorderStyle)borderStyle returnKeyType:(enum UIReturnKeyType)returnKeyType keyboardType:(enum UIKeyboardType)keyboardType placeholder:(NSString *)placeholder secureTextEntry:(BOOL)secureTextEntry clearButtonMode:(enum UITextFieldViewMode)clearButtonMode clearsOnBeginEditing:(BOOL)cleardOnBeginEditing delegate:(id)delegate
4 {
5 if ([super initWithFrame:frame]) {
6 _label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width*percent, frame.size.height)];
7 _label.text=text;
8 _label.backgroundColor=background;
9 _label.font=[UIFont systemFontOfSize:size];
10 _label.textAlignment=alignment;
11 _label.textColor=textColor;
12 [self addSubview:_label];
13 _textField=[[UITextField alloc]initWithFrame:CGRectMake(frame.size.width*percent+interval, 0, frame.size.width*(1-percent)-20-interval, frame.size.height)];
14 _textField.borderStyle=borderStyle;
15 _textField.returnKeyType=returnKeyType;
16 _textField.keyboardType=keyboardType;
17 _textField.placeholder=placeholder;
18 _textField.clearsOnBeginEditing=cleardOnBeginEditing;
19 _textField.clearButtonMode=clearButtonMode;
20 _textField.delegate=delegate;
21 [self addSubview:_textField];
22 }
23 return self;
24 }
25 @end

 1 #import "RegisterViewController.h"
2 #import "LTView.h"
3 #import "GameViewController.h"
4 #import "ViewController.h"
5 @interface RegisterViewController ()
6 {
7 LTView *_userName;
8 LTView *_password;
9
10 }
11 @end
12
13 @implementation RegisterViewController
14
15 - (void)viewDidLoad {
16 [super viewDidLoad];
17 self.view.backgroundColor=[UIColor grayColor];
18
19 UIImageView *image=[[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
20 [image setImage:[UIImage imageNamed:@"fj.jpg"]];
21 [self.view addSubview:image];
22
23 _userName=[[LTView alloc]initWithFrame:CGRectMake(0,150, 320, 30) text:@"用戶名" background:[UIColor grayColor] fontSize:18 alignment:NSTextAlignmentCenter textColor:[UIColor blackColor] percent:0.28 interval:10 textField:@"525655828" borderStyle:UITextBorderStyleRoundedRect returnKeyType:UIReturnKeyDone keyboardType:UIKeyboardTypeNumberPad placeholder:@"請輸入用戶名" secureTextEntry:NO clearButtonMode:UITextFieldViewModeWhileEditing
24 clearsOnBeginEditing:YES delegate:self];
25 [self.view addSubview:_userName];
26 _password=[[LTView alloc]initWithFrame:CGRectMake(0, 230, 320, 30) text:@"密碼" background:[UIColor grayColor] fontSize:18 alignment:NSTextAlignmentCenter textColor:[UIColor blackColor] percent:0.28 interval:10 textField:@"12345678" borderStyle:UITextBorderStyleRoundedRect returnKeyType:UIReturnKeyDone keyboardType:UIKeyboardTypeDefault placeholder:@"請輸入密碼" secureTextEntry:YES clearButtonMode:UITextFieldViewModeWhileEditing clearsOnBeginEditing:YES delegate:self];
27 [self.view addSubview:_password];
28 UIButton *registe=[[UIButton alloc]initWithFrame:CGRectMake(120, 310, 80, 30)];
29 [registe setBackgroundColor:[UIColor greenColor]];
30 [registe.layer setBorderColor:(__bridge CGColorRef)([UIColor redColor])];
31 [registe.layer setCornerRadius:10];
32 [registe.layer setBorderWidth:1];
33 [registe setTitle:@"開始游戲" forState:UIControlStateNormal];
34 [registe setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
35 [registe setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
36 registe.titleLabel.font=[UIFont systemFontOfSize:18];
37 [self.view addSubview:registe];
38 [registe addTarget:self action:@selector(doClickRegister) forControlEvents:UIControlEventTouchDown];
39 }
40 -(void)doClickRegister
41 {
42 UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"歡迎回到游戲" message:@"請選擇想要進入的游戲" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"開始游戲", nil];
43 [alertView show];
44 }
45 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
46 {
47 if (buttonIndex==0) {
48 [self.navigationController pushViewController:[[ViewController alloc]init] animated:YES];
49
50 }
51 if (buttonIndex==1) {
52 [self.navigationController pushViewController:[[GameViewController alloc]init] animated:YES];
53 }
54 }
55 //觸摸屏幕退出鍵盤
56 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
57 {
58 [self.view endEditing:YES];
59 }
60 - (void)didReceiveMemoryWarning {
61 [super didReceiveMemoryWarning];
62 // Dispose of any resources that can be recreated.
63 }
64
65 /*
66 #pragma mark - Navigation
67
68 // In a storyboard-based application, you will often want to do a little preparation before navigation
69 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
70 // Get the new view controller using [segue destinationViewController].
71 // Pass the selected object to the new view controller.
72 }
73 */
74
75 @end
 1 - (void)viewDidLoad {
2 [super viewDidLoad];
3 self.view.backgroundColor=[UIColor grayColor];
4 UIImageView *image=[[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
5 [image setImage:[UIImage imageNamed:@"fj.jpg"]];
6 [self.view addSubview:image];
7 UILabel *grameName=[[UILabel alloc]initWithFrame:CGRectMake(85, 80,220, 200)];
8 grameName.text=@"開始游戲";
9 grameName.font=[UIFont systemFontOfSize:40];
10 grameName.textColor=[UIColor blackColor];
11 [self.view addSubview:grameName];
12 UIButton *registe=[[UIButton alloc]initWithFrame:CGRectMake(130, 260, 60, 30)];
13
14 registe.titleLabel.textAlignment=NSTextAlignmentCenter;
15 [registe setTitle:@"登陸" forState:UIControlStateNormal];
16 [registe setBackgroundColor:[UIColor greenColor]];
17 [registe.layer setCornerRadius:10];
18 [registe setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
19 [registe setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
20 [registe addTarget:self action:@selector(doClickRegiste) forControlEvents:UIControlEventTouchDown];
21 [self.view addSubview:registe];
22 }
23 -(void)doClickRegiste
24 {
25 [self.navigationController pushViewController:[[RegisterViewController alloc]init] animated:YES];
26 }
27 - (void)didReceiveMemoryWarning {
28 [super didReceiveMemoryWarning];
29 // Dispose of any resources that can be recreated.
30 }
31
32 @end
  1 #import "GameViewController.h"
2
3 @interface GameViewController ()
4
5 @end
6
7 @implementation GameViewController
8
9 //設置行數和列數
10 int line=1;
11 int row=1;
12
13
14 - (void)viewDidLoad {
15 [super viewDidLoad];
16
17 //設置背景圖片
18 UIImageView *background=[[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
19 //將背景圖片設置為手機屏幕大小
20 [background setImage:[UIImage imageNamed:@"fj-1.jpg"]];
21 [self.view addSubview:background];
22 //設置游戲圖片所占view的大小
23 float size=background.frame.size.width;
24
25 //循環列數和行數
26 for (int i=0; i<line; i++) {
27 for (int j=0; j<row; j++) {
28 UIButton *btnImage=[[UIButton alloc]initWithFrame:CGRectMake(j*size/line,((background.frame.size.height-size)/2)+i*(size/row), size/line, size/row)];
29 btnImage.backgroundColor=[UIColor greenColor];
30 //設置游戲圖片
31 [btnImage setBackgroundImage:[UIImage imageNamed:@"anniu.jpg"] forState:UIControlStateNormal];
32 //給每張按鈕圖片設置tag值
33 btnImage.tag=i*line+j+1;
34 //設置游戲圖片的背景顏色
35
36 //根據圖片的半徑設置圓形圖片
37 btnImage.layer.cornerRadius=size/line/2;
38 btnImage.layer.masksToBounds=YES;
39 //將游戲圖片加到主視圖上
40 [self.view addSubview:btnImage];
41 //設置點擊事件
42 [btnImage addTarget:self action:@selector(doClickChangeBackgroundImage:) forControlEvents:UIControlEventTouchDown];
43
44 }
45 }
46 }
47 //實現點擊方法
48 -(void)doClickChangeBackgroundImage:(UIButton *)sender
49 {
50 [self changeBackgroundImage:(UIButton *)[sender.superview viewWithTag:sender.tag]];
51 if ((sender.tag-1)%line!=0) {
52 [self changeBackgroundImage:(UIButton *)[sender.superview viewWithTag:sender.tag-1]];
53 }
54 if (sender.tag%line!=0) {
55 [self changeBackgroundImage:(UIButton *)[sender.superview viewWithTag:sender.tag+1]];
56 }
57 if (sender.tag-line>0) {
58 [self changeBackgroundImage:(UIButton *)[sender.superview viewWithTag:sender.tag-line]];
59 }
60 if (sender.tag+line<=line*row) {
61 [self changeBackgroundImage:(UIButton *)[sender.superview viewWithTag:sender.tag+line]];
62 }
63 //調用判斷圖片背景顏色的方法,看是否已經完成全部圖片背景顏色的切換
64 [self imageSame];
65
66 }
67 //判斷圖片背景是否全部換完
68 -(void)imageSame
69 {
70 int count=0;
71 for (int i=1; i<line*row+1; i++) {
72 if ([self.view viewWithTag:i].backgroundColor==[UIColor whiteColor]) {
73 count++;
74 }
75 }
76 if (count==line*row){
77 UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"闖關成功" message:@"恭喜闖關成功" delegate:self cancelButtonTitle:@"退出游戲" otherButtonTitles:@"下一關",@"重新開始", nil];
78 [alertView show];
79
80 }
81 }
82 /**
83 *
84 *
85 */
86 //警告框的實現
87 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
88 {
89 if (buttonIndex==1) {
90
91 for (int i=0; i<line*row; i++) {
92 //移除按鈕后面的背景色
93 [[self.view viewWithTag:i+1]removeFromSuperview];
94 }
95 line++;
96 row++;
97 NSLog(@"2");
98 [self viewDidLoad];
99 }if (buttonIndex==2) {
100 for (int i=0; i<line*row; i++) {
101 [[self.view viewWithTag:i+1]removeFromSuperview];
102 }
103 [self viewDidLoad];
104 }
105 }
106 //改變按鈕圖片
107 -(void)changeBackgroundImage:(UIButton *)btn
108 {
109 if (btn.backgroundColor==[UIColor greenColor]) {
110 [btn setBackgroundImage:[UIImage imageNamed:@"xiaolian.jpg"] forState:UIControlStateNormal];
111 btn.backgroundColor=[UIColor whiteColor];
112 }else
113 {
114 [btn setBackgroundImage:[UIImage imageNamed:@"anniu.jpg"] forState:UIControlStateNormal];
115 btn.backgroundColor=[UIColor greenColor];
116 }
117 }
118 - (void)didReceiveMemoryWarning {
119 [super didReceiveMemoryWarning];
120 // Dispose of any resources that can be recreated.
121 }
122
123
124
125
126 @end

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: