码迷,mamicode.com
首页 > 其他好文 > 详细

关灯小游戏的基本代码实现

时间:2015-04-26 19:43:50      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

#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

 

关灯小游戏的基本代码实现

标签:

原文地址:http://www.cnblogs.com/sevenTian/p/4458132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!