简单地做了一个躲避落物的游戏,初步想法就是落物由上落下,空缺一个位置,从这个位置躲避公共计数+1,随着计数越来越多下落速度越来越快
全局变量声明
UIImage *_imageBarrier;//障碍物图片
UIView *_backgroundView;//背景
int _blankX;//空缺位置X坐标
int _animalX;//小动物坐标X坐标
int _barrierY;//障碍物Y坐标
float _speed;//下落间隔
int _count;//成功躲避次数
UIButton *_btnLeft;//向左移动按钮
UIButton *_btnRight;//向右移动按钮
UILabel *_labelCount;//用于计数的label
UIView *_viewGameSpace;//游戏区域,自控区域大小
UIImageView *_IVBarrier1;//五个障碍物
UIImageView *_IVBarrier2;
UIImageView *_IVBarrier3;
UIImageView *_IVBarrier4;
UIImageView *_IVBarrier5;
UIButton *_btnStart;//开始游戏按钮
UIImageView *_IVAnimal;//存放小动物图片的imageview
UILabel *_labelOver;//gameover提示语
NSTimer *_timer;//定时器
然后是初始化部分
/变量初始化
_animalX= 100;//动物横坐标
_speed = 0.5;//速度
_count =0;//计数
//添加一张背景,设为黑色铺满屏幕
_backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
_backgroundView.backgroundColor = [UIColor blackColor];
[self.window addSubview:_backgroundView];
//向左和向右按钮
_btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
_btnLeft.frame = CGRectMake(0, 440, 160, 40);
[_btnLeft setTitle:@"←.←" forState:UIControlStateNormal];
_btnLeft.backgroundColor = [UIColor grayColor];
[_btnLeft setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_btnLeft.titleLabel.font = [UIFont systemFontOfSize:20];
_btnLeft.layer.cornerRadius = 20;
_btnLeft.tag = 1;
[_btnLeft addTarget:self action:@selector(btnDirectionClick:) forControlEvents:UIControlEventTouchDown];
[_backgroundView addSubview:_btnLeft];
_btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
_btnRight.frame = CGRectMake(160, 440, 160, 40);
[_btnRight setTitle:@"→.→" forState:UIControlStateNormal];
_btnRight.backgroundColor = [UIColor grayColor];
[_btnRight setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_btnRight.titleLabel.font = [UIFont systemFontOfSize:20];
_btnRight.layer.cornerRadius = 20;
_btnRight.tag = 2;
[_btnRight addTarget:self action:@selector(btnDirectionClick:) forControlEvents:UIControlEventTouchDown];
[_backgroundView addSubview:_btnRight];
//计数板
_labelCount = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 35, 30)];
_labelCount.text = @"0";
_labelCount.backgroundColor = [UIColor blackColor];
_labelCount.textColor = [UIColor whiteColor];
[_backgroundView addSubview:_labelCount];
//游戏界面
_viewGameSpace = [[UIView alloc]initWithFrame:CGRectMake(35, 30, 250, 400)];
_viewGameSpace.backgroundColor = [UIColor whiteColor];
[_backgroundView addSubview:_viewGameSpace];
//障碍物图片框
_imageBarrier = [UIImage imageNamed:@"障碍物图片.png"];
_IVBarrier1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
_IVBarrier1.image = _imageBarrier;
[_viewGameSpace addSubview:_IVBarrier1];
_IVBarrier2 = [[UIImageView alloc]initWithFrame:CGRectMake(50, 0, 50, 50)];
_IVBarrier2.image = _imageBarrier;
[_viewGameSpace addSubview:_IVBarrier2];
_IVBarrier3 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 0, 50, 50)];
_IVBarrier3.image = _imageBarrier;
[_viewGameSpace addSubview:_IVBarrier3];
_IVBarrier4 = [[UIImageView alloc]initWithFrame:CGRectMake(150, 0, 50, 50)];
_IVBarrier4.image = _imageBarrier;
[_viewGameSpace addSubview:_IVBarrier4];
_IVBarrier5 = [[UIImageView alloc]initWithFrame:CGRectMake(200, 0, 50, 50)];
_IVBarrier5.image = _imageBarrier;
[_viewGameSpace addSubview:_IVBarrier5];
//开始按钮
_btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
[_btnStart setTitle:@"START!" forState:UIControlStateNormal];
_btnStart.frame = CGRectMake(75, 200, 100, 40);
_btnStart.backgroundColor = [UIColor grayColor];
_btnStart.layer.cornerRadius = 10 ;
[_btnStart addTarget:self action:@selector(btnStartGame1) forControlEvents:UIControlEventTouchDown];
[_viewGameSpace addSubview:_btnStart];
//动物图片框
UIImage *imgXJ = [UIImage imageNamed:@"小鸡"];
_IVAnimal = [[UIImageView alloc]initWithFrame:CGRectMake(_animalX, 350, 50, 50)];
_IVAnimal.image = imgXJ;
[_viewGameSpace addSubview:_IVAnimal];
//GAME OVER提示
_labelOver =[[UILabel alloc]initWithFrame:CGRectMake(20, 60, 210, 100)];
_labelOver.text = @"GAME OVER";
_labelOver.textColor = [UIColor redColor];
_labelOver.font = [UIFont systemFontOfSize:30];
_labelOver.textAlignment = NSTextAlignmentCenter;
//左右按钮点击事件,动物左右移动
-(void)btnDirectionClick:(UIButton *)btn
{
if (btn.tag == 1)
{
if (_animalX == 0) {
Nil;
}
else
{
_animalX = _animalX -50;
_IVAnimal.frame = CGRectMake(_animalX, 350, 50, 50);
}
}
else if(btn.tag == 2)
{
if (_animalX == 200) {
Nil;
}
else
{
_animalX = _animalX +50;
_IVAnimal.frame = CGRectMake(_animalX, 350, 50, 50);
}
}
}
//开始游戏点击方法,游戏重新开始
-(void)btnStartGame1
{
_speed = 0.5;
_count = 0;
[_btnStart removeFromSuperview];//开始游戏按钮隐藏
_timer =[NSTimer scheduledTimerWithTimeInterval:_speed target:self selector:@selector(move) userInfo:nil repeats:YES];//计时器,移动障碍物间隔
[_labelOver removeFromSuperview];//GAME OVER 提示语隐藏
}
//障碍物移动方法
-(void)move
{
//随机移除一个位置的障碍物
if (_blankX == 0) {
[_IVBarrier1 removeFromSuperview];
}
else if ( _blankX ==1 )
{
[_IVBarrier2 removeFromSuperview];
}
else if (_blankX == 2)
{
[_IVBarrier3 removeFromSuperview];
}
else if (_blankX == 3)
{
[_IVBarrier4 removeFromSuperview];
}
else if (_blankX == 4)
{
[_IVBarrier5 removeFromSuperview];
}
//改变障碍物位置
_IVBarrier1.frame = CGRectMake(0, _barrierY , 50, 50);
_IVBarrier2.frame = CGRectMake(50, _barrierY , 50, 50);
_IVBarrier3.frame = CGRectMake(100, _barrierY , 50, 50);
_IVBarrier4.frame = CGRectMake(150, _barrierY , 50, 50);
_IVBarrier5.frame = CGRectMake(200, _barrierY , 50, 50);
//判断是否到了最后一行
_barrierY = _barrierY + 50;
if (_barrierY == 400) {
//最后一行返回第一行
_barrierY = 0;
//动物与障碍物同一排时不可移动
_btnLeft.enabled = NO;
_btnRight.enabled = NO;
//判断石头撞到障碍物
if (_animalX != _blankX*50) {
[_viewGameSpace addSubview:_labelOver];//撞到之后GAME OVER
[_timer invalidate];//计时器停止
_timer = nil;
[_viewGameSpace addSubview:_btnStart];//显示开始按钮
}
}
//判断是否是第一行
if (_barrierY == 50) {
//左右Button重新启用
_btnLeft.enabled = YES;
_btnRight.enabled =YES;
//补全空缺位置
if (_blankX == 0) {
[_viewGameSpace addSubview:_IVBarrier1];
}
else if ( _blankX ==1 )
{
[_viewGameSpace addSubview:_IVBarrier2];
}
else if (_blankX == 2)
{
[_viewGameSpace addSubview:_IVBarrier3];
}
else if (_blankX == 3)
{
[_viewGameSpace addSubview:_IVBarrier4];
}
else if (_blankX == 4)
{
[_viewGameSpace addSubview:_IVBarrier5];
}
//重置空缺位置
_blankX = arc4random()%5;
_count = _count + 1;//计数加1
NSString *str =[NSString stringWithFormat:@"%d",_count];
_labelCount.text =str;
//每三次增加一次难度
if (_count % 3 == 0)
{
_speed = _speed *0.7;
}
//计时器重启
[_timer invalidate];
_timer = nil;
_timer =[NSTimer scheduledTimerWithTimeInterval:_speed target:self selector:@selector(move) userInfo:nil repeats:YES];
}
}