2048小游戏部分代码,仅供参考哦
2017-10-24 21:37
323 查看
2048小游戏部分代码
GameView.javapackage com.example.d.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.GridLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * Created by duboxuan on 2017/7/7. */ public class GameView extends GridLayout { Box[][] boxarr = new Box[4][4]; List<Point> point = new ArrayList<>(); public GameView(Context context) { super(context); init(); } public GameView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public GameView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { Box b; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { b = new Box(getContext()); addView(b); boxarr[x][y] = b; } } random(); random(); random(); random(); random(); random(); this.setOnTouchListener(new OnTouchListener() { float x, y, offsetX, offsetY; @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: x = motionEvent.getX(); y = motionEvent.getY(); break; case MotionEvent.ACTION_UP: offsetX = motionEvent.getX() - x; offsetY = motionEvent.getY() - y; if (Math.abs(offsetX) > Math.abs(offsetY)) { //水平 if (offsetX < 0) { left(); } else if (offsetX > 0) { right(); } } else { //垂直 if (offsetY < 0) { top(); } else if (offsetY > 0) { botton(); } } break; } return true; } }); } //生成随机数的方法 private void random() { point.clear(); for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (boxarr[x][y].getNumber() <= 0) { point.add(new Point(x, y)); } } } Point p = point.remove((int) (Math.random() * point.size())); boxarr[p.x][p.y].setNumber(Math.random() > 0.2 ? 2 : 4); } private void left() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { for (int x1 = x + 1; x1 < 4; x1++) { if (boxarr[x1][y].getNumber() > 0) { if (boxarr[x][y].getNumber() <= 0) { boxarr[x][y].setNumber(boxarr[x1][y].getNumber()); boxarr[x1][y].setNumber(0); x--; break; } else if (boxarr[x][y].equals(boxarr[x1][y])) { boxarr[x][y].setNumber(boxarr[x][y].getNumber() * 2); boxarr[x1][y].setNumber(0); break; } } } } } } private void top() { } private void right() { } private void botton() { } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2048" /> <com.example.d.myapplication.GameView android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="4" android:rowCount="4"> </com.example.d.myapplication.GameView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Box.java
package com.example.d.myapplication; import android.content.Context; import android.support.annotation.NonNull; import android.view.Gravity; import android.widget.FrameLayout; import android.widget.TextView; /** * Created by Administrator on 2017/7/7. */ public class Box extends FrameLayout { private int number = 0; private TextView box; public void setNumber(int number) { this.number = number; if(number==0){ box.setText(""); }else{ box.setText(""+number); } } public int getNumber() { return number; } public boolean equals(Box b){ return b.getNumber()==getNumber(); } // public Box(Context context){ super(context); box = new TextView(getContext()); setNumber(0); LayoutParams lp = new LayoutParams(-1,-1); lp.setMargins(10,10,0,0); box.setGravity(Gravity.CENTER); box.setTextSize(50); addView(box,lp); } }
相关文章推荐
- XML读写部分代码,仅供参考!
- Android 小游戏2048 代码简单实现
- 粤嵌arm开发版2048小游戏c语言代码
- android小游戏——2048 核心功能的实现与简单的代码重构(2)
- jQuery的实现原理的模拟代码 -1 核心部分
- 2048网页小游戏
- Android_代码的工程分为三个部分
- 200行Python代码实现2048
- 谷歌代码规范中,部分常用的技巧
- HTML网页版2048小游戏
- 第四部分 程序编译与代码优化
- TensorFlow中cnn-cifar10样例输入部分代码详解
- 编程算法 - 部分和问题 代码(C)
- C# 结构体保存自定义文件 实例部分代码
- Firefly《暗黑世界》碎片合成部分代码
- 慢慢分析linux代码--启动部分
- qt简单界面更新代码(菜鸟级)(部分代码)
- 《阿里巴巴Android开发手册》v1.0.1更新,优化部分内容和示例代码
- JavaScript 2048 游戏实例代码(简单易懂)
- “Java核心技术代码点和代码单元”部分学习