ios 简单的计时器游戏 NSUserDefaults NSDate NSTimer
2019独角兽企业重金招聘Python工程师标准>>>
1.NSUserDefaults是应用程序实现过程中只会存在单独一个的对象,故可以用它记录小型的数据如系统偏好设置等
2.初始化:
self.myuserDefaults = [NSUserDefaults standardUserDefaults];
3.取值:
[_myuserDefaults objectForKey:KMaxScore];
4.赋值:
[_myuserDefaults setObject:[NSNumber numberWithInt:0] forKey:KMaxScore];
5.NSDate是日期的数据类型
self.startDate = [NSDate date];//记录当前时间
6.计算间隔时间
self.timeUse = [_gameTimer.fireDate timeIntervalSinceDate:_startDate];
7.初始化计时器
self.gameTimer = [NSTimer scheduledTimerWithTimeInterval:_timeBetween
target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
8.计时器响应时间
self.timeUse = [_gameTimer.fireDate timeIntervalSinceDate:_startDate];
一个简单的计时器小游戏
#define KMaxScore @"KMaxScore"@interface ViewController ()<UIAlertViewDelegate>@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *scoreLabel;
@property (nonatomic, strong) UILabel *animationLabel;
@property (nonatomic, strong) UIButton *aButton;
@property (nonatomic, strong) UIAlertView *myAlertView;@property (nonatomic, strong) NSDate *startDate;
@property (nonatomic, strong) NSTimer *gameTimer;@property (nonatomic, assign) NSInteger timeBetween;//button出现的时间间隔@property (nonatomic, assign) NSInteger buttonX;
@property (nonatomic, assign) NSInteger buttonY;@property (nonatomic, assign) NSInteger score;
@property (nonatomic, assign) NSInteger timeUse;@property (nonatomic, strong) NSUserDefaults *myuserDefaults;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self initTheData];[self initTheGame];
}- (void)initTheData{//判断是否第一次启动游戏if ([_myuserDefaults objectForKey:KMaxScore] == nil) {NSLog(@"%@", [_myuserDefaults objectForKey:KMaxScore]);self.myuserDefaults = [NSUserDefaults standardUserDefaults];[_myuserDefaults setObject:[NSNumber numberWithInt:0] forKey:KMaxScore];
// NSLog(@"haha");}NSLog(@"%@", [_myuserDefaults objectForKey:KMaxScore]);//0
}- (void) initTheGame{UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];[bgImageView setImage:[UIImage imageNamed:@"djfoj.jpg"]];[self.view addSubview:bgImageView];
// NSLog(@"haha");
// self.timeUse = 0;self.animationLabel = [[UILabel alloc] init];self.timeBetween = 1;self.startDate = [NSDate date];self.gameTimer = [NSTimer scheduledTimerWithTimeInterval:_timeBetween target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];self.score = 0;self.scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 320, 40)];[_scoreLabel setText:[NSString stringWithFormat:@"得分:%d", _score]];[_scoreLabel setTextAlignment:NSTextAlignmentCenter];[self.view addSubview:_scoreLabel];self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 320, 60)];[_timeLabel setTextAlignment:NSTextAlignmentCenter];[self.view addSubview:_timeLabel];self.aButton = [UIButton buttonWithType:UIButtonTypeCustom];
}- (void)timerFired:(NSTimer *) timer{[_aButton setBackgroundImage:[UIImage imageNamed:@"Unlock_DotLock1_Selected"] forState:UIControlStateNormal];self.buttonX = arc4random_uniform(220) + 50;self.buttonY = arc4random_uniform(360) + 50;[_aButton setFrame:CGRectMake(_buttonX, _buttonY, 52, 52)];[_aButton addTarget:self action:@selector(buttonTouch:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:_aButton];self.timeUse = [_gameTimer.fireDate timeIntervalSinceDate:_startDate];[_timeLabel setText:[NSString stringWithFormat:@"剩余时间: %d", 61 - _timeUse]];if (_timeUse >= 56) {[_timeLabel setAlpha:1];[_timeLabel setTextColor:[UIColor redColor]];[_timeLabel setFont:[UIFont fontWithName:_timeLabel.font.fontName size:_timeLabel.font.pointSize + 3]];[UIView animateWithDuration:0.8 animations:^{[_timeLabel setAlpha:0];}];}if (_timeUse == 61) {if (_score > [[_myuserDefaults objectForKey:KMaxScore] intValue]) {[_myuserDefaults setObject:[NSNumber numberWithInt:_score] forKey:KMaxScore];}self.myAlertView = [[UIAlertView alloc] initWithTitle:@"游戏结束" message:
[NSString stringWithFormat:@"您的最高得分为:%d, 您本次的得分为:%d",
[[_myuserDefaults objectForKey:KMaxScore] intValue], _score]
delegate:self cancelButtonTitle:@"再玩一次" otherButtonTitles:@"退出游戏", nil];[_myAlertView show];[_gameTimer invalidate];}
}- (void)buttonTouch:(UIButton *)thisButton{_score++;[_scoreLabel setText:[NSString stringWithFormat:@"得分:%d", _score]];[_animationLabel setAlpha:1];[_animationLabel setFrame:CGRectMake(thisButton.frame.origin.x, thisButton.frame.origin.y, 40, 40)];[_animationLabel setBackgroundColor:[UIColor greenColor]];[_animationLabel setText:@"恭喜"];[_animationLabel setTextAlignment:NSTextAlignmentCenter];[_animationLabel setTextColor:[UIColor redColor]];[self.view addSubview:_animationLabel];[UIView animateWithDuration:1.0 animations:^{[_animationLabel setAlpha:0];}];
}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{if (buttonIndex == 0) {[self initTheGame];}else{NSLog(@"已经退出游戏");}
}@end
转载于:https://my.oschina.net/u/1999967/blog/303588
ios 简单的计时器游戏 NSUserDefaults NSDate NSTimer相关推荐
- 加速计简单使用---迷宫游戏
今天通过编写一个简单的迷宫游戏,来展示如何使用iPhone的内置加速计. 游戏效果如下图所示.用户通过上下左右摇晃屏幕控制这个橙色的pacman挪动,pacman撞到屏幕边缘或者墙壁(蓝色边框方块)会 ...
- python跑酷游戏源码_Phaser.js实现简单的跑酷游戏附源码下载
采用的物理引擎是Phaser.js 在这里对此引擎不做过多介绍(因为我也是小白,嘿嘿) 效果展示: 源码(详细源码图片资源可点击文章下方或屏幕右上方的github链接进行clone) 1.创建游戏舞台 ...
- C++编写简单的俄罗斯方块游戏
代码地址如下: http://www.demodashi.com/demo/14593.html C++编写简单的俄罗斯方块游戏 使用C++编写一个简单的俄罗斯方块游戏. 1 环境要求 使用C++图形 ...
- 03、三种简单的计时器
1.计时器在游戏中的使用次数很多,以下是三种简单的计时器写法 2.代码: 1 using System.Collections; 2 using System.Collections.Generic; ...
- 简单跑酷java代码_如何实现一个简单的跑酷游戏?(代码详解)
本篇文章给大家带来的内容是介绍如何实现一个简单的跑酷游戏?(代码详解).有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助. 采用的物理引擎是:Phaser.js 官网地址:http:// ...
- 简单的Windows游戏-第1部分:游戏框架
我已决定使用C#和WinForms创建一个简单的Windows游戏,从而得出一系列见解. 还有其他方法可以完成此任务,但我选择了使事情保持简单并演示如何制作游戏的方法. 更有经验的开发人员会注意到我的 ...
- 目前大部分的游戏框架_简单的Windows游戏-第1部分:游戏框架
目前大部分的游戏框架 我已决定使用C#和WinForms创建一个简单的Windows游戏,从而得出一系列见解. 还有其他方法可以完成此任务,但我选择了使事情保持简单并演示如何制作游戏的方法. 经验丰富 ...
- cocos2d-x 提升篇 (17) 简单的桌上足球游戏
这个例子改编来自Cocos2d-x by Example. 相当于一个简单的桌上足球游戏,可以通过触摸的方式碰撞红色的球,进入对方的球门就可以加一分. #ifndef _APP_DELEGATE_H_ ...
- 如何使用cocos2d-x 3.0来做一个简单的iphone游戏教程(第一部分)
游戏截图: cocos2d-x 是一个支持多平台的开源框架,用于构建游戏.应用程序和其他图形界面交互应用.Cocos2d-x项目可以很容易地建立和运行在iOS,Android的三星Bada,黑莓Bla ...
最新文章
- spring mvc框架设计与实现
- CentOS下Hive2.0.0单机模式安装详解
- Raft协议安全性保证
- php怎么传json数据_php和js如何通过json互相传递数据相关问题探讨
- C语言的参数传递原理解析(值传递)
- WIN10常用快捷键(打开资源管理器、显示桌面、截图)
- JS对数据进行判空操作
- 算法图解第八章笔记与习题(贪婪算法)
- 基于GPT2实现考公申论文章生成
- NowCoder错题
- 百度招聘Android客服端(1)
- 腾讯信鸽自定义推送通知
- Html源代码图片解密,通过图片加密、解密文件
- Hadoop Streaming 实战: 实用Partitioner类KeyFieldBasedPartitioner
- 【老生谈算法】matlab实现匈牙利算法源码——匈牙利算法
- 初识html5-当当网图书分类页面,图书添加页面 图书分类加载不出来
- ds6708 symbol 驱动_SymbolDS6708
- 飞鸽传书2008默认分类
- 火大,新浪微博封ID
- reverseorder_Java集合reverseOrder()方法及示例
热门文章
- 迁移学习比赛:OpenAI喊你重温「音速小子索尼克」
- 斯坦福吴恩达团队公布最大医学影像数据集
- 在硅谷谈AI不够有创意,到苹果开吐槽大会 | 跟着李开复去硅谷
- 那么多GAN哪个好?谷歌大脑泼来冷水:都和原版差不多 | 论文
- 《用户网络行为画像》读书笔记(二)
- 《AngularJS高级程序设计》——第2章 你的第一个AngularJS应用 2.1 准备项目
- CentOS 6.4 安装 media wiki 1.23.6(转)
- EBS_FORM_开发:关于切换不同BLOCK的时候弹出需要保存的窗口
- 重新编译hadoop-2.7.2-src的native以支持Snappy解压压缩库
- Linux下套接字详解(四)----简单的TCP套接字应用(迭代型)