编程实现贪吃蛇游戏界面的主要代码
2013-10-17 13:25
731 查看
- (void)viewDidLoad
{
[super
viewDidLoad];
allsheshen=[[NSMutableArray alloc ]
init];
if (time==0)
{
time=1;
}
printf("time=%f",time);
//allsheshen=[[NSMutableArray alloc]
init];
// Do any additional setup after loading
the view from its nib.
tim=[NSTimer
scheduledTimerWithTimeInterval:time
target:self selector:@selector(moveSnake:) userInfo:nil
repeats:YES];
//实现蛇身的动画移动 通过time来控制移动速度
// [NSTimer
scheduledTimerWithTimeInterval:0.3 target:self
selector:@selector(run) userInfo:nil repeats:YES];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}
//获得难度对应的时间
-(void)gettime:(int)nand;
//设置难度的时间,难度增加蛇移动的速度增快
{ NSLog(@"HUOLAI =======%d",nand);
switch (nand)
{
case
1:
time=1;
break;
case
2:
time=0.9;
break;
case
3:
time=0.8;
break;
case
4:
time=0.7;
break;
case
5:
time=0.6;
break;
case
6:
time=0.5;
break;
case
7:
time=0.4;
break;
case
8:
time=0.3;
break;
case
9:
time=0.2;
break;
case
10:
time=0.1;
break;
default:
break;
}
[tim invalidate];//让原来的失效
执行下面新的
tim=[NSTimer
scheduledTimerWithTimeInterval:time
target:self selector:@selector(moveSnake:) userInfo:nil
repeats:YES];
}
-(IBAction)backToMenu:(id)sender{
[AppDelegate
switchViewController:0];
//跳转主要界面
state=1;
}
- (IBAction)move:(id)sender {
//判断蛇的移动方向
UIButton * nowbt=sender;
switch (nowbt.tag)
{
case
100:
a=1;
break;
case
101:
a=2;
break;
case
102:
a=3;
break;
case
103:
a=4;
break;
default:
break;
}
}
//撞墙或者撞到自己
-(void)over
{
[AppDelegate
switchViewController:5];
UIAlertView *
aa=[[UIAlertView
alloc]initWithTitle:@"Game
Over" message:labfenshu.text
delegate:self cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];
[aa show];
state=1;
labfenshu.text=@"0";
score=0;
//结束就把身体清除
UISnakeBody * now;
for ( int i=[allsheshen count]-1;i>=0;i--)
{
now=[allsheshen objectAtIndex:i];
[now
removeFromSuperview];
}
//初始化身体数组
allsheshen=[[NSMutableArray alloc]
init];
}
//随机产生坐标
-(void)zuobiao{
x=rand()%15*20+10;
y=rand()%22*20+10;
}
//吃到食物
-(void)eat{
score+=100;
labfenshu.text=[[NSString alloc ]initWithFormat:@"分数%d",score
];
labdian.center=CGPointMake(x, y);
//添加一个节点到m_allBody的尾部
UISnakeBody * labBody=[[UISnakeBody alloc]initWithFrame:CGRectMake(0, 0,
20, 20)];
labBody.backgroundColor=[UIColor yellowColor];
UISnakeBody * lastBody=[[UISnakeBody alloc]initWithFrame:CGRectMake(0, 0,
20, 20)];//蛇尾部
if(allsheshen.count==0)
{
lastBody.center=CGPointMake(ax, ay);
labBody.dir=a;//蛇头的方向
}
else
{
lastBody=allsheshen.lastObject;
labBody.dir=lastBody.dir;//等于最后一个节点的方向
switch
(labBody.dir) {
case 1://up
labBody.center=CGPointMake(lastBody.center.x,lastBody.center.y+20);
break;
case 2://down
labBody.center=CGPointMake(lastBody.center.x,lastBody.center.y-20);
break;
case 3://left
labBody.center=CGPointMake(lastBody.center.x+20,lastBody.center.y);
break;
case 4://right
labBody.center=CGPointMake(lastBody.center.x-20,lastBody.center.y);
break;
default:
break;
}
}
[allsheshen addObject:labBody];//保存到集合
[self.view addSubview:labBody];
}
//检测随机产生的坐标是否和蛇身冲突
-(int)zbchongtu
{
if (allsheshen.count!=0)
{
for (UISnakeBody *
node in
allsheshen)
{
if (x==node.center.x&&y==node.center.y) {
return 2;
}
else
{
return 1;
}
}
}
return YES;
}
-(void)moveSnake:(int)dir
{
NSLog(@"SSSSSS====%f",time);
if (state==0)
{
AppDelegate
* nowdelegate=[UIApplication
sharedApplication].delegate;
[UIView beginAnimations:nil
context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:nowdelegate.window
cache:YES];
ax=labshe.center.x;
ay=
labshe.center.y;
switch
(a) {
case 1://up
self.labshe.center=CGPointMake(labshe.center.x, labshe.center.y-20);
break;
case 2://down
self.labshe.center=CGPointMake(labshe.center.x, labshe.center.y+20);
break;
case 3://left
self.labshe.center=CGPointMake(labshe.center.x-20,
labshe.center.y);
break;
case 4://right
self.labshe.center=CGPointMake(labshe.center.x+20,
labshe.center.y);
break;
default:
break;
}
//撞墙的时候
if (labshe.center.x>[[UIScreen mainScreen]bounds].size.width||labshe.center.x<</span>0||labshe.center.y<</span>0||labshe.center.y>[[UIScreen mainScreen]bounds].size.height)
{
[self over];
}
//撞到自己身体的时候
if
(allsheshen.count!=0)
{
for (UISnakeBody * node in allsheshen)
{
if (labshe.center.x==node.center.x&&labshe.center.y==node.center.y)
{
[self
over];
}
}
}
//当蛇碰到了食物
if(labshe.center.x==labdian.center.x&&
labshe.center.y==labdian.center.y)
{
[self zuobiao];
bool1=[self zbchongtu];
while (bool1==2)
{
printf("684358739467356");
[self zuobiao];
bool1=[self zbchongtu];
}
[self eat];
}
//移动蛇的身体
UISnakeBody
* nowbody;
UISnakeBody
* prebody;
if
(allsheshen.count>0)
{
for ( int i=[allsheshen count]-1;i>0;i--)
{
nowbody=[allsheshen objectAtIndex:i];
prebody=[allsheshen objectAtIndex:i-1];
nowbody.center=prebody.center;
}
UISnakeBody *
first=[allsheshen objectAtIndex:0];
first.center=CGPointMake(ax, ay);
}
}
}
-(void)touchfx:(CGPoint)_cg{
int mx=abs(_cg.x-labshe.center.x);
int my=abs(_cg.y-labshe.center.y);
if (mx>my)
{
if
(_cg.x>labshe.center.x)
{
a=4;
}
else
{
a=3;
}
}
else
{
if
(_cg.y>labshe.center.y)
{
a=2;
}
else
{
a=1;
}
}
}
@end
{
[super
viewDidLoad];
allsheshen=[[NSMutableArray alloc ]
init];
if (time==0)
{
time=1;
}
printf("time=%f",time);
//allsheshen=[[NSMutableArray alloc]
init];
// Do any additional setup after loading
the view from its nib.
tim=[NSTimer
scheduledTimerWithTimeInterval:time
target:self selector:@selector(moveSnake:) userInfo:nil
repeats:YES];
//实现蛇身的动画移动 通过time来控制移动速度
// [NSTimer
scheduledTimerWithTimeInterval:0.3 target:self
selector:@selector(run) userInfo:nil repeats:YES];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}
//获得难度对应的时间
-(void)gettime:(int)nand;
//设置难度的时间,难度增加蛇移动的速度增快
{ NSLog(@"HUOLAI =======%d",nand);
switch (nand)
{
case
1:
time=1;
break;
case
2:
time=0.9;
break;
case
3:
time=0.8;
break;
case
4:
time=0.7;
break;
case
5:
time=0.6;
break;
case
6:
time=0.5;
break;
case
7:
time=0.4;
break;
case
8:
time=0.3;
break;
case
9:
time=0.2;
break;
case
10:
time=0.1;
break;
default:
break;
}
[tim invalidate];//让原来的失效
执行下面新的
tim=[NSTimer
scheduledTimerWithTimeInterval:time
target:self selector:@selector(moveSnake:) userInfo:nil
repeats:YES];
}
-(IBAction)backToMenu:(id)sender{
[AppDelegate
switchViewController:0];
//跳转主要界面
state=1;
}
- (IBAction)move:(id)sender {
//判断蛇的移动方向
UIButton * nowbt=sender;
switch (nowbt.tag)
{
case
100:
a=1;
break;
case
101:
a=2;
break;
case
102:
a=3;
break;
case
103:
a=4;
break;
default:
break;
}
}
//撞墙或者撞到自己
-(void)over
{
[AppDelegate
switchViewController:5];
UIAlertView *
aa=[[UIAlertView
alloc]initWithTitle:@"Game
Over" message:labfenshu.text
delegate:self cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];
[aa show];
state=1;
labfenshu.text=@"0";
score=0;
//结束就把身体清除
UISnakeBody * now;
for ( int i=[allsheshen count]-1;i>=0;i--)
{
now=[allsheshen objectAtIndex:i];
[now
removeFromSuperview];
}
//初始化身体数组
allsheshen=[[NSMutableArray alloc]
init];
}
//随机产生坐标
-(void)zuobiao{
x=rand()%15*20+10;
y=rand()%22*20+10;
}
//吃到食物
-(void)eat{
score+=100;
labfenshu.text=[[NSString alloc ]initWithFormat:@"分数%d",score
];
labdian.center=CGPointMake(x, y);
//添加一个节点到m_allBody的尾部
UISnakeBody * labBody=[[UISnakeBody alloc]initWithFrame:CGRectMake(0, 0,
20, 20)];
labBody.backgroundColor=[UIColor yellowColor];
UISnakeBody * lastBody=[[UISnakeBody alloc]initWithFrame:CGRectMake(0, 0,
20, 20)];//蛇尾部
if(allsheshen.count==0)
{
lastBody.center=CGPointMake(ax, ay);
labBody.dir=a;//蛇头的方向
}
else
{
lastBody=allsheshen.lastObject;
labBody.dir=lastBody.dir;//等于最后一个节点的方向
switch
(labBody.dir) {
case 1://up
labBody.center=CGPointMake(lastBody.center.x,lastBody.center.y+20);
break;
case 2://down
labBody.center=CGPointMake(lastBody.center.x,lastBody.center.y-20);
break;
case 3://left
labBody.center=CGPointMake(lastBody.center.x+20,lastBody.center.y);
break;
case 4://right
labBody.center=CGPointMake(lastBody.center.x-20,lastBody.center.y);
break;
default:
break;
}
}
[allsheshen addObject:labBody];//保存到集合
[self.view addSubview:labBody];
}
//检测随机产生的坐标是否和蛇身冲突
-(int)zbchongtu
{
if (allsheshen.count!=0)
{
for (UISnakeBody *
node in
allsheshen)
{
if (x==node.center.x&&y==node.center.y) {
return 2;
}
else
{
return 1;
}
}
}
return YES;
}
-(void)moveSnake:(int)dir
{
NSLog(@"SSSSSS====%f",time);
if (state==0)
{
AppDelegate
* nowdelegate=[UIApplication
sharedApplication].delegate;
[UIView beginAnimations:nil
context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:nowdelegate.window
cache:YES];
ax=labshe.center.x;
ay=
labshe.center.y;
switch
(a) {
case 1://up
self.labshe.center=CGPointMake(labshe.center.x, labshe.center.y-20);
break;
case 2://down
self.labshe.center=CGPointMake(labshe.center.x, labshe.center.y+20);
break;
case 3://left
self.labshe.center=CGPointMake(labshe.center.x-20,
labshe.center.y);
break;
case 4://right
self.labshe.center=CGPointMake(labshe.center.x+20,
labshe.center.y);
break;
default:
break;
}
//撞墙的时候
if (labshe.center.x>[[UIScreen mainScreen]bounds].size.width||labshe.center.x<</span>0||labshe.center.y<</span>0||labshe.center.y>[[UIScreen mainScreen]bounds].size.height)
{
[self over];
}
//撞到自己身体的时候
if
(allsheshen.count!=0)
{
for (UISnakeBody * node in allsheshen)
{
if (labshe.center.x==node.center.x&&labshe.center.y==node.center.y)
{
[self
over];
}
}
}
//当蛇碰到了食物
if(labshe.center.x==labdian.center.x&&
labshe.center.y==labdian.center.y)
{
[self zuobiao];
bool1=[self zbchongtu];
while (bool1==2)
{
printf("684358739467356");
[self zuobiao];
bool1=[self zbchongtu];
}
[self eat];
}
//移动蛇的身体
UISnakeBody
* nowbody;
UISnakeBody
* prebody;
if
(allsheshen.count>0)
{
for ( int i=[allsheshen count]-1;i>0;i--)
{
nowbody=[allsheshen objectAtIndex:i];
prebody=[allsheshen objectAtIndex:i-1];
nowbody.center=prebody.center;
}
UISnakeBody *
first=[allsheshen objectAtIndex:0];
first.center=CGPointMake(ax, ay);
}
}
}
-(void)touchfx:(CGPoint)_cg{
int mx=abs(_cg.x-labshe.center.x);
int my=abs(_cg.y-labshe.center.y);
if (mx>my)
{
if
(_cg.x>labshe.center.x)
{
a=4;
}
else
{
a=3;
}
}
else
{
if
(_cg.y>labshe.center.y)
{
a=2;
}
else
{
a=1;
}
}
}
@end
相关文章推荐
- java GUI编程-贪吃蛇游戏简单实现
- 贪吃蛇游戏——主要代码
- 游戏编程笔记-起步(三)-贪吃蛇游戏实现
- Java GUI编程之贪吃蛇游戏简单实现方法【附demo源码下载】
- GameBuilder开发游戏应用系列之100行代码实现贪吃蛇
- 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之四 读取xml格式文档到界面
- 【代码笔记】多线程游戏开发——伏魔记:第一步——开始游戏界面实现(一)
- html5贪吃蛇游戏使用63行代码完美实现
- unity_NGUI系统学习(十四)_游戏界面滑动切换的效果实现(无代码实现)
- 基于C语言实现的贪吃蛇游戏完整实例代码
- 63行代码完美实现html5 贪吃蛇游戏
- 经典游戏---贪吃蛇从C++代码实现
- 3DGAMES中WIN32+OpenGL游戏中实现光照阴影算法主要代码实现
- 【贪吃蛇,C++实现】,由简单游戏开始学习OOP,的编程新手
- Matlab---GUI界面编程2(m文件纯代码实现)
- [置顶] 63行代码完美实现html5 贪吃蛇游戏
- 经典游戏---贪吃蛇从C++代码实现
- 零基础HTML5游戏制作教程 第6章 贪吃蛇的实现及代码
- 经典游戏---贪吃蛇从C++代码实现
- java GUI编程-贪吃蛇游戏简单实现