最近微信上很火的小游戏【壹秒】android版——开发分享
来源:互联网 发布:saber大邪神手办淘宝 编辑:程序博客网 时间:2024/04/29 17:28
最近在朋友圈,朋友转了一个html小游戏【壹秒】,游戏的规则是:用户按住按钮然后释放,看谁能精准地保持一秒的时间。^_^刚好刚才在linux服务器上调试程序的时候服务器挂了,腾出点时间分享下前天自己做的这个小游戏。
话不多说,直接上图:
呐,作为一个对android无比热爱的小伙伴,腾出一个小小时间做一个小小的小游戏,也不失为一种享受哈。整个游戏比较简单,主要分为UI设计与开发和时间计算逻辑。当然,这个小游戏界面要做得漂亮点,所以我侧重讲下UI设计与开发。
UI设计与开发
采用RelativeLayout相对布局来控制整个界面比较灵活。基本的布局相信对你们来说都很easy,都是简单的排版。主要在于按住“按住”按钮之后怎么实现布局变长,这里提一下。我的思路是显示秒数布局一开始包含了全部所需要的空间,这个布局采用高度随内容增长,然后写一个高度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>
首先在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);
时间计算逻辑
时间计算逻辑比较简单,首先对按钮实现触摸监听,设置两个变量记录手摁下那一刻和离开那一刻(同个触摸过程)的系统时间,后者减去前者再进行单位换算就可以算出手触碰的持续时间。
代码如下:
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游戏代码下在:一秒游戏源码
2 0
- 最近微信上很火的小游戏【壹秒】android版——开发分享
- 微信上最近奥运会很火很炫的广告是怎么实现的
- 基于cocos2d开发的android小游戏——采花仙
- 微信上抽奖的界面
- 分享我的第一个Android小游戏——猜拳游戏
- Android项目开发—2048小游戏
- Lua小游戏别撞车——接入微信分享
- Android—拼图小游戏
- 微信上流传的offer待遇档次
- 微信上沉默的“僵尸关系”
- 获取用户微信上的所在地
- 分享:如何开发出成功的iOS小游戏?
- cocos2d-x开发的《派对小游戏》-github源码分享
- Java小游戏——是男人就坚持20秒
- Android 2048小游戏开发
- 基于腾讯地图的高校LBS签到在微信上的应用开发应用实践效果展示
- 【Android小游戏】安卓小游戏开发--拼图
- 给大家分享一下最近开发遇到的一些坑
- VPN服务器配置详解(二)
- C++获取系统时间如何插入到MySQL里面的datetime型属性中
- 以ValueProvider为核心的值提供系统2: DictionaryValueProvider
- MapReduce的模式、算法和用例
- 数据结构之数组创建及操作
- 最近微信上很火的小游戏【壹秒】android版——开发分享
- MySQL中having 与where 的异同点
- shell编程
- 有关于android 定位和地图功能
- 为什么百度首页的HTML源码最后一行要多一行?浪费空间呀!
- spark RDD API详解
- 以ValueProvider为核心的值提供系统3: ValueProviderFactory
- iOS面试题
- 2012年5月SAT香港真题解析