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

#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
时间: 2024-11-10 11:09:42

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

小游戏 Lights Out (关灯) 的求解 —— 异或方程组

Author : Evensgn  Blog Link : http://www.cnblogs.com/JoeFan/ Article Link : http://www.cnblogs.com/JoeFan/p/4338003.html   游戏介绍 Lights Out (关灯)是一款据说在20世纪90年代就已经被设计出的小游戏,游戏的玩法十分简单. 首先,给定一个 n 行 m 列的矩形方格阵,每个格子上都有一盏灯. 初始时,有些灯是开着的,有些灯是关着的. 玩家每次进行一次操作,选中一盏

JS写小游戏(一):游戏框架

前言 前一阵发现一个不错的网站,都是一些用html5+css+js写的小游戏,于是打算学习一番,写下这个系列博客主要是为了加深理解,当然也有一些个人感悟,如果英文好可以直接Click Here. 概述 一般,小游戏都要关注两个问题:刷新和交互.因为游戏一般是动态的,所以需要不断刷新.JavaScript是单线程,如果用C语言写过贪吃蛇之类的小游戏,应该知道,单线程一般是挂起一个时间来达到动态效果.比如C语言的Sleep(),JS的setInterval()等.但是js还有一种更高性能的方法req

js选择颜色小游戏(随机生成不含重复数字的数组,通过数组中的数控制定义好的数组)

<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>js网页版小游戏</title> <style media="screen"> .wrap { width: 577px; outline: 1px solid hotpink; margin: 100px auto; box-shadow: 0 0 5px; } .

使用AxureRP7.0制作经典小游戏"大家来找茬"

本案例是<网站蓝图AxureRP7.0从入门到精通视频教程>中的最后一节,适用于对Axure基础知识掌握比较熟练的同学:教程由axure原型库网站录制,转载请注明出处!相信很多刚接触Axure或者学习了一段时间但并没有深入的同学们,看到这个案例一定很惊讶,使用Axure竟然能做出如此逼真的交互效果!通过此案例也可以激发广大同学们学习Axure的热情和信心!试想一下,如果你有情侣的话,把你们珍藏的合影拿出来处理几张,做成大家来找茬的小游戏,不但锻炼了自己的技能,还能给对方一个惊喜,岂不是一举两得

使用AxureRP7.0制作经典数独小游戏原型,axure游戏原型下载

之前,有同学在Q群中提问,如何使用axure制作经典数独解谜小游戏,当时由于时间关系没有来得及亲手制作,而是给同学们提供了Axure6.5版本的一个数独解谜游戏的原型,本教程由axure原型库网站录制,转载请注明出处!但是那个原型做的太过繁杂,所以仅供大家参考交流:在此,金乌老师特地抽时间给同学们使用AxureRP7.0制作了一下,感觉对实战逻辑分析和axure变量的掌握比较有考验,所以就放出来供大家学习交流使用. 在学习的过程中,如果你仅凭自己现有的对axure的掌握,无法准确分析并组织出原型

【H5小游戏开发教程】如何限制微信游戏只能在微信端打开?

在这行里接触的时间多了,就会发现很多有意思的东西. 比如,很多微信小游戏会限制只能在微信端打开,有木有? 有这样的, 也有这样的, 妈蛋,不能用PC访问,这游戏就没法扒呀..... 其实涛舅舅告诉你,这两种都可以扒 而且是用PC! 但是今天,我不教你扒皮 我要教你的是,怎么让你的微信游戏也能限制PC打不开 很想学吧  准备开始! 1.第一种不提了,因为人家是设置了微信授权登录,从微信那里就拦截住了,只能用微信访问,你可能弄不了这么高级的微信授权这块,如果你真能弄,这一讲你也不用听了,因为你已经能

Chrome自带恐龙小游戏的源码研究(完)

在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每达到100分就会出现闪烁特效,游戏第一次gameover时显示历史最高分.分数记录器由DistanceMeter构造函数实现,以下是它的全部代码: 1 DistanceMeter.dimensions = { 2 WIDTH: 10, //每个字符的宽度 3 HEIGHT: 13, //每个字符的高 4 DE

C语言小游戏设计报告

课程设计名称:贪吃蛇小游戏 专业班级:计科15-2 学号:150809229 姓名:XXX 一.设计目标 通过设计,培养学生对电脑的动手能力,使学生巩固<C语言程序设计>课程学习的内容,掌握编写程序的基本方法,强化对其的动手能力,可以独自完成程序的编写. 二.设计内容和要求 设计内容 编写贪吃蛇的小游戏,使其可以成功运行并且操作玩耍. 设计要求 1)源程序要有适当的注释,使程序便于阅读. 2)要有程序运行结果作为依据 三.程序流程 1.编写地图 运用函数,数组编写地图 2.相同的方法把蛇添加进

用Python写一个小游戏

刚学Python时间不长,但也知道了一点,看别人的参考写了一个猜数字小游戏,也算是禹学于乐吧. #!/usr/bin/env   python #coding=utf-8 import random secret = random.randint(1,100) guess,tries = 0,0 print u"已经给出了一个1-99的数字" while guess != secret and tries < 5: print u"请给出你猜的数字:" pri