大致思想:圈定一个区域,里面的每个格子都用一个数组表示,用于记录该格子是否可以移动过去,动物也用一个数组表示,根据定时器设定时间间隔移动,方向存在头部,点击按钮可以改变头部的方向属性。移动时头按方向移动,每格身体都移动到前一个的位置,同时修改坐标相同的区域大数组中对象的属性移动到的位置不可用,离开的位置可用。食物坐标随机,如果随机的左边不可用,则再次随机知道坐标可用位置。当头和食物的坐标相同时,移动上去的一瞬间食物坐标重新随机,身体最末端增加一个对象。自定义一个UIImageView,给其添加几个属性,用于标记坐标,是否是食物,是否可用,以及头部的移动方向
//自定义UIimageView,为imageView添加几个标记属性
@interface myIV : UIImageView@property (nonatomic,assign) BOOL available;//是否可以走上去,身体和墙的位置为no
@property (nonatomic,assign) BOOL food;//是否是食物
@property (nonatomic,assign) int direction;//标记头的移动方向1234代表上下左右
@property (nonatomic,assign) CGPoint location;//位置,即在30*30的可移动区域的坐标
@end
全局变量声明
@class myIV;
@interface MAViewController : UIViewController
{
NSMutableArray *_baseArray;//储存地图上每个格子的数组
NSMutableArray *_arraySnake;//储存动物身体的数组
myIV *_viewFood;//现实食物的图片
UIButton *_btnStart;//开始按钮
int _count;//计数
UILabel *_labelCount;//计数板
NSTimer *_timer;//控制移动的计时器
}
@end
程序主体
#import "MAViewController.h"
#import "myIV.h"
@interface MAViewController ()
@end
@implementation MAViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//初始化数组用预存发放相关内用
//地图大小为32*32,其中最外圈是围墙,baseArray中的每个对象代表一行,每行为一个数组
_baseArray = [[NSMutableArray alloc]initWithCapacity:32];
//初始化长度为3,分为1头2身体
_arraySnake = [[NSMutableArray alloc]initWithCapacity:3];
//初始化食物imageView
_viewFood = [[myIV alloc]init];
_viewFood.image = [UIImage imageNamed:@"食物图片.png"];
UIImageView *IVBackground = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
IVBackground.image = [UIImage imageNamed:@"背景图片.jpg"];
[self.view addSubview:IVBackground];
//创建头
myIV *headView = [[myIV alloc]init];
headView.location = CGPointMake(30, 28);
headView.direction = 1;
headView.frame = CGRectMake(10+(headView.location.x - 1) * 10,30 +(headView.location.y - 1) * 10 ,10 ,10);
headView.image = [UIImage imageNamed:@"头部图片.png"];
headView.available = NO;
[_arraySnake addObject:headView];
//创建上墙
NSMutableArray *array0 = [[NSMutableArray alloc]initWithCapacity:32];
for (int i = 0; i <32; i ++) {
myIV *view = [[myIV alloc]initWithFrame:CGRectMake(i*10, 20, 10, 10)];
view.image = [UIImage imageNamed:@"方块"];
view.available = NO;
view.food = NO;
view.layer.cornerRadius = 3;
[array0 addObject:view];
[self.view addSubview:view];
}
[_baseArray addObject:array0];
//上墙下墙中间部分的30个数组
for (int i = 0; i < 30; i ++) {
NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:32];
for (int j = 0; j < 30; j ++) {
myIV * blankView = [[myIV alloc]init];
blankView.available = YES;
blankView.frame = CGRectMake(10+j*10, 30 +i * 10, 10, 10);
[self.view addSubview:blankView];
[array addObject:blankView];
}
[_baseArray addObject:array];
}
//下墙
NSMutableArray *array31 = [[NSMutableArray alloc]initWithCapacity:32];
for (int i = 0; i <32; i ++) {
myIV *view = [[myIV alloc]initWithFrame:CGRectMake(i*10, 330, 10, 10)];
view.image = [UIImage imageNamed:@"方块"];
view.available = NO;
view.food = NO;
view.layer.cornerRadius = 3;
[self.view addSubview:view];
[array31 addObject:view];
}
[_baseArray addObject:array31];
//左墙
for (int i = 0; i <30; i ++) {
myIV *view = [[myIV alloc]initWithFrame:CGRectMake(0, 30+i*10,10,10)];
view.image = [UIImage imageNamed:@"方块"];
view.available = NO;
view.food = NO;
view.layer.cornerRadius = 3;
[self.view addSubview:view];
NSMutableArray *array0 = [_baseArray objectAtIndex:i+1];
[array0 insertObject:view atIndex:0];
}
//右墙
for (int i = 0; i <30; i ++) {
myIV *view = [[myIV alloc]initWithFrame:CGRectMake(310, 30+i*10, 10, 10)];
view.image= [UIImage imageNamed:@"方块"];
view.available = NO;
view.food = NO;
view.layer.cornerRadius = 3;
[self.view addSubview:view];
NSMutableArray *array0 = [_baseArray objectAtIndex:i+1];
[array0 addObject:view];
}
//初始化身体*2
for (int i = 2; i > 0; i--) {
myIV *bodyView = [[myIV alloc]init];
bodyView.frame = CGRectMake(10+29*10, 30+(30-i)*10, 10, 10);
bodyView.available = NO;
bodyView.image = [UIImage imageNamed:@"身体图片.png"];
bodyView.location = CGPointMake(30, 31-i);
[_arraySnake addObject:bodyView];
}
//身体添加到视图上
for (myIV * view in _arraySnake) {
[self.view addSubview:view];
}
[self.view addSubview:_viewFood];
//创建控制方向的按钮
[self createDirectionBtn];
//开始按钮
_btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
_btnStart.frame = CGRectMake(50, 360, 100, 40);
[_btnStart setTitle:@"START" forState:UIControlStateNormal];
_btnStart.backgroundColor = [UIColor grayColor];
[_btnStart setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
_btnStart.titleLabel.font = [UIFont systemFontOfSize:20];
[_btnStart addTarget:self action:@selector(btnStartClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btnStart];
//计数板
_labelCount = [[UILabel alloc]initWithFrame:CGRectMake(50, 410, 100, 40)];
_labelCount.text = @"0";
_labelCount.font = [UIFont systemFontOfSize:20];
[self.view addSubview:_labelCount];
}
//头和身体的移动
-(void)snakeMove
{
int count = _arraySnake.count;
myIV *viewHead = [_arraySnake objectAtIndex:0];
myIV *viewLast =[_arraySnake objectAtIndex:count - 1];
myIV *viewBodyForEat =[[myIV alloc]init];
//这个将用做吃到食物后所添加的对象
viewBodyForEat.frame = viewLast.frame;
viewBodyForEat.location = viewLast.location;
viewBodyForEat.image = [UIImage imageNamed:@"身体图片.png"];
viewBodyForEat.available = NO;
if ( viewHead.direction == 1)
{
int x,y;
x = viewHead.location.x;
y = viewHead.location.y;
x = x;
y = y - 1;
myIV *viewHeadDirection = [[myIV alloc]init];
viewHeadDirection = [[_baseArray objectAtIndex:y] objectAtIndex:x];
//判断头的下一个位置是否可用
if (viewHeadDirection.available == NO) {
[_timer invalidate];
_timer = nil;
}
else
{
for (int i = count -1; i > 0 ; i--)
{
myIV *view = [_arraySnake objectAtIndex:i];
myIV *viewNext =[_arraySnake objectAtIndex:i-1];
view.center = viewNext.center;
myIV *viewForBodyMark = [[_baseArray objectAtIndex:viewNext.location.y] objectAtIndex:viewNext.location.x];
viewForBodyMark.available = NO;
view.location = viewNext.location;
}
viewHead.center = CGPointMake(viewHead.center.x, viewHead.center.y - 10);
viewHead.location = CGPointMake(viewHead.location.x, viewHead.location.y - 1);
}
}
else if (viewHead.direction == 2)
{
int x,y;
x = viewHead.location.x;
y = viewHead.location.y;
x = x;
y = y + 1;
myIV *viewHeadDirection = [[myIV alloc]init];
viewHeadDirection = [[_baseArray objectAtIndex:y] objectAtIndex:x];
if (viewHeadDirection.available == NO) {
[_timer invalidate];
_timer = nil;
}
else
{
for (int i = count -1; i > 0 ; i--) {
myIV *view = [_arraySnake objectAtIndex:i];
myIV *viewNext =[_arraySnake objectAtIndex:i-1];
view.center = viewNext.center;
myIV *viewForBodyMark = [[_baseArray objectAtIndex:viewNext.location.y] objectAtIndex:viewNext.location.x];
viewForBodyMark.available = NO;
view.location = viewNext.location;
}
viewHead.center = CGPointMake(viewHead.center.x, viewHead.center.y +10);
viewHead.location = CGPointMake(viewHead.location.x, viewHead.location.y + 1);
}
}
else if (viewHead.direction == 3)
{
int x,y;
x = viewHead.location.x;
y = viewHead.location.y;
x = x-1;
y = y ;
myIV *viewHeadDirection = [[myIV alloc]init];
viewHeadDirection = [[_baseArray objectAtIndex:y] objectAtIndex:x];
if (viewHeadDirection.available == NO) {
[_timer invalidate];
_timer = nil;
}
else
{
for (int i = count -1; i > 0 ; i--) {
myIV *view = [_arraySnake objectAtIndex:i];
myIV *viewNext =[_arraySnake objectAtIndex:i-1];
view.center = viewNext.center;
myIV *viewForBodyMark = [[_baseArray objectAtIndex:viewNext.location.y] objectAtIndex:viewNext.location.x];
viewForBodyMark.available = NO;
view.location = viewNext.location;
}
viewHead.center = CGPointMake(viewHead.center.x - 10, viewHead.center.y );
viewHead.location = CGPointMake(viewHead.location.x-1, viewHead.location.y );
}
}
else if (viewHead.direction == 4)
{
int x,y;
x = viewHead.location.x;
y = viewHead.location.y;
x = x+1;
y = y ;
myIV *viewHeadDirection = [[myIV alloc]init];
viewHeadDirection = [[_baseArray objectAtIndex:y] objectAtIndex:x];
if (viewHeadDirection.available == NO) {
[_timer invalidate];
_timer = nil;
}
else
{
for (int i = count -1; i > 0 ; i--) {
myIV *view = [_arraySnake objectAtIndex:i];
myIV *viewNext =[_arraySnake objectAtIndex:i-1];
view.center = viewNext.center;
myIV *viewForBodyMark = [[_baseArray objectAtIndex:viewNext.location.y] objectAtIndex:viewNext.location.x];
viewForBodyMark.available = NO;
view.location = viewNext.location;
}
viewHead.center = CGPointMake(viewHead.center.x+10, viewHead.center.y );
viewHead.location = CGPointMake(viewHead.location.x+1, viewHead.location.y );
}
}
myIV * viewForBodyMark2 = [[_baseArray objectAtIndex:viewBodyForEat.location.y] objectAtIndex:viewBodyForEat.location.x];
viewForBodyMark2.available = YES;
//判断是否吃到了食物
if (viewHead.location.x == _viewFood.location.x && viewHead.location.y == _viewFood.location.y) {
_count = _count + 1;
_labelCount.text = [NSString stringWithFormat:@"%d",_count];
[self createFood];
[self.view addSubview:viewBodyForEat];
[_arraySnake addObject:viewBodyForEat];
}
}
-(void)btnStartClick
{
[_timer invalidate];
_timer = nil;
_count = 0;
_baseArray = [[NSMutableArray alloc]initWithCapacity:32];
_arraySnake = [[NSMutableArray alloc]initWithCapacity:3];
[self viewDidLoad];
_labelCount.text = [NSString stringWithFormat:@"%d",_count];
//移动定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(snakeMove) userInfo:nil repeats:YES];
[self createFood];
}
//创建控制方向的按钮
-(void)createDirectionBtn
{
UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeCustom];
btnUp.frame =CGRectMake(230, 380, 40, 40);
[btnUp setBackgroundImage:[UIImage imageNamed:@"方向上.png"] forState:UIControlStateNormal];
btnUp.tag = 1;
[btnUp addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnUp];
UIButton *btnDown = [UIButton buttonWithType:UIButtonTypeCustom];
btnDown.frame =CGRectMake(230, 430, 40, 40);
[btnDown setBackgroundImage:[UIImage imageNamed:@"方向下.png"] forState:UIControlStateNormal];
btnDown.tag = 2;
[btnDown addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnDown];
UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
btnLeft.frame =CGRectMake(205, 405, 40, 40);
[btnLeft setBackgroundImage:[UIImage imageNamed:@"方向左.png"] forState:UIControlStateNormal];
btnLeft.tag = 3;
[btnLeft addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnLeft];
UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
btnRight.frame =CGRectMake(255, 405, 40, 40);
[btnRight setBackgroundImage:[UIImage imageNamed:@"方向右.png"] forState:UIControlStateNormal];
btnRight.tag = 4 ;
[btnRight addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnRight];
}
//刷新食物的方法
-(void)createFood
{
int x,y ;
myIV *viewFoodLoaction = [[myIV alloc]init];
do {
x = arc4random()%30+1;
y = arc4random()%30+1;
viewFoodLoaction = [[_baseArray objectAtIndex:y] objectAtIndex:x];
} while (viewFoodLoaction.available == NO);
_viewFood.frame = CGRectMake( x *10, 20 + 10 * y, 10, 10);
_viewFood.location = CGPointMake(x, y);
}
//改变方向的方法
-(void)btnClick:(UIButton *)btn
{
myIV *viewhead = [_arraySnake objectAtIndex:0];
if (viewhead.direction == 1||viewhead.direction == 2)
{
if (btn.tag == 3 ||btn.tag == 4)
{
viewhead.direction = btn.tag;
}
}
else if (viewhead.direction == 3 || viewhead.direction == 4)
{
if (btn.tag == 1 ||btn.tag == 2)
{
viewhead.direction = btn.tag;
}
}
}
@end