#在蓝懿学习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);
}
大家,要一起共勉,要一起加油啊,
第一,我们要插入一张图片为吃豆人(即:吃货)和四个按钮,并使其能上下左右的移动
例://设置按钮
- (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);
}
大家,要一起共勉,要一起加油啊,
相关文章推荐
- iOS定位和位置信息获取
- iOS开发:XCTest单元测试(附上一个单例的测试代码)
- iOS之There was an internal API error.
- iOS避免文件被同步到iCloud或iTunes
- iOS断言
- ios逆向工程-静态分析
- iOS蓝牙4.0开发流程
- IOS开发—无需自定义,修改tabbar背景颜色
- iOS TextFiled、TextView 关于键盘的收起以及处理键盘遮挡
- ReactiveCocoa,最受欢迎的iOS函数响应式编程库(2.5版),没有之一!
- iOS 的 XMPPFramework 简介
- Block - 3
- ios系统框架一: Core Animation
- iOS_mapKit与Core Location
- IOS中自己关于Ivar和objc_property_t的理解,整理下网上资料
- iOS开发中常用的第三方类库
- iOS开发中更改手机状态栏和隐藏手机状态栏的方法
- iOS9 定位遇到的一点问题
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- IOS FMDB框架 简单粗暴的使用