初学,制作了个躲避小车的小游戏:
ZYAppDelegate.h:
<span style="font-family:Comic Sans MS;font-size:18px;">#import <UIKit/UIKit.h>
@interface ZYAppDelegate : UIResponder <UIApplicationDelegate>
{
UIImageView *zebra;
UIButton *start;
UIImageView *ren;
int x;
int cy;
int a;
int y;//小汽车的y坐标
UIImageView *car1;
UIImageView *car2;
UIImageView *car3;
UIImageView *car4;
UIImageView *car5;
NSTimer *timer;
UIView *page3;
UIView *page2;
float speed;
int scoureX;
UILabel *fenShu3;
int b;
}
@property (strong, nonatomic) UIWindow *window;
@end</span>下面是实现的文件,都写在一个类里,实在有点low
ZYAppDelegate.m
:#import "ZYAppDelegate.h"
@implementation ZYAppDelegate
- (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];
[self.window makeKeyAndVisible];
speed = 0.02;
UIView *page1 = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIImageView *bkg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"road.jpg"]];
bkg.frame = CGRectMake(0, 0, 320, 480);
[page1 addSubview:bkg];
[self.window addSubview:page1];
cy = 20;
zebra = [[UIImageView alloc] initWithFrame:CGRectMake(140, 0, 40, 300)];
zebra.backgroundColor = [UIColor whiteColor];
[page1 addSubview:zebra];
start = [UIButton buttonWithType:UIButtonTypeCustom];
start.frame = CGRectMake(120, 230, 80, 40);
[start setBackgroundColor:[UIColor lightGrayColor]];
[start setTitle:@"开始" forState:UIControlStateNormal];
start.clipsToBounds = YES;
start.layer.cornerRadius = 3;
[start addTarget:self action:@selector(kaiShi) forControlEvents:UIControlEventTouchUpInside];
page3 = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[page1 addSubview:page3];
car1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"car1"]];
car1.frame = CGRectMake(20, cy, 60, 60);
[page3 addSubview:car1];
car2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"car2"]];
car2.frame = CGRectMake(80, cy, 60, 60);
[page3 addSubview:car2];
car3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"car3"]];
car3.frame = CGRectMake(140, cy, 60, 60);
[page3 addSubview:car3];
car4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"car4"]];
car4.frame = CGRectMake(200, cy, 60, 60);
[page3 addSubview:car4];
car5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"car5"]];
car5.frame = CGRectMake(260, cy, 60, 60);
[page3 addSubview:car5];
[page3 addSubview:start];
UILabel *fenShu1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 20, 20)];
fenShu1.text = @"分";
fenShu1.textColor = [UIColor whiteColor];
[page3 addSubview:fenShu1];
UILabel *fenShu2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 20, 20)];
fenShu2.text = @"数";
fenShu2.textColor = [UIColor whiteColor];
[page3 addSubview:fenShu2];
fenShu3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 20, 20)];
fenShu3.textColor = [UIColor whiteColor];
[page3 addSubview:fenShu3];
UIButton *toLeft = [UIButton buttonWithType:UIButtonTypeCustom];
toLeft.frame = CGRectMake(0, 440, 160, 40);
[toLeft setBackgroundColor:[UIColor lightTextColor]];
[toLeft setBackgroundImage:[UIImage imageNamed:@"toLeft"] forState:UIControlStateNormal];
[toLeft addTarget:self action:@selector(toLeft) forControlEvents:UIControlEventTouchUpInside];
[page3 addSubview:toLeft];
UIButton *toRight = [UIButton buttonWithType:UIButtonTypeCustom];
toRight.frame = CGRectMake(160, 440, 160, 40);
[toRight setBackgroundColor:[UIColor lightTextColor]];
[toRight setBackgroundImage:[UIImage imageNamed:@"toRight"] forState:UIControlStateNormal];
[toRight addTarget:self action:@selector(toRight) forControlEvents:UIControlEventTouchUpInside];
[page3 addSubview:toRight];
ren = [[UIImageView alloc] initWithFrame:CGRectMake(140, 380, 60, 60)];
[ren setImage:[UIImage imageNamed:@"jy8"]];
[page3 addSubview:ren];
x = ren.frame.origin.x;
// [timer setFireDate:[NSDate distantFuture ]];
page2 = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
page2.backgroundColor = [UIColor clearColor];
UILabel *gameOver = [[UILabel alloc] initWithFrame:CGRectMake(70, 140, 220, 60)];
gameOver.text = @"游戏结束";
gameOver.textColor = [UIColor redColor];
gameOver.font = [UIFont systemFontOfSize:45];
[page2 addSubview:gameOver];
UIButton *reStart = [UIButton buttonWithType:UIButtonTypeCustom];
reStart.frame = CGRectMake(120, 230, 80, 40);
[reStart setBackgroundColor:[UIColor lightGrayColor]];
[reStart setTitle:@"重新开始" forState:UIControlStateNormal];
reStart.clipsToBounds = YES;
reStart.layer.cornerRadius = 3;
[reStart addTarget:self action:@selector(reStart) forControlEvents:UIControlEventTouchUpInside];
[page2 addSubview:reStart];
return YES;
}
- (void)toLeft
{
y = car1.frame.origin.y;
if (x == 260 && y < 340) {
ren.frame = CGRectMake(x - 60, 380, 60, 60);
x = 200;
} else if (x == 200 && y < 340)
{
ren.frame = CGRectMake(x - 60, 380, 60, 60);
x = 140;
}else if (x == 140 && y < 340)
{
ren.frame = CGRectMake(x - 60, 380, 60, 60);
x = 80;
}else if (x == 80 && y < 340)
{
ren.frame = CGRectMake(x - 60, 380, 60, 60);
x = 20;
}else if (x == 20 && y < 340)
{
ren.frame = CGRectMake(x, 380, 60, 60);
}
}
- (void)toRight
{
y = car1.frame.origin.y;
if (x == 20 && y < 340) {
ren.frame = CGRectMake(x + 60, 380, 60, 60);
x = 80;
} else if(x == 80 && y < 340)
{
ren.frame = CGRectMake(x + 60, 380, 60, 60);
x = 140;
}else if (x == 140 && y < 340)
{
ren.frame = CGRectMake(x + 60, 380, 60, 60);
x = 200;
}else if (x == 200 && y < 340)
{
ren.frame = CGRectMake(x + 60, 380, 60, 60);
x = 260;
}else if (x == 260 && y < 340)
{
ren.frame = CGRectMake(x, 380, 60, 60);
}
}
- (void)reStart
{
[page2 removeFromSuperview];
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(runCar) userInfo:nil repeats:YES];
[timer fire];
}
- (void)kaiShi
{
// CGRect a = CGRectMake(car3.frame.origin.x, car2.frame.origin.y, 60, 60);
// CGRect b = CGRectMake(ren.frame.origin.x, ren.frame.origin.y, 60, 60);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationDuration:5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
start.frame = CGRectMake(120, 480, 80, 40);
zebra.frame = CGRectMake(140, -300, 40, 300);
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(runCar) userInfo:nil repeats:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(begin2)];
[UIView commitAnimations];
// [timer setFireDate:[NSDate distantPast]];
b = 1;
}
- (void)runCar
{
//使小汽车向下降
cy = cy + 1;
if (cy > 380) {
cy = 20;
if (a == 0) {
[page3 addSubview:car1];
} else if (a == 1)
{
[page3 addSubview:car2];
}else if (a == 2)
{
[page3 addSubview:car3];
}else if (a == 3)
{
[page3 addSubview:car4];
}else
{
[page3 addSubview:car5];
}
a = arc4random()%5;
static int scoure;
scoure = scoure + 1;
if (scoure % 3 == 0) {
speed = speed * 0.7;
}
[timer invalidate];
timer = nil;
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(runCar) userInfo:nil repeats:YES];
if (b == 0) {
scoure = 0;
}
scoureX = scoure;
NSString *str1 = [NSString alloc];
str1 = [NSString stringWithFormat:@"%d",scoureX];
fenShu3.text = str1;
b = b + 1;
}
car1.frame = CGRectMake(20, cy, 60, 60);
car2.frame = CGRectMake(80, cy, 60, 60);
car3.frame = CGRectMake(140, cy, 60, 60);
car4.frame = CGRectMake(200, cy, 60, 60);
car5.frame = CGRectMake(260, cy, 60, 60);
[self deleteCar];
[self crashCar];
}
- (void)crashCar
{
if (cy == 380 && x != (3*a+1)*20) {
[timer invalidate];
[self.window addSubview:page2];
speed = 0.02;
b = 0;
}
}
- (void)deleteCar
{
if (a == 0) {
[car1 removeFromSuperview];
} else if (a == 1)
{
[car2 removeFromSuperview];
}else if (a == 2)
{
[car3 removeFromSuperview];
}else if (a == 3)
{
[car4 removeFromSuperview];
}else
{
[car5 removeFromSuperview];
}
}
- (void)begin2
{
zebra.frame = CGRectMake(140, 480, 40, 300);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationDuration:6];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
zebra.frame = CGRectMake(140, 0, 40, 300);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(begin3)];
[UIView commitAnimations];
}
- (void)begin3
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationDuration:5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
zebra.frame = CGRectMake(140, -300, 40, 300);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(begin2)];
[UIView commitAnimations];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end其中小车及小鸡的素材都在http://www.easyicon.net/这个网站上找的,请多关注!
以下是截图:
这是一个iOS平台上的简单躲避游戏,使用Objective-C编写。游戏中玩家控制的角色需要避开向下移动的小汽车以获得分数。

被折叠的 条评论
为什么被折叠?



