当前位置: 首页 > news >正文

看博客学学Android(二十二)

原文地址:

Day 20: Weekly challenge. Persistent player data. Java date woes.

Weekly challenge will be to collect some amount of stars during a week and get some nice reward, like 8 atomic bombs, 5 shields or similar. I made a nice gold star in Gimp. I tried different particle effects on it and also some diffused star-light, but it did not look really good. So I went back to the particle effect used for powerups and tweaked that until I got something distinct for the star. Stars show up on their own pacing, so you can have both a star and a powerup on the screen at the same time.

Weekly Challenge

I also worked on loading and saving player data. It was much easier than I expected. I expected to have to learn some Android data storage API, but for simple key-value storage, libGDX provides the Preferences class. Just init with:

Preferences prefs = Gdx.app.getPreferences("DroneInvaders");

and then use get("key", defaultValute) and set(key, value) to read and write the values.

The only thing I had problems with are the dates. To keep track of daily and weekly challenges, the game stores the date of last play. When player launches the game, it compares that and resets some counters. Theoretically, I could prevent players from changing the system's calendar to past date, but I don't want to. What I am doing, is setting new set of daily and weekly challenges when day rolls over and resetting the counters for number of stars and letters picked up.

To make that work, I had to get the day difference between the previous play and current date. It matters whether it's the same day, exactly one day apart or more than one day. Googling got me to various websites and StackOverflow questions. Answers are funny. Many programmers simply calculate difference in seconds and then divide by 60 * 60 * 24 to get the days, completely ignorant about issues with daylight savings and leap seconds. One could argue that it does not matter that much for a game, but still getting many bug reports twice a year doesn't sound like fun to me. Some other guys simply count the days by adding one-by-one from start until they reach the end. While those loops might look correct, they still miss sometimes as they do not take the time out of the calculation. If one object stored 01.Jan at 5am, and you calculate it vs 02.Jan at 23pm, adding one day to first object is still less than the second, so they add two and get 2 day difference.

One of the tricks I use in this situation is to always set the time of the earlier Date to be something like 10am, and set the time for the later Date object to 5pm. Since daylight changes always happen at night, this is safe. And we also have 7 hours in between, just in case someone in future decides to do daylight saving changes in the middle of the day.

 

第20天:周挑战。可持续的用户数据。Java日期问题

每周的挑战将是收集这一周中的一些星星并且获得不错的奖赏,像8个原子弹,5个盾牌或者类似的东西。我使用Gimp制作了一个很好看的星星。我尝试在它上面是用不同的微粒效果并且也进行一些星光的扩散效果,但是效果真的不好.所以我重新换回使用微粒效果并且一直调整到星星很明显。星星以一个固定的速度显示出来,所以在屏幕上你可以同时看到一个星星和一个能量条。

Weekly Challenge

同时我也在进行游戏数据的加载和保存的工作。它要比我想的要容易很多。我还以为要去学习一些安卓的数据存储API,但是libGDX提供了一个接口类来存储简单的的键值对.仅仅需要如此初始化:

Preferences prefs = Gdx.app.getPreferences("DroneInvaders");

然后使用get("key", defaultValute) and set(key, value)的方式来读取和写入数据。

对我来说唯一的问题是日期。为了保持对日常挑战和周挑战的追踪,游戏存储了上一次玩的日期。当用户运行游戏,它会进行比较并且重置一些计数器。理论上,我能够阻止玩家将系统日历改为之前的日期,但是我不想这么做。我能做的是设,当日期回滚后,设置一个新的日常挑战周挑战并且重置星星和捕获的星星的数量。

为了如此,我需要获得之前游戏的日期和当前日期的不同。重要的是无论是否是同一天,都能精确的分辨出是不是超过了一天。通过谷歌查找了各种网站和一些论坛提问。答案是很有有趣的。许多的程序员只是简单的来计算相差的秒数,然后通过除以60*60*24来获得天数,完全忽略了夏时制和闰秒。有人会说这对游戏来说无关紧要,但是一年两次的获得很多bug报告对我来说是不好的事情。另外的一些童鞋只是简单通过从开始一个一个的加到结束来统计天数。尽管他们的循环看起来很正确,但是他们依旧遗忘了一些东西,比如他们不能控制时间溢出。如果一个一个对象存储的是1月1日上午5点,然后和1月2日晚上23点进行比较,给第一个对象增加一天依旧会小于第二个对象,所以他们会增加2天,最后获得2天的差距。

