您的位置:首页 > 移动开发 > IOS开发

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

2015-10-15 08:23 465 查看

初学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 = @"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 游戏 objective-c