源码下载位置:http://download.csdn.net/detail/u011005737/8633931
.h文件代码
//
// ZQAppDelegate.h
// Plant
//
// Created by mac on 15-4-26.
// Copyright (c) 2015年 KangZhiQiang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZQAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,retain) NSMutableArray * dijiArr,*zidanArr,*boomArr;
@property (nonatomic,retain) UIImageView *bg1,*bg2,*zhanji;
@property (nonatomic,assign) CGPoint location;
@end
.m文件代码
//
// ZQAppDelegate.m
// Plant
//
// Created by mac on 15-4-26.
// Copyright (c) 2015年 KangZhiQiang. All rights reserved.
//
#import "ZQAppDelegate.h"
@implementation ZQAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//初始化数组
_dijiArr = [[NSMutableArray alloc]init];
_boomArr = [[NSMutableArray alloc]init];
_zidanArr = [[NSMutableArray alloc]init];
//背景平移
_bg1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, moveBg, 320, 480)];
_bg1.image = [UIImage imageNamed:@"bg.png"];
[self.window addSubview:_bg1];
_bg2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, moveBg-480, 320, 480)];
_bg2.image = [UIImage imageNamed:@"bg.png"];
[self.window addSubview:_bg2];
//创建敌机
for (int i = 0; i<10; i++) {
UIImageView *diji = [[UIImageView alloc]init];
diji.frame = CGRectMake(10, i*25, 20, 20);
diji.image = [UIImage imageNamed:@"diji.png"];
diji.tag = 5;//5为非激活状态,6为激活状态
[self.window addSubview:diji];
[_dijiArr addObject:diji];
}
//创建子弹
for (int i = 0; i<20; i++) {
UIImageView *zidan = [[UIImageView alloc]init];
//zidan.frame = CGRectMake(160, 450, 6, 12);
zidan.image = [UIImage imageNamed:@"zidan.png"];
zidan.tag = 5;//5为非激活状态,6为激活状态
[self.window addSubview:zidan];
[_zidanArr addObject:zidan];
}
//创建战机
_zhanji= [[UIImageView alloc]initWithFrame:CGRectMake(160-25, 450-30, 50, 60)];
_zhanji.animationImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"plane1.png"],[UIImage imageNamed:@"plane2.png"], nil];
_zhanji.userInteractionEnabled = YES;
_zhanji.animationDuration = 0.2;
[_zhanji startAnimating];
[self.window addSubview:_zhanji];
//创建爆炸
for (int i = 0 ; i<25; i++) {
UIImageView *imgView = [[UIImageView alloc]init];
imgView.animationImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"bz1.png"],[UIImage imageNamed:@"bz2.png"],[UIImage imageNamed:@"bz3.png"],[UIImage imageNamed:@"bz4.png"],[UIImage imageNamed:@"bz5.png"], nil];
imgView.animationDuration = .5;
imgView.animationRepeatCount = 1;
[_boomArr addObject:imgView];
}
[NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(upDate) userInfo:nil repeats:YES];
[self.window makeKeyAndVisible];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
orMove =NO;
if (_zhanji ==[touch view]) {
_location = [touch locationInView:_zhanji];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if (_zhanji == [touch view]) {
CGPoint point = [touch locationInView:_zhanji];
float dx = point.x-_location.x;
float dy = point.y-_location.y;
CGPoint center = _zhanji.center;
center.x+=dx;
center.y+=dy;
_zhanji.center = center;
orMove =YES;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([touch view]==_zhanji) {
if (!orMove) {
[self boom];
}
}
}
//全部飞机爆炸,即单击飞机全屏爆炸
- (void)boom{
for (UIImageView *imgView in _dijiArr) {
for (UIImageView *imgView1 in _boomArr) {
if (![imgView1 isAnimating]) {
imgView1.frame = imgView.frame;
imgView.frame = CGRectZero;
imgView.tag = 5;
[self.window addSubview:imgView1];
[imgView1 startAnimating];
break;
}
}
}
orMove =NO;
}
int speed = 0;
int moveBg = 0;
bool orMove=NO;
//背景移动
- (void)bgMove{
moveBg+=5;
if (moveBg == 475) {
moveBg = 0;
}
_bg1.frame = CGRectMake(0, moveBg, 320, 480);
_bg2.frame = CGRectMake(0, moveBg-480, 320, 480);
}
- (void)upDate{
if (speed%8 == 0) {
[self finDiji];
[self findZidan];
}
speed++;
[self bgMove];
[self moveDiji];
[self moveZidan];
[self jiZhong];
}
//判断子弹是否击中敌机
- (void)jiZhong{
for (UIImageView *imgView in _zidanArr) {
if (imgView.tag == 6) {
for (UIImageView *imgView1 in _dijiArr) {
if (imgView1.tag == 6) {
if (CGRectIntersectsRect(imgView.frame, imgView1.frame)) {
NSLog(@"000");
for (UIImageView *imgView2 in _boomArr) {
if (![imgView2 isAnimating]) {
imgView2.frame = imgView1.frame;
imgView1.frame = CGRectZero;
imgView1.tag = 5;
imgView.frame = CGRectZero;
imgView.tag = 5;
[self.window addSubview:imgView2];
[imgView2 startAnimating];
break;
}
}
}
}
}
}
}
}
//激活敌机
- (void)finDiji{
for (UIImageView *imgView in _dijiArr) {
if (imgView.tag == 5) {
imgView.tag =6;
imgView.frame = CGRectMake(arc4random()%301, -20, 20, 20);
break;
}
}
}
//敌机移动
- (void)moveDiji{
for (UIImageView *imgView in _dijiArr) {
if (imgView.tag ==6) {
CGRect rect = imgView.frame;
rect.origin.y+=5;
if (rect.origin.y >480) {
imgView.tag =5;
rect.origin.y = -20;
}
imgView.frame = rect;
}
}
}
//激活子弹
- (void)findZidan{
for (UIImageView *imgView in _zidanArr) {
//NSLog(@"000");
if (imgView.tag ==5) {
imgView.tag =6;
imgView.frame = CGRectMake(_zhanji.center.x-3,_zhanji.center.y-35, 6, 12);
break;
}
}
}
//移动子弹
- (void)moveZidan{
for (UIImageView *imgView in _zidanArr) {
if (imgView.tag == 6) {
CGRect rect = imgView.frame;
rect.origin.y-=5;
if (rect.origin.y <-12) {
//rect.origin.y =_zhanji.center.y-25;
imgView.frame = CGRectMake(_zhanji.center.x-3,_zhanji.center.y-35, 6, 12);
imgView.tag = 5;
}
imgView.frame = rect;
}
}
}
@end
用到的图片
bg.png
bz1.png
bz2.png
bz3.png
bz4.png
bz5.png
diji.png
plane1.png
plane2.png
zidan.png