您的位置:首页 > 编程语言

2048小游戏部分代码,仅供参考哦

2017-10-24 21:37 323 查看

2048小游戏部分代码

GameView.java

package 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);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  安卓游戏开发 2048