打飞机,打得漂亮~~~~
我们要用之前学过的东西做个简单的打飞机小游戏了,先来分析一下。
首先要有个背景,你看着界面在移动其实是背景在移动,先创建两张,给背景加个动画,第一张超过屏幕的时候切换第二张。
然后是我们的飞机,其实就是一张图片,上面添加一个动作,当手指拖动的时候跟着手指移动。
接着就是子弹和敌机了,其实可以用之前的雪花下落来实现。
OK,开动了,先做一个欢迎页面
- (void)hitPlane3{
//欢迎页
// 设置背景图片
UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) ];
view.image = [UIImage imageNamed:@"bg"];
[self.view addSubview:view];
UILabel *lab1 = [[UILabel alloc] init];
lab1.frame = CGRectMake(0, 0, 102, 50);
// lab1.backgroundColor = [UIColor greenColor];
lab1.text = @"欢迎!欢迎!";
[self.view addSubview:lab1];
UILabel *lab2 = [[UILabel alloc] init];
lab2.frame = CGRectMake(295, 0, 110, 50);
// lab2.backgroundColor = [UIColor greenColor];
lab2.text = @"热烈欢迎!";
[self.view addSubview:lab2];
//UIView 动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
lab1.frame = CGRectMake(80, 230, 102, 50);
lab2.frame = CGRectMake(183, 230, 110, 50);
[UIView commitAnimations];
UIButton *btn = [[UIButton alloc] init];
UIImage *img = [UIImage imageNamed:@"start"];
[btn setBackgroundImage:img forState:UIControlStateNormal];
btn.frame = CGRectMake(90, 280, 155, 40);
[btn setTitle:@"开始游戏" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(fireGame) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//开始游戏
- (void)fireGame{
FightPlaceViewController *fightVC = [[FightPlaceViewController alloc] init];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//UIView 翻页动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
window.rootViewController =fightVC;
[UIView commitAnimations];
}
效果如下:
当我们点击开始的时候就切换到主页面,主页面上面有咱们的飞机,敌机和子弹,还有暂停按钮和分数,来看看实现
先在头文件里声明以下内容
//宏定义
#define GOON 100
#define RESTART 101
#define GOBACK 102
@interface FightPlaceViewController (){
UIImageView *bgImageViewOne;
UIImageView *bgImageViewTwo;
UILabel *scoreLab;
AVPlayer *player;
NSMutableArray *enemyArray;
NSMutableArray *bulletArray;
UIImageView *hero;
NSArray *array;
BOOL isStop;
UIView *view;
int score;
}
@end
先看背景与主机的实现
- (void)viewDidLoad{
[super viewDidLoad];
// 1.铺地图,还在用4S的大小做,好丢人啊~~~ 以后再说适配的问题
bgImageViewOne = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
bgImageViewOne.image = [UIImage imageNamed:@"backGround "];
[self.view addSubview:bgImageViewOne];
bgImageViewTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0, -480, 320, 480)];
bgImageViewTwo.image = [UIImage imageNamed:@"backGround "];
[self.view addSubview:bgImageViewTwo];
// 3 大飞机
hero = [[UIImageView alloc] initWithFrame:CGRectMake(120, 350, 50, 50)];
hero.tag =10;
hero.image = [UIImage imageNamed:@"plane1"];
[self.view addSubview:hero];
}
// 地图移动
- (void)mapMove{
CGRect rect = bgImageViewOne.frame;
rect.origin.y += 10;
// 记得赋值
bgImageViewOne.frame = rect;
bgImageViewTwo.frame = CGRectMake(0,rect.origin.y - 480, 320, 480);
if (rect.origin.y >= 480){
rect.origin.y = 0;
// 记得重新赋值
bgImageViewOne.frame = rect;
}
}
//手指按着飞机移动 用的这里
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// 获取每一个触摸点的对象
UITouch *touch = [touches anyObject];
// 获取触摸点所在的xy 坐标
CGPoint point = [touch locationInView:self.view];
UIImageView *heroImageView = (UIImageView *)[self.view viewWithTag:10];
// 让飞机的中心点等于获取的触摸点xy
// 把剩下的判断写完
// if (point.x < heroImageView.frame.size.width/2)
// {
// heroImageView.center = CGPointMake(heroImageView.frame.size.width/2, point.y);
// }
heroImageView.center = point;
}
效果如下
接着是敌机和子弹
- (void)viewDidLoad{
[super viewDidLoad];
//当子弹与敌机相撞时的爆照动画图片数组
array = [NSArray arrayWithObjects:[UIImage imageNamed:@"DJBob1"],[UIImage imageNamed:@"DJBob2"],[UIImage imageNamed:@"DJBob3"],[UIImage imageNamed:@"DJBob4"],[UIImage imageNamed:@"DJBob5"],nil];
// 1.铺地图,还在用4S的大小做,好丢人啊~~~ 以后再说适配的问题
bgImageViewOne = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
bgImageViewOne.image = [UIImage imageNamed:@"backGround "];
[self.view addSubview:bgImageViewOne];
bgImageViewTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0, -480, 320, 480)];
bgImageViewTwo.image = [UIImage imageNamed:@"backGround "];
[self.view addSubview:bgImageViewTwo];
// 2 定时器 动起来
[NSTimer scheduledTimerWithTimeInterval:0.09 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
// 3 大飞机
hero = [[UIImageView alloc] initWithFrame:CGRectMake(120, 350, 50, 50)];
hero.tag =10;
hero.image = [UIImage imageNamed:@"plane1"];
[self.view addSubview:hero];
// 4.敌机数组
enemyArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < 20; i++){
TestImage *imageView = [[TestImage alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
imageView.image = [UIImage imageNamed:@"diji"];
[self.view addSubview:imageView];
[enemyArray addObject:imageView];
}
// 5.子弹数组
bulletArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < 20; i++){
TestImage *bulletImageView = [[TestImage alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
bulletImageView.image = [UIImage imageNamed:@"zidan"];
[self.view addSubview:bulletImageView];
[bulletArray addObject:bulletImageView];
}
}
static int count = 0;
//定时器绑定的事件
- (void)onTimer{
if (isStop ==NO){
[self mapMove];
count ++;
if (count >= 10){
[self findAEnemy];
[self findABullet];
count = 0;
}
[self enemyDown];
[self bulletUp];
}
}
//发射子弹
- (void)bulletUp{
for (TestImage *bullet in bulletArray){
if (bullet.isUse == YES){
CGRect rect = bullet.frame;
rect.origin.y -= 5;
if (rect.origin.y < -20)
{
bullet.isUse = NO;
}
bullet.frame = rect;
}
}
}
//找子弹,就是子弹的复用
- (void)findABullet{
for (TestImage *bullet in bulletArray){
if (bullet.isUse == NO){
bullet.isUse = YES;
bullet.frame = CGRectMake(hero.center.x, hero.center.y, 5, 20);
break;
}
}
}
//敌机下落 其实就是雪花下落
- (void)enemyDown{
for (TestImage *enemyImageView in enemyArray){
if (enemyImageView.isUse == YES){
CGRect rect = enemyImageView.frame;
rect.origin.y+=10;
if (rect.origin.y>=480){
enemyImageView.isUse = NO;
}
enemyImageView.frame = rect;
[self crash:rect enemyObj:enemyImageView];
}
}
}
- (void)crash:(CGRect)enemyRect enemyObj:(TestImage *)enemyImageView{
// 找在屏幕中的子弹
for (TestImage *bulletImage in bulletArray){
if (bulletImage.isUse == YES){
//
// 获得屏幕内子弹的坐标
CGRect bulletRect = bulletImage.frame;
// Intersect 相交 子弹与敌机碰撞的时候是根据下面判断的
if (CGRectIntersectsRect(enemyRect, bulletRect)){
NSLog(@" 我的小宇宙");
UIImageView *boomImageView = [[UIImageView alloc] initWithFrame:enemyRect];
// boomImageView.image = [UIImage imageNamed:@"DJBob1"];
[self.view addSubview:boomImageView];
// 帧动画
boomImageView.animationImages = array;
// 设置一个循环的持续时间
boomImageView.animationDuration = 0.5;
// 设置重复的次数
boomImageView.animationRepeatCount = 1;
[boomImageView startAnimating];
// 延迟多少s执行的方法
[self performSelector:@selector(remove:) withObject:boomImageView afterDelay:1];
bulletImage.frame = CGRectMake(0, -20, 0, 0);
enemyImageView.frame = CGRectMake(0, 480, 0, 0);
score += 10;
scoreLab.text = [NSString stringWithFormat:@"%d",score];
// bulletImage.isUse = NO;
}
}
}
}
//找敌机,其实就是敌机的复用
- (void)findAEnemy{
for (TestImage *enemy in enemyArray){
if (enemy.isUse == NO){
enemy.isUse = YES;
enemy.frame = CGRectMake(arc4random()%(321-50), -50, 25, 25);
break;
}
}
}
// 地图移动
- (void)mapMove{
CGRect rect = bgImageViewOne.frame;
rect.origin.y += 10;
// 记得赋值
bgImageViewOne.frame = rect;
bgImageViewTwo.frame = CGRectMake(0,rect.origin.y - 480, 320, 480);
if (rect.origin.y >= 480){
rect.origin.y = 0;
// 记得重新赋值
bgImageViewOne.frame = rect;
}
}
//手指按着飞机移动 用的这里
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// 获取每一个触摸点的对象
UITouch *touch = [touches anyObject];
// 获取触摸点所在的xy 坐标
CGPoint point = [touch locationInView:self.view];
UIImageView *heroImageView = (UIImageView *)[self.view viewWithTag:10];
// 让飞机的中心点等于获取的触摸点xy
// 把剩下的判断写完
// if (point.x < heroImageView.frame.size.width/2)
// {
// heroImageView.center = CGPointMake(heroImageView.frame.size.width/2, point.y);
// }
heroImageView.center = point;
}
//移除敌机
- (void)remove:(UIImageView *)imageView{
[imageView removeFromSuperview];
}
来看效果
最后再做个暂停按钮和分数显示,顺便加了个背景音乐播放~
- (void)viewDidLoad{
[super viewDidLoad];
//当子弹与敌机相撞时的爆照动画图片数组
array = [NSArray arrayWithObjects:[UIImage imageNamed:@"DJBob1"],[UIImage imageNamed:@"DJBob2"],[UIImage imageNamed:@"DJBob3"],[UIImage imageNamed:@"DJBob4"],[UIImage imageNamed:@"DJBob5"],nil];
//这一段是播放音乐
// Bundle 束 应用程序的目录
NSBundle *bundle = [NSBundle mainBundle];
// 获取资源文件的路径
NSString *path = [bundle pathForResource:@"Yours" ofType:@"mp3"];
NSLog(@"===== %@",path);
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
player = [[AVPlayer alloc] initWithURL:url];
[player play];
// 1.铺地图,还在用4S的大小做,好丢人啊~~~ 以后再说适配的问题
bgImageViewOne = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
bgImageViewOne.image = [UIImage imageNamed:@"backGround "];
[self.view addSubview:bgImageViewOne];
bgImageViewTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0, -480, 320, 480)];
bgImageViewTwo.image = [UIImage imageNamed:@"backGround "];
[self.view addSubview:bgImageViewTwo];
// 2 定时器 动起来
[NSTimer scheduledTimerWithTimeInterval:0.09 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
// 3 大飞机
hero = [[UIImageView alloc] initWithFrame:CGRectMake(120, 350, 50, 50)];
hero.tag =10;
hero.image = [UIImage imageNamed:@"plane1"];
[self.view addSubview:hero];
// 4.敌机数组
enemyArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < 20; i++){
TestImage *imageView = [[TestImage alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
imageView.image = [UIImage imageNamed:@"diji"];
[self.view addSubview:imageView];
[enemyArray addObject:imageView];
}
// 5.子弹数组
bulletArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < 20; i++){
TestImage *bulletImageView = [[TestImage alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
bulletImageView.image = [UIImage imageNamed:@"zidan"];
[self.view addSubview:bulletImageView];
[bulletArray addObject:bulletImageView];
}
// 设置暂停按钮和分数
UIButton *stopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
stopBtn.frame = CGRectMake(20, 30, 30, 30);
[stopBtn setBackgroundImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
[stopBtn addTarget:self action:@selector(stopGame) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stopBtn];
scoreLab = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 30)];
scoreLab.text = @"0";
[self.view addSubview:scoreLab];
}
//暂停按钮
- (void)stopGame{
isStop = YES;
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:view];
NSArray *titleArray = @[@"继续",@"重新开始",@"返回"];
for (int i = 0; i < titleArray.count; i++){
UIButton *stopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
stopBtn.frame = CGRectMake(100, (i*60)+120, 100, 50);
[stopBtn addTarget:self action:@selector(menuClick:) forControlEvents:UIControlEventTouchUpInside];
[stopBtn setTitle:[titleArray objectAtIndex:i] forState:UIControlStateNormal];
[stopBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
stopBtn.tag = i+100;
[stopBtn setBackgroundColor:[UIColor purpleColor]];
[view addSubview:stopBtn];
}
}
//暂停按钮绑定的事件
- (void)menuClick:(UIButton *)btn{
isStop = NO;
[view removeFromSuperview];
switch (btn.tag) {
case GOON:{
}
break;
case RESTART:{
score = 0;
scoreLab.text = [NSString stringWithFormat:@"%d",score];
for (TestImage *imageView in bulletArray){
if (imageView.isUse == YES){
imageView.frame = CGRectMake(0, -20, 0, 0);
}
}
for (TestImage *imageView in enemyArray){
if (imageView.isUse == YES){
imageView.frame = CGRectMake(0, 480, 0, 0);
}
}
}
break;
case GOBACK:{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
window.rootViewController= [[ViewController alloc] init];
}
break;
default:
break;
}
}
static int count = 0;
//定时器绑定的事件
- (void)onTimer{
if (isStop ==NO){
[self mapMove];
count ++;
if (count >= 10){
[self findAEnemy];
[self findABullet];
count = 0;
}
[self enemyDown];
[self bulletUp];
}
}
//发射子弹
- (void)bulletUp{
for (TestImage *bullet in bulletArray){
if (bullet.isUse == YES){
CGRect rect = bullet.frame;
rect.origin.y -= 5;
if (rect.origin.y < -20)
{
bullet.isUse = NO;
}
bullet.frame = rect;
}
}
}
//找子弹,就是子弹的复用
- (void)findABullet{
for (TestImage *bullet in bulletArray){
if (bullet.isUse == NO){
bullet.isUse = YES;
bullet.frame = CGRectMake(hero.center.x, hero.center.y, 5, 20);
break;
}
}
}
//敌机下落
- (void)enemyDown{
for (TestImage *enemyImageView in enemyArray){
if (enemyImageView.isUse == YES){
CGRect rect = enemyImageView.frame;
rect.origin.y+=10;
if (rect.origin.y>=480){
enemyImageView.isUse = NO;
}
enemyImageView.frame = rect;
[self crash:rect enemyObj:enemyImageView];
}
}
}
- (void)crash:(CGRect)enemyRect enemyObj:(TestImage *)enemyImageView{
// 找在屏幕中的子弹
for (TestImage *bulletImage in bulletArray){
if (bulletImage.isUse == YES){
//
// 获得屏幕内子弹的坐标
CGRect bulletRect = bulletImage.frame;
// Intersect 相交 子弹与敌机碰撞的时候是根据下面判断的
if (CGRectIntersectsRect(enemyRect, bulletRect)){
NSLog(@" 我的小宇宙");
UIImageView *boomImageView = [[UIImageView alloc] initWithFrame:enemyRect];
// boomImageView.image = [UIImage imageNamed:@"DJBob1"];
[self.view addSubview:boomImageView];
// 帧动画
boomImageView.animationImages = array;
// 设置一个循环的持续时间
boomImageView.animationDuration = 0.5;
// 设置重复的次数
boomImageView.animationRepeatCount = 1;
[boomImageView startAnimating];
// 延迟多少s执行的方法
[self performSelector:@selector(remove:) withObject:boomImageView afterDelay:1];
bulletImage.frame = CGRectMake(0, -20, 0, 0);
enemyImageView.frame = CGRectMake(0, 480, 0, 0);
score += 10;
scoreLab.text = [NSString stringWithFormat:@"%d",score];
// bulletImage.isUse = NO;
}
}
}
}
//找敌机,其实就是敌机的复用
- (void)findAEnemy{
for (TestImage *enemy in enemyArray){
if (enemy.isUse == NO){
enemy.isUse = YES;
enemy.frame = CGRectMake(arc4random()%(321-50), -50, 25, 25);
break;
}
}
}
// 地图移动
- (void)mapMove{
CGRect rect = bgImageViewOne.frame;
rect.origin.y += 10;
// 记得赋值
bgImageViewOne.frame = rect;
bgImageViewTwo.frame = CGRectMake(0,rect.origin.y - 480, 320, 480);
if (rect.origin.y >= 480){
rect.origin.y = 0;
// 记得重新赋值
bgImageViewOne.frame = rect;
}
}
//手指按着飞机移动 用的这里
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// 获取每一个触摸点的对象
UITouch *touch = [touches anyObject];
// 获取触摸点所在的xy 坐标
CGPoint point = [touch locationInView:self.view];
UIImageView *heroImageView = (UIImageView *)[self.view viewWithTag:10];
// 让飞机的中心点等于获取的触摸点xy
// 作业1 把剩下的判断写完
// if (point.x < heroImageView.frame.size.width/2)
// {
// heroImageView.center = CGPointMake(heroImageView.frame.size.width/2, point.y);
// }
heroImageView.center = point;
}
//移除敌机
- (void)remove:(UIImageView *)imageView{
[imageView removeFromSuperview];
}
看看效果如何~
我这里没有实现飞机与敌机相撞时的动画和判断,但是根据上面用到的方法就可以实现这个功能了,还不去练练手~~~