iOS-demo系列之第二篇:贪吃蛇(哈哈,我在卖萌)
好了现在讲一下基本思路:首先要把蛇画出来,蛇要有蛇头和蛇身,然后蛇身是跟着蛇头动的,也就是蛇头往哪里走,蛇身就往哪里走,
那么我们通过一个数组来描述整个蛇,数组的第一个元素表示蛇头,最后一个元素表示蛇尾,当蛇头往前进一步的时候,数组最后一个元素删除,
然后整个数组元素下移,最后数组的第一个元素添加蛇头的坐标,
调用setNeedDisplay 刷新界面
这样蛇就画出来了,那么我们要让蛇不停的前进就要开启NSTimer定时器,每个一个时间间隔就重复一次前面的步骤(我的是间隔0.2s)
好了前面的还只是让蛇直线前进,接下来还要添加手势让蛇上下左右前进,思路也很简单,就是控制好蛇头的方向就是了,蛇身自然就往哪个方向走了;
好了基本思路讲完,接下来上核心代码:
-(void)drawflash:(CGContextRef)context{
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
// 画蛇头 j ,k 表示蛇头的坐标(x,y) (9,9)表示蛇头的大小
CGRect re1=CGRectMake(j, k, 9, 9);
CGContextAddRect(context, re1);
// 画蛇身 arr 存储蛇身的坐标
for (int i=0; i<[arr count]; i++) {
p1=CGPointFromString([arr objectAtIndex:i]);
CGRect re2=CGRectMake(p1.x, p1.y, 9, 9);
CGContextAddRect(context, re2);
}
CGContextFillPath(context);
}
//传坐标和数组过来 添加蛇身坐标
-(void)set:(int)a set1:(int)b set2:(int)c set3:(int)d point:(NSMutableArray *)arr1{
j=a,k=b;
l=c,m=d;
arr=[NSArray arrayWithArray:arr1];
}
-(void)drawRect:(CGRect)rect{
CGContextRef context=UIGraphicsGetCurrentContext();
[self drawflash:context];
}
上面是把蛇画出来,接下来,开启定时器,刷新蛇:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
//定时器绑定的方法
-(void)timerFired:(NSTimer *)timer{
//控制蛇头的四个方向,也就是蛇头坐标的上下左右 (i,j)表示坐标
if (n1) {
j=j+10;
}
if (n2) {
i=i-10;
}
if (n3) {
i=i+10;
}
if (n4) {
j=j-10;
}
NSLog(@"%d",j);
p.x=i;p.y=j;
NSString *str=NSStringFromCGPoint(p);
// 判断是否吃到食物,吃到就蛇尾数组加一,更新随机数
if (i==(m%29+1)*10&&j==(m/29+1)*10) {
NSLog(@"%d",m);
[arr addObject:str];
m = arc4random() % 1073;
k=(m%29+1)*10;l=(m/29+1)*10;
}
// 蛇头坐标更新,即在数组第一个插入最新的蛇头坐标
[arr insertObject:str atIndex:0];
// 蛇尾坐标更新,即在数组最后一个删掉一个坐标
[arr removeObjectAtIndex:[arr count]-1];
// 调用myview的方法,传蛇头的坐标过去,还有蛇尾数组
[self.my set:i set1:j set2:k set3:l point:arr];
// 判断是否撞墙
if (i<10||i>310||j<10||j>380) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"游戏结束" message:nil delegate:nil cancelButtonTitle:@"重新开始" otherButtonTitles: nil];
[alert show];
[timer setFireDate:[NSDate distantFuture]];// 临时停止,开启 distantpast
//[timer invalidate];// 永远停止
}
CGPoint h;
for (int o=1; o<[arr count]; o++) {
h=CGPointFromString([arr objectAtIndex:o]);
if (p.x==h.x&&p.y==h.y) {
NSLog(@"nihaoooooo");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"游戏结束" message:nil delegate:nil cancelButtonTitle:@"重新开始" otherButtonTitles: nil];
[alert show];
[timer setFireDate:[NSDate distantFuture]];// 临时停止,开启 distantpast
//[timer invalidate];// 永远停止
}
}
// 调用刷新
[self.my setNeedsDisplay];
}
接下来添加手势:
// 定义添加手势
UISwipeGestureRecognizer *swp1=nil;swp1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handswp:)];
[swp1 setDirection:UISwipeGestureRecognizerDirectionDown];
[self.my addGestureRecognizer:swp1];
swp1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handswp:)];
[swp1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.my addGestureRecognizer:swp1];
swp1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handswp:)];
[swp1 setDirection:UISwipeGestureRecognizerDirectionRight];
[self.my addGestureRecognizer:swp1];
swp1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handswp:)];
[swp1 setDirection:UISwipeGestureRecognizerDirectionUp];
[self.my addGestureRecognizer:swp1];
//手势绑定的方法
-(void)handswp:(UISwipeGestureRecognizer *)gestur{
if (gestur.direction==UISwipeGestureRecognizerDirectionDown) {
n1=YES;n2=n3=n4=NO;
NSLog(@"nihao");
}
if (gestur.direction==UISwipeGestureRecognizerDirectionLeft) {
n2=YES;n1=n3=n4=NO;
NSLog(@"nihao1");
}
if (gestur.direction==UISwipeGestureRecognizerDirectionRight) {
n3=YES;n1=n2=n4=NO;
NSLog(@"nihao2");
}
if (gestur.direction==UISwipeGestureRecognizerDirectionUp) {
n4=YES;n1=n2=n3=NO;
NSLog(@"nihao3");
}
}
然后就是添加蛋蛋了,产生随机数:
// 产生随机数,坐标点是由37*29=1073组成
m = arc4random() % 1073;
// 计算随机数产生的坐标点(k,l)
k=(m%29+1)*10;l=(m/29+1)*10;
效果图:
好了,贪吃蛇的基本思路及核心代码讲到这里,源代码在这点击打开链接