初学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编程语言实现,并使用了UIKit框架来创建用户界面元素,如文本框、标签和按钮等。核心功能包括随机数生成、用户输入处理及结果显示。

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



