周末闲来——2048游戏开发
上个星期杭州这边突然刮起台风,无奈回不了公司上班,所以窝在宿舍无聊,想起之前看过一些2048的视频还有教程,手动写了一个2048的小游戏,在此分享下。先上图看看效果:
这个游戏其实比较简单,重点的部分主要有3个:UI界面实现、游戏逻辑控制还有数据存储。当然完成的游戏中还有包括语音播放等等,这里不做具体的说明。
UI界面设计与实现
<span style="font-family:SimHei;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:layout_margin="10dp"><LinearLayoutandroid:layout_marginTop="20dp"android:layout_width="fill_parent"android:layout_height="60dp"android:orientation="horizontal"><LinearLayoutandroid:layout_height="fill_parent"android:layout_width="0dp"android:layout_weight="1"android:orientation="vertical"android:background="@drawable/sorce_bg"><TextViewandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:text="Sorce"android:gravity="center"android:textSize="20sp"android:textColor="#FFF8DC"/><TextViewandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:id="@+id/id_sorce"android:gravity="center_horizontal"android:textSize="20sp"android:textColor="#ffffff"android:text="0" /></LinearLayout><LinearLayoutandroid:layout_height="fill_parent"android:layout_width="0dp"android:layout_weight="0.1"android:orientation="vertical"></LinearLayout><LinearLayoutandroid:layout_height="fill_parent"android:layout_width="0dp"android:layout_weight="1"android:orientation="vertical"android:background="@drawable/sorce_bg"><TextViewandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:text="MaxSorce"android:gravity="center"android:textSize="20sp"android:textColor="#FFF8DC" /><TextViewandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:id="@+id/id_maxsorce"android:gravity="center_horizontal"android:textSize="20sp"android:textColor="#ffffff"android:text="0" /></LinearLayout></LinearLayout><LinearLayoutandroid:layout_marginTop="10dp"android:layout_width="fill_parent"android:layout_height="40dp"android:orientation="horizontal"><Buttonandroid:layout_margin="5dp"android:background="@drawable/bt_sound"android:id="@+id/id_sound"android:layout_width="30dp"android:layout_height="30dp"/><Buttonandroid:background="@drawable/bt_start"android:layout_marginLeft="10dp"android:id="@+id/id_newStart"android:layout_width="0dp"android:layout_weight="1"android:layout_height="fill_parent"android:text="New Game"android:textSize="15sp"android:textColor="#ffffff"/></LinearLayout><com.example.yummy.game2048.GameViewandroid:layout_marginTop="10dp"android:id="@+id/id_gameView"android:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"/></LinearLayout>
</span>
<span style="font-family:SimHei;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" ><gradientandroid:endColor="#bbada0"android:startColor="#bbada0" /><cornersandroid:bottomLeftRadius="8dp"android:bottomRightRadius="8dp"android:topLeftRadius="8dp"android:topRightRadius="8dp" /><strokeandroid:width="1dip"android:color="#bbada0" /></shape>
</span>
个人认为,学习Android开发selector和shape样式定义是必须学会的,这有利于我们开发各式各样的界面布局。
游戏逻辑控制
布置4x4布局
<span style="font-family:SimHei;font-size:18px;">package com.example.yummy.game2048;import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;/*** Created by yummy on 2015/7/11.*/
public class Card extends FrameLayout{private int number = 0;private TextView tv;public Card(Context context) {super(context);tv = new TextView(getContext());tv.setTextSize(32);tv.setGravity(Gravity.CENTER);tv.setTextColor(0xffffffff);tv.setBackgroundColor(0xffbbada0);LayoutParams layoutParams = new LayoutParams(-1,-1);layoutParams.setMargins(10,10,0,0);addView(tv,layoutParams);}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;if(number == 0){tv.setText("");}else{tv.setText(number+"");}}@Overridepublic boolean equals(Object o) {return this.getNumber()==((Card)o).getNumber();}
}
</span>
其中重写equal方法主要是为后面合并两个相同的子布局做铺垫。然后在GameView初始化的时候进行16个子布局添加形成游戏布局。代码如下:
<span style="font-family:SimHei;font-size:18px;"> //初始化布局Card card;for( int y = 0; y < 4; y++){for(int x = 0; x < 4; x++){card = new Card(getContext());card.setNumber(0);addView(card,cardSideLenght,cardSideLenght);//添加在记录中cards[x][y] = card;}}</span>
别忘了要设置4列,不然就形成不了4x4方阵
<span style="font-family:SimHei;font-size:18px;"> //设置4列setColumnCount(4);</span>
重写手势监听
<span style="font-family:SimHei;font-size:18px;">//设置手势监听setOnTouchListener(new View.OnTouchListener() {private float startX ,startY,offsetX,offsetY;@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()){case MotionEvent.ACTION_DOWN:startX = event.getX();startY = event.getY();break;case MotionEvent.ACTION_UP:offsetX = event.getX() - startX;offsetY = event.getY() - startY;//如果x轴偏移比y轴偏移大,则认为x轴有滑动行为而y轴没有滑动行为if(Math.abs(offsetX) > Math.abs(offsetY)){if(offsetX < -5){toLeft();}else if(offsetX > 5){toRight();}else{//滑动距离太小}}else{if(offsetY < -5){toUp();}else if(offsetY > 5){toDowm();}else{//滑动距离太小}}break;}return true;}});</span>
<span style="font-family:SimHei;font-size:18px;"><span style="color:#5a5a5a;"> for(int y = 0; y < 4; y++){for(int x = 0; x < 4; x++){for(int x1 = x+1; x1 < 4; x1++){if(cards[x1][y].getNumber() > 0){//当前位置的值为0,则应该把后面的值放到当前位置上去if(cards[x][y].getNumber() ==0){cards[x][y].setNumber(cards[x1][y].getNumber());cards[x1][y].setNumber(0);</span><span style="color:#ff0000;"> x--;</span><span style="color:#5a5a5a;">flag =true;mediaPlayer2.start();}else if(cards[x][y].equals(cards[x1][y])){cards[x][y].setNumber(cards[x1][y].getNumber()*2);cards[x1][y].setNumber(0);//计分MainActivity.getMainActivity().addSorce(cards[x][y].getNumber());flag =true;mediaPlayer1.start();}break;}}}}</span></span>
值得注意的是:当遍历点为0且右边格子不为0,此时合并之后需要再此进行重新遍历,为什么呢?举个例子,当有一行的格子数值为
数据存储
数据存储几乎是每个APP都必备的一个模块,2048的数据很少,只需要才每次安装的时候生成一个data文件来记录最高分即可。那么SharedPreferences就是首选。
申明SharedPreferences对象和SharedPreferences.Editor对象并进行get和put的操作就可以了,别忘记Editor的数据操作需要commit来完成哦。代码部分比较简单就不过多叙述。
2048游戏源码下载
github下载链接
CSDN下载链接
周末闲来——2048游戏开发相关推荐
- Cocos2d-x 3.x版2048游戏开发
Cocos2d-x 3.x版2048游戏开发 本篇博客给大家介绍如何快速开发2048这样一款休闲游戏,理解整个2048游戏的开发流程,从本篇博客你将可以学习到以下内容: 这里注明一下,本教程来自极客学 ...
- python实现2048游戏开发笔记
一.游戏描述 这里省略了(估计来看的人都玩过2048游戏) 二.游戏设计 首先将游戏分解成若干个局部一一实现 (1)输入规则:asdw分别代表左下由上,r为reset,q为退出,其他不执行. (2)输 ...
- android2048项目报告,Android项目开发实战-2048游戏
<2048>是一款比较流行的数字游戏,最早于2014年3月20日发行.原版2048首先在GitHub上发布,原作者是Gabriele Cirulli,后被移植到各个平台.这款游戏是基于&l ...
- 微信公众平台开发(100) 2048游戏
微信开发第100篇了,算上微信支付和微信小店,其实已经超过了,这次完整讲一下2048游戏吧. 一.2048游戏 <2048>是比较流行的一款数字游戏.原版2048首先在github上发布, ...
- Excel_VBA开发2048游戏教程——Einsphoton
VBA对游戏开发的流程帮助甚微,甚至影响游戏开发效率,本应用实例仅为消遣,切勿过分关注! 前言 抱歉,这可能是我最后的几篇文章之一了. 由于最近工作中遇到很多问题,作者现在处于自我检讨中,恐怕以后将要 ...
- 华为鸿蒙2048小游戏,《从零开发鸿蒙小游戏App》直播答疑(包含新版2048游戏代码)...
请到附件中下载新版2048游戏代码.最好能对该代码再做一些优化(见下述问题8),欢迎感兴趣的朋友随时跟我探讨,^_^ 问题1:如何将开发的Lite Wearable项目部署在鸿蒙手表Watch GT2 ...
- 基于android的2048游戏设计,基于Android平台的2048游戏设计与开发.docx
基于Android平台的2048游戏设计与开发 高级操作系统论文(设计) 题 目 基于Android个人2048 小游戏的设计与开发姓 名 学 号 院. 系 信息学院 专 业 指导教师 职称(学历)目 ...
- 基于粤嵌gec6818开发板嵌入式开发电子相册,音乐播放,视频播放,2048游戏
一.功能与要求 实现功能:本系统需要使用粤嵌的GEC-6818开发板设计一款娱乐影音系统,其中包括图片显示(相册).音乐播放.视频播放,游戏四个部分,在每个部分内部,具有操控各个部分的功能触摸按键.本 ...
- 使用python开发网页游戏_不敢想!不敢想!我用Python自动玩转2048游戏
近来在折腾selenium自动化, 感觉配合爬虫很有意思, 大多数以前难以模拟登录的网站都可以爬了,折腾了这么久,于是想自动玩个2048游戏!嘿嘿, 我是一个不擅长玩游戏的人, 以前玩2048就经常得 ...
- LibGDX_8.1: LibGDX 项目实战: 开发跨平台 2048 游戏
本文链接: http://blog.csdn.net/xietansheng/article/details/50188259 LibGDX 基础教程(总目录) 声明: 游戏中使用到的部分图片和音频资 ...
最新文章
- div文字自动扩充_文字资料扩充
- php服务划分,云计算提供的服务分为哪三个层次
- mysql主从配置错误_mysql主从配置失败,主从通讯失败
- LeetCode 402. 移掉K位数字(贪心,单调栈)
- redhat linux yum仓库,关于RHEL6发行版yum仓库的配置
- 从 VC7 的 CHtmlView 不能正常退出谈 CComPtr 使用中的一个误区
- Web前端笔记-画布拖动及放缩(two.js)
- risc 服务器 操作系统,数据中心系统用RISC还是CISC?
- JAVA输入输出IO流→File、RandomAccessFilse、字节流InputSream与OutputStream、字符流Reader与Writer、对象序列化Serializable
- BlackBerry 9520上结合139邮箱实现Gmail邮件的短信通知
- 【三维路径规划】基于matlab RRT算法无人机三维路径规划【含Matlab源码 155期】
- 将element-china-area-data获取的地区编码转为文字存储
- 【历史上的今天】1 月 23 日:现代集成电路雏形;JDK 1.0 发布;数学大师诞生
- 第十届蓝桥杯大赛个人赛省赛(软件类本科B组)做题笔记Partial
- AS3多线程快速入门(三):NAPE物理引擎+Starling[译]
- angr入门之CLE
- 110种有趣的游戏和应用
- java学习笔记—java的学习路线
- 空前绝后!PostgresConf.CNPGConf.Asia 2020大会闭幕
- UCOSII MailBox