我在这里使用的一个策略是一直设置早一点的时间在10点左右,然后设置晚一点的时间在下午五点左右,因为夏时制一个发生在晚上,所以这样是安全的。并且在这之间我们依旧有7个小时,仅仅是为了预防万一在将来夏时制发生在中午.

涨姿势:

夏时制(Daylight Saving Time:DST):又称“日光节约时制”和“夏令时间”,是一种为节约能源而人为规定地方时间的制度,在这一制度实行期间所采用的统一时间称为“夏令时间”。一般在天亮早的夏季人为将时间提前一小时,可以使人早起早睡,减少照明量,以充分利用光照资源,从而节约照明用电。各个采纳夏时制的国家具体规定不同。目前全世界有近110个国家每年要实行夏令时。 自2011年3月27日开始俄罗斯永久使用夏令时,把时间拨快一小时,不再调回。

闰秒:是指为保持协调世界时接近于世界时时刻,由国际计量局统一规定在年底或年中(也可能在季末)对协调世界时增加或减少1秒的调整。由于地球自转的不均匀性和长期变慢性(主要由潮汐摩擦引起的),会使世界时(民用时)和原子时之间相差超过到0.9秒时,就把世界时向前拨1秒(负闰秒,最后一分钟为59秒)或向后拨1秒(正闰秒,最后一分钟为61秒); 闰秒一般加在公历年末或公历六月末。2012年3月,中科院国家授时中心宣布我国7月1日进行闰秒调整,届时将现7:59:60。

 

 

转载于:https://www.cnblogs.com/NorthDrift/p/3440520.html

相关文章:

  • Jquery重新学习之一[加载与属性html(),text(),val()]
  • Ubuntu13.10安装仿苹果启动菜单Cairo-Dock
  • Linux awk 命令 说明
  • 数组资源
  • html取出指定div的内容(不怕嵌套)
  • (转)Linux整合apache和tomcat构建Web服务器
  • 同一台Windows机器中启动多个Memcached服务
  • WebSphere MQ 入门指南
  • Glusterfs3.3.1DHT(hash分布)源代码分析
  • 进入保护模式(一)
  • SmartWatch2开发-ControlSample分析
  • 回车和换行
  • [Jquery] 实现鼠标移到某个对象,在旁边显示层。
  • 【转】Navigation Drawer(导航抽屉)
  • python的zipfile模块记录
  • Computed property XXX was assigned to but it has no setter
  • ES6系列(二)变量的解构赋值
  • Flannel解读
  • Laravel 实践之路: 数据库迁移与数据填充
  • pdf文件如何在线转换为jpg图片
  • Transformer-XL: Unleashing the Potential of Attention Models
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 分享几个不错的工具
  • 两列自适应布局方案整理
  • 实现简单的正则表达式引擎
  • 使用docker-compose进行多节点部署
  • 首页查询功能的一次实现过程
  • kubernetes资源对象--ingress
  • ​草莓熊python turtle绘图代码(玫瑰花版)附源代码
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • %@ page import=%的用法
  • (13):Silverlight 2 数据与通信之WebRequest
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (论文阅读11/100)Fast R-CNN
  • (三)uboot源码分析
  • (转)winform之ListView
  • (转)大道至简,职场上做人做事做管理
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • ./configure、make、make install 命令
  • .NET Core 和 .NET Framework 中的 MEF2
  • .NET MVC第五章、模型绑定获取表单数据
  • :not(:first-child)和:not(:last-child)的用法
  • [4.9福建四校联考]
  • [C语言]——内存函数
  • [ES-5.6.12] x-pack ssl
  • [Flexbox] Using order to rearrange flexbox children
  • [Go WebSocket] 多房间的聊天室(五)用多个小锁代替大锁,提高效率
  • [HNOI2008]Cards
  • [Java] IDEA Scala环境搭建
  • [leetcode] 66. 加一
  • [LeetCode]Spiral Matrix
  • [NOIP2013]华容道
  • [NOSQL] Redis介绍