Amazed | 读取assets文件夹内的txt文件 | 绘图 | 加速度感应器 SensorManager |

一、概述

游戏规则:大理石圆球(Marble)由于重力加速度移动,当碰到黑色区域判定为输,碰到蓝色区域判定为赢,碰到灰色区域继续移动。

[效果图]

二、加载迷宫地图

(1)描述迷宫的数据

随着关卡的递增,迷宫样式也会增加复杂度。迷宫的样式是借助文档描述(txt文档)显示出来的。

例如上图样式是由以下的数据绘制出来的:

 

(2)读取assets文件夹内的txt文件

Android 系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里。/res 和/assets的不同点是,android不为/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路径和文件名。

下面的代码实现了从assets文件夹指定的文件中读取迷宫描述数据,保存至整型数组中。

 private static int[] mMazeData;

 

(3)绘图 View Canvas Paint OnDraw

Android绘图操作,通过继承View 实现,在onDraw 函数中实现绘图。invalidate和postInvalidate这两个方法是用来刷新界面的 ,调用这两个方法后,会调用onDraw事件,让界面重绘。

以下代码实现游戏界面的绘制和更新操作,gameTick()是根据加速度传感器获得的坐标值来重新为绘制参数赋值。


(4)Activity显示自定义View

确保只在运行状态访问加速度感应器服务。在onResume注册,在 onPause反注册。

“To ensure that you request service updates only while in the Running state, register for updates in onResume() and unregister in onPause() .”

三、加速度感应器的使用

手机屏幕向上(z轴朝天)水平放置的时侯,(x,y,z)的值分别为(0,0,10);
手机屏幕向下(z轴朝地)水平放置的时侯,(x,y,z)的值分别为(0,0,-10);
手机屏幕向左侧放(x轴朝天)的时候,(x,y,z)的值分别为(10,0,0);
手机竖直(y轴朝天)向上的时候,(x,y,z)的值分别为(0,10,0);
其他的如此类推,规律就是:朝天的就是正数,朝地的就是负数。 利用x,y,z三个值求三角函数,就可以精确检测手机的运动状态了。

 

以下代码的功能是,获得一个加速度感应器服务的管理者(Manager),创建一个监听(SensorListener)并实现其接口(onSensorChanged) ,这样,当事件触发事,服务管理者就能调用该回调函数(onSensorChanged)了。在这里,回调函数中实现了存储加速度感应坐标的功能。

四、判定输赢

在OnDraw函数里面,将小球所在的坐标转换为行号和列号,根据行号和列号算出所在的mMazeData数组的索引,求出索引值,1代表输、2代表赢、0代表继续移动。

 

参考

http://www.pin5i.com/showtopic-android-sensormanager-demo.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 1. 统计text中to的出现次数: 可以使用Python的字符串函数count()来实现: ```python text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with." count = text.count("to") print(count) ``` 输出结果为: ``` 5 ``` 2. 求每个句子(句号为准)各有多少个单词: 可以使用Python的字符串函数split()来将句子分割成单词,再统计单词数量: ```python text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with." sentences = text.split(".") for sentence in sentences: words = sentence.split() print(len(words)) ``` 输出结果为: ``` 8 20 21 12 13 ``` 3. 求出现次数最多的三个单词(不能包含逗号、句号): 可以使用Python的collections模块中的Counter来统计单词出现的次数,再取前三个出现次数最多的单词: ```python import re from collections import Counter text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with." words = re.findall(r'\b\w+\b', text) words = [word for word in words if word not in [",", "."]] counter = Counter(words) top_words = counter.most_common(3) print(top_words) ``` 输出结果为: ``` [('to', 5), ('Awareness', 2), ('the', 2)] ``` 其中,to出现了5次,Awareness和the各出现了2次。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值