IOS:oc制作的猜数字小游戏

初学ios,做了一个极其简单的小游戏,来看一下:

用了一个类ZYAppDelegate.h:

#import <UIKit/UIKit.h>

@interface ZYAppDelegate : UIResponder <UIApplicationDelegate>


{
    int a;
    UITextField * inputk;
    UILabel *show;
    UILabel *face;
    int n;
    UILabel *count;
}

@property (strong, nonatomic) UIWindow *window;

- (void)compare;

@end
算法的核心当然是随机数,在屏幕的文本框获取字符串来转换成浮点型或整形的用来计算,反过来转换输出在屏幕上,

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];
    
    a = arc4random()%100;
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake (0,0,320,480)];
    UIImage *image = [UIImage imageNamed:@"02.jpg"];
    imageView.image = image;
    [self.window addSubview:imageView];
    
    UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(115, 40, 100, 30)];
    title.text = @"猜大小,小游戏";
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:15];
    [self.window addSubview:title];
    
    UILabel *input = [[UILabel alloc] initWithFrame:CGRectMake(30, 90, 70, 30)];
    input.text = @"请输入:";
    input.textAlignment = NSTextAlignmentLeft;
    input.font = [UIFont systemFontOfSize:15];
    [self.window addSubview:input];
    
    inputk = [[UITextField alloc] initWithFrame:CGRectMake(120, 90, 120, 30)];
    inputk.placeholder = @"在这里写数";
    inputk.borderStyle = UITextBorderStyleRoundedRect;
    inputk.backgroundColor = [UIColor grayColor];
    inputk.keyboardType = UIKeyboardTypeNumberPad;
    inputk.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.window addSubview:inputk];
    
    show = [[UILabel alloc] initWithFrame:CGRectMake(65, 150, 200, 170)];
    show.font = [UIFont systemFontOfSize:50];
    show.textAlignment = NSTextAlignmentCenter;
//    show.text = @"猜大了";
    [self.window addSubview:show];
    
    UIButton *reset = [UIButton buttonWithType:UIButtonTypeCustom];
    reset.frame = CGRectMake(170, 310, 50, 25);
    [reset setTitle:@"重置" forState:UIControlStateNormal];
    [reset setBackgroundColor:[UIColor grayColor]];
    reset.clipsToBounds = YES;
    reset.layer.cornerRadius = 6;
    [reset addTarget:self action:@selector(newOne) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:reset];
    
    UIButton *guess = [UIButton buttonWithType:UIButtonTypeCustom];
    guess.frame = CGRectMake(90, 310, 50, 25);
    [guess setTitle:@"猜数" forState:UIControlStateNormal];
    [guess setBackgroundColor:[UIColor grayColor]];
    guess.clipsToBounds = YES;
    guess.layer.cornerRadius = 6;
    [guess addTarget:self action:@selector(compare) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:guess];
    
    UILabel *hint = [[UILabel alloc] initWithFrame:CGRectMake(70, 350, 200, 30)];
    hint.text = @"(提示:数在0~99之间)";
    hint.font = [UIFont systemFontOfSize:20];
    [self.window addSubview:hint];
    
    face = [[UILabel alloc] initWithFrame:CGRectMake(250, 90, 30, 30)];
    face.font = [UIFont systemFontOfSize:15];
    [self.window addSubview:face];
    
    count = [[UILabel alloc] initWithFrame:CGRectMake(120, 280, 100, 20)];
//    int str = [self compare];
//
    count.font = [UIFont systemFontOfSize:15];
    [self.window addSubview:count];
    
    
    return YES;
}
- (void)compare
{
    if ([inputk.text isEqualToString:@""]) {
        return;
    }
    
    int sub =[inputk.text intValue];
//    n = -1;
    if (a > sub) {
        show.text = @"猜小了!";
        face.text = @"
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值