您的位置:首页 > 其它

简单的猜拳游戏

2015-07-29 11:21 323 查看
<span style="font-size:24px;">//
//  ZYAppDelegate.m
//  CaiGame
//
//  Created by mac on 15-7-29.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//

#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];
   
    
//    创建一个试图并初始化,让其背景图片为1.jpg
    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];

    imageView.frame=self.window.frame;
    [self.window addSubview:imageView];
//    label全局变量
    label=[[UILabel alloc] initWithFrame:CGRectMake(90, 20, 160, 30)];
    label.text=@"欢迎来到猜拳游戏";
    label.textColor=[UIColor greenColor];
    [imageView addSubview:label];
    
    
    
    btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame=CGRectMake(20, 100, 80, 100);
    btn1.backgroundColor=[UIColor whiteColor];
    
    [imageView addSubview:btn1];
    
    
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame=CGRectMake(120, 100, 80, 100);
    btn2.backgroundColor=[UIColor whiteColor];
    [btn2 setImage:[UIImage imageNamed:@"vs.gif"] forState:UIControlStateNormal];
    [imageView addSubview:btn2];
    
    btn3=[UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame=CGRectMake(220, 100, 80, 100);
    btn3.backgroundColor=[UIColor whiteColor];
    
    [imageView addSubview:btn3];
    
    
    UIButton *jianDao=[UIButton buttonWithType:UIButtonTypeCustom];
    jianDao.frame=CGRectMake(90, 350, 35, 70);
    [jianDao setImage:[UIImage imageNamed:@"jiandao.gif"] forState:UIControlStateNormal];
    jianDao.backgroundColor=[UIColor redColor];
    jianDao.tag=10086;
    [jianDao addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:jianDao];
    
    
    UIButton *bu=[UIButton buttonWithType:UIButtonTypeCustom];
    bu.frame=CGRectMake(130, 350, 35, 70);
    [bu setImage:[UIImage imageNamed:@"bu.gif"] forState:UIControlStateNormal];
    bu.backgroundColor=[UIColor redColor];
    bu.tag=100861;
    [bu addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:bu];
    
    UIButton *shitou=[UIButton buttonWithType:UIButtonTypeCustom];
    shitou.frame=CGRectMake(170, 350, 35, 70);
    [shitou setImage:[UIImage imageNamed:@"shitou.gif"] forState:UIControlStateNormal];
    shitou.backgroundColor=[UIColor blueColor];
    shitou.tag=100862;
    [shitou addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:shitou];
    return YES;
}
-(void)onClick:(UIButton *)btn
{

    int  num=arc4random()%3;
    
    if (btn.tag==10086) {
        [btn3 setImage:[UIImage imageNamed:@"jiandao.gif"] forState:UIControlStateNormal];
        switch (num) {
            case 0:
                [btn1 setImage:[UIImage imageNamed:@"jiandao.gif"] forState:UIControlStateNormal];
                label.text=@"你居然猜的一样";
                break;
                
            case 1:
                [btn1 setImage:[UIImage imageNamed:@"shitou.gif"] forState:UIControlStateNormal];
                label.text=@"小猪你输了";
            break;
        
            case 2:
                [btn1 setImage:[UIImage imageNamed:@"bu.gif"] forState:UIControlStateNormal];
                label.text=@"小样儿你真棒";
                break;
        }
    }
    if (btn.tag==100861) {
        [btn3 setImage:[UIImage imageNamed:@"bu.gif"] forState:UIControlStateNormal];
        switch (num) {
            case 0:
                [btn1 setImage:[UIImage imageNamed:@"jiandao.gif"] forState:UIControlStateNormal];
                label.text=@"小猪你输了";
                break;
                
            case 1:
                [btn1 setImage:[UIImage imageNamed:@"shitou.gif"] forState:UIControlStateNormal];
                label.text=@"小样儿你真棒";
                break;
                
            case 2:
                [btn1 setImage:[UIImage imageNamed:@"bu.gif"] forState:UIControlStateNormal];
                label.text=@"你居然猜的一样";
                break;
        
    }

        
}
    if (btn.tag==100862) {
        [btn3 setImage:[UIImage imageNamed:@"shitou.gif"] forState:UIControlStateNormal];
        switch (num) {
            case 0:
                [btn1 setImage:[UIImage imageNamed:@"jiandao.gif"] forState:UIControlStateNormal];
                label.text=@"小样儿你真棒";
                break;
                
            case 1:
                [btn1 setImage:[UIImage imageNamed:@"shitou.gif"] forState:UIControlStateNormal];
                label.text=@"你居然猜的一样";
                break;
                
            case 2:
                [btn1 setImage:[UIImage imageNamed:@"bu.gif"] forState:UIControlStateNormal];
                label.text=@"小猪你输了";
                break;
        }
    }

        
}

- (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
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: