近期在朋友圈,朋友转了一个html小游戏【壹秒】,游戏的规则是:用户按住button然后释放,看谁能精准地保持一秒的时间。^_^刚好刚才在linuxserver上调试程序的时候server挂了,腾出点时间分享下前天自己做的这个小游戏。

话不多说,直接上图:

   

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="300" height="500" alt="">

呐,作为一个对android无比热爱的小伙伴,腾出一个小小时间做一个小小的小游戏,也不失为一种享受哈。整个游戏比較简单,主要分为UI设计与开发和时间计算逻辑。当然,这个小游戏界面要做得美丽点,所以我側重讲下UI设计与开发。

UI设计与开发

採用RelativeLayout相对布局来控制整个界面比較灵活。主要的布局相信对你们来说都非常easy,都是简单的排版。

主要在于按住“按住”button之后怎么实现布局变长。这里提一下。我的思路是显示秒数布局一開始包括了所有所须要的空间。这个布局採用高度随内容增长,然后写一个高度10dp的view把高度撑起来。然后其它直接隐藏,后面依据逻辑直接显示就好。

以下是我的xml布局代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<!-- android:background="@drawable/bg"--> <!-- <TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="one second game。ready GO。"
android:textSize="22sp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#000000"/>--> <LinearLayout
android:id="@+id/linearLayout"
android:layout_marginTop="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:id="@+id/tip_l"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="你的"
android:textSize="50sp"
android:gravity="right"
android:textColor="#666666"
android:textStyle="bold"/> <TextView
android:id="@+id/tip_r"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.3"
android:text="一秒钟"
android:textSize="50sp"
android:textColor="#FF3333"
android:gravity="left"
android:textStyle="bold"
/>
</LinearLayout> <TextView
android:layout_marginTop="3dp"
android:id="@+id/tip_b"
android:paddingLeft="8dp"
android:layout_below="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="究竟有多长?"
android:textSize="40sp"
android:textColor="#666666"
android:textStyle="bold"
android:gravity="center"/> <TextView
android:layout_marginTop="15dp"
android:id="@+id/tip_e"
android:layout_below="@+id/tip_b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="How long is one second?"
android:textSize="15sp"
android:textColor="#666666"
android:textStyle="bold"
android:gravity="center"/> <LinearLayout
android:layout_below="@+id/tip_e"
android:id="@+id/show_linearlayout"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="3dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/show"
android:orientation="vertical"> <TextView
android:layout_width="fill_parent"
android:layout_height="10dp"/> <LinearLayout
android:layout_marginTop="10dp"
android:id="@+id/show_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"> <TextView
android:id="@+id/show_result_l"
android:layout_width="0dp"
android:layout_weight="1.7"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="#CC9933"
android:textSize="40sp"/> <TextView
android:layout_marginLeft="3dp"
android:id="@+id/show_result_r"
android:text="秒"
android:gravity="left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="25sp"/>
</LinearLayout> <TextView
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:id="@+id/show_remark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textColor="#ffffff"
android:textSize="15sp"
android:layout_marginBottom="20dp"/> </LinearLayout> <Button
android:layout_below="@+id/show_linearlayout"
android:layout_centerHorizontal="true"
android:background="@drawable/button"
android:layout_marginTop="20dp"
android:id="@+id/cul_bt"
android:layout_height="120dp"
android:layout_width="120dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="按住"
android:textColor="#ffffff"
android:textSize="30sp"/>
</RelativeLayout>

在UI界面中。为了更加美化增加了特殊字体。我加的是“方体卡通”和“华文彩云”字体。在android中显示特殊字体的方法是:假设你的IDE是eclipse,那么直接放在assets中(方法在此我不多介绍),我的IDE是android studio。假设你的IDE和我是一样的,能够使用下面方法:

首先在app→src→main文件夹下新建assets包。然后在自己项目的.iml文件里加入

<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />

之后在自己的代码中进行引用

private Typeface fangtikatong,huawencaiyun;//方体卡通,华文彩云
// 将字体文件保存在assets文件夹下。www.linuxidc.com创建Typeface对象
fangtikatong = Typeface.createFromAsset(getAssets(),"fangtikatong.ttf");
huawencaiyun = Typeface.createFromAsset(getAssets(),"huawencaiyun.TTF");
result_r.setTypeface(fangtikatong);
tip_l.setTypeface(huawencaiyun);

时间计算逻辑

时间计算逻辑比較简单,首先对button实现触摸监听,设置两个变量记录手摁下那一刻和离开那一刻(同个触摸过程)的系统时间。后者减去前者再进行单位换算就能够算出手触碰的持续时间。

代码例如以下:

click_cl.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){
x1 = (long)System.currentTimeMillis();
//show = new StringBuffer("第一次按下"+'\n'+x1+'\n');
} if(event.getAction() == MotionEvent.ACTION_UP){
x2 = (long)System.currentTimeMillis();
if(result_l.getVisibility() == View.GONE ||
result_l.getVisibility() == View.GONE ||
remark.getVisibility() == View.GONE ){
result.setVisibility(View.VISIBLE);
result_l.setVisibility(View.VISIBLE);
result_r.setVisibility(View.VISIBLE);
remark.setVisibility(View.VISIBLE);
}
num = (x2-x1)*1.000/1000;
result_l.setText(num+"");
//小于或等于1的先乘以1000再除以200
if(num <= 1){
remark.setText(remarks[(int)((num*1000)/200)]);
//大于1的先乘以1000再除以200
}else{
remark.setText(remarks[(int)((num*1000+200)/200)]);
}
}
return true;
}
});

到此,整个小游戏基本开发完毕,是不是也挺简单的呢?

热爱android的你们,空暇的时候也能够尝试开发自己的小游戏呢?

CSDN游戏代码下载:一秒游戏源代码

github游戏代码下在:一秒游戏源代码

近期微信上非常火的小游戏【壹秒】android版——开发分享的更多相关文章

  1. 如何在CentOS上安装一个2048小游戏

    如何在centos上安装一个2048小游戏 最近在学习CentOS系统,就琢磨着玩点什么,然后我看到有人在玩2048小游戏,所有我就在想,为啥不装一个2048小游戏搞一下嘞,于是乎,我就开始工作啦 由 ...

  2. 微信小程序基础之在微信上显示和体验小程序?

    随着小程序正式上线,用户现在可以通过二维码.搜索等方式体验到开发者们开发的小程序了. 用户只要将微信更新至最新版本,体验过小程序后,便可在发现页面看到小程序TAB,但微信并不会通过这个地方向用户推荐小 ...

  3. 【C语言程序设计】—最近超火的小游戏—【数字炸弹】!

    ✍  准备工作和建议 一.程序的原理 在动手编程之前,得先跟大家说一下这个程序是干什么的. 我们可以称呼这个游戏为<数字炸弹>. 游戏的原理是这样: 每一轮电脑从 1 到 100 中随机抽 ...

  4. 基于jQuery的2048小游戏设计(网页版)

    上周模仿一个2048小游戏,总结一下自己在编写代码的时候遇到的一些坑. 游戏规则:省略,我想大部分人都玩过,不写了 源码地址:https://github.com/xinhua6/2048game.g ...

  5. “限时分享“ 本地80个小游戏 HTML+CSS+JS源码分享

    ​ 里面有80款小游戏源码,支持内置导航,可以拿来练手或者消磨时间,具体功能以及游戏请看下图 ​ ​ ​ ​ ​ ​ ​ ​ 维京战争小游戏源码 链接:https://pan.baidu.com/s/ ...

  6. 开发腾讯移动游戏平台SDK Android版Ane扩展 总结

    本文记录了在开发 腾讯移动游戏平台SDK(MSDK) Android版Ane扩展 过程中所遇到的问题和相关解决方式 问题一:编译报错:Unable to resolve target 'android ...

  7. 2048小游戏(C语言版)

    #include <climits> #include <cstdio> #include <cstring> #include <stack> #in ...

  8. 别踩白块儿游戏源码Android版

    这个项目有带说明文档,大家可以看看源码附件的说明文档吧,“别踩白块儿”是目前非常火的一款游戏,游戏非常简单刺激.关于具体怎么火法怎么玩我就不多说了,相信看到本文的朋友们都非常地清楚. 什么游戏火,我们 ...

  9. 【转】微信小游戏接入Fundebug监控

    在SegmentFault上看到Fundebug上线小游戏监控,刚好最近开始玩微信小游戏,于是尝试接入试了一下. 接入方法 创建项目的时候选择左下角的微信小游戏图标. 点击继续进入接入插件页面. 第三 ...

随机推荐

  1. CSS3中的border-radius兼容IE低版本解决方法

    ie-css3.htc先说道说道这斯是弄啥嘞ie-css3.htc是一个可以让IE浏览器支持部份CSS3属性的htc文件,不只是box-shadow,它还可以让你的IE浏览器支持圆角属性border- ...

  2. C语言遇到的小问题

    堆栈区内存大小,2M或者4M,如果char够用,你就按照char去开,就只有int的1/4啦 printf("%s",s[i])不能运行 case 's': s = va_arg( ...

  3. [译] Pandas中根据列的值选取多行数据

    # 选取等于某些值的行记录 用 == df.loc[df['column_name'] == some_value] # 选取某列是否是某一类型的数值 用 isin df.loc[df['column ...

  4. 清除Jquery动画的队列

    当我们在写页面效果时,有时希望当鼠标放到某个元素上,这时会有动态的效果,当鼠标移出时效果会消失.但实际中,如果快速的用鼠标指向元素并移出,反复几次.即便鼠标不再指向这个元素,但这个元素会不停的重复着动 ...

  5. spl_autoload_register和__autoload

    1.实例化一个未定义的类时会触发 2.类存在继承关系时,被继承的类没有引入的情况下,会触发 (继承关系的两个类必须在同一个目录下)  __autoload 实例化PRINTIT类,'PRINTIT'作 ...

  6. 都系坤坤-微信助手V 0.1,解放双手发信息

    端午节刚过,相信大家在端午节都收到不少微信祝福信息,有复制长篇大论的祝福语群发的,有人工手打的简单祝福群发,我更喜欢人工手打带上称呼的祝福信息,这样看起来更加亲切. 那么问题来了,当你的通讯录里好友多 ...

  7. 【CCF】无线网络 搜索+思维

    #include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...

  8. bzoj3238 [Ahoi2013]差异 后缀数组+单调栈

    [bzoj3238][Ahoi2013]差异 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Ou ...

  9. WebRTC 介绍 (转)

    google开源了WebRTC项目,网址是:http://code.google.com/p/webrtc/. WebRTC实现了基于网页的视频会议,标准是WHATWG 协议,目的是通过浏览器提供简单 ...

  10. 使用 IntelliJ IDEA 开发一般 Java 应用程序时配置 Allatori 进行代码混淆

    使用 IntelliJ IDEA 开发一般 Java 应用程序时通过 Allatori 进行代码混淆非常容易配置,下面总结一下本人经验,介绍一下配置方法. 首先在 IDEA 的 Module 所在硬盘 ...