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 = @"
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 我是运营,我没有假期
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- 每个 Linux 游戏玩家都绝不想要的恼人体验
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 在 Fedora 上使用 Steam play 和 Proton 来玩 Windows 游戏
- Steam 让我们在 Linux 上玩 Windows 的游戏更加容易
- 如何使用 Steam Play 在 Linux 上玩仅限 Windows 的游戏
- 新一代iPad适配应用之游戏篇
- VB实现的《QQ美女找茬游戏》作弊器实例
- C#实现洗牌游戏实例
- C#实现的算24点游戏算法实例分析
- C#实现简单的井字游戏实例
- C++编写简单的打靶游戏
- C++实现基于控制台界面的吃豆子游戏
- 纯javascript实现的小游戏《Flappy Pig》实例