您的位置:首页 > 移动开发 > IOS开发

#在蓝懿学习iOS的日子#小游戏吃豆人

2015-10-20 10:55 351 查看
#在蓝懿学习iOS的日子#由于初学,我们都没有掌握什么高级技巧,需要用比较繁琐的的代码l来表示一个小小的东西,那么我们就来回忆一下吃豆人的不周吧

第一,我们要插入一张图片为吃豆人(即:吃货)和四个按钮,并使其能上下左右的移动

例://设置按钮

- (IBAction)top:(id)sender {

x = 0;

y = -1.5;

,还要设置吃豆人的运动范围

例:chihuo.center = CGPointMake(chihuo.center.x+x, chihuo.center.y+y);

if (chihuo.center.x>=300) {

chihuo.center = CGPointMake(300, chihuo.center.y);

}

第二,设置插入一张图片(即:food)

出现食物的间隔

//设置食物出现的间隔

[self addfood];

[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(addfood) userInfo:nil repeats:YES];

}

-(void)addfood{

//设置食物

food = [[UIImageView alloc]initWithFrame:CGRectMake(arc4random()%280, arc4random()%528, 40, 40)];

food.image = [UIImage imageNamed:@"a.jpg"];

[self.view addSubview:food];

//设置食物出现的时间

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(missFood) userInfo:nil repeats:NO];

}

-(void)missFood{

//食物移出屏幕

food.center = CGPointMake(1000, 0);

//判断吃货是否有吃到食物 和 判断吃货是否在疯狂状态

if (CGRectIntersectsRect(chihuo.frame, food.frame)&&chihuo. tag == 0) {

food.center = CGPointMake(1000, 0);

chihuo.image = [UIImage imageNamed:@"b.jpg"];

chihuo. tag = 1;

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(moveAction) userInfo:nil repeats:NO];

第三,设置小鬼(即:插入图片)的出现运动轨迹,和吃豆人的相撞

//判断chihuo和小鬼的碰撞

if (CGRectIntersectsRect(chihuo.frame, topiv.frame)) {

if (chihuo.tag==0) {

//数字和文本的转换,叠加死亡的次数

int count =diedcount.text. intValue;

diedcount.text =[@(count+1)stringValue];

//chihuo碰到小鬼死亡,回到中心点

chihuo.center = CGPointMake(160, 568/2);

//chihuo在中心点,保持不动

x= 0;

y= 0;

}else{

//吃货在吃掉食物后,变强大,小鬼消失

topiv.center = CGPointMake(1000, 0);

}

第四,设置吃豆人丢失生命的次数(用label)来记录,

if (CGRectIntersectsRect(chihuo.frame,bottomiv.frame)) {

if (chihuo.tag==0) {

//数字和文本的转换,叠加死亡的次数

int count =diedcount.text. intValue;

diedcount.text =[@(count+1)stringValue];

//chihuo碰到小鬼死亡,回到中心点

chihuo.center = CGPointMake(160, 568/2);

//chihuo在中心点,保持不动

x= 0;

y= 0;

}else{

//吃货在吃掉食物后,变强大,小鬼消失

bottomiv.center = CGPointMake(1000, 0);

}

大家,要一起共勉,要一起加油啊,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: