就是圖片這樣,滑動又是有分數變化的。


代碼:
package com.example.administrator.game2048;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;
import java.util.ArrayList;
import java.util.List;
public class GameView extends GridLayout{
public GameView(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs, defStyle);
initGameView();
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
initGameView();
}
public GameView(Context context) {
super(context);
initGameView();
}
//游戲初始化方法
private void initGameView(){
setColumnCount(4);
//將面板設定成4列
setBackgroundColor(0xffbbada0);
System.out.println("initGameView");
setOnTouchListener(new View.OnTouchListener() {
/*
* startX:手機剛開始在螢屏上的X坐標
* startY:手機剛開始在螢屏上的Y坐標
* offsetX,offsetY,分別是手指在螢屏上的X,Y上的偏移量
*/
private float startX,startY,offsetX,offsetY;
@Override
public 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;
if(Math.abs(offsetX) > Math.abs(offsetY)){
if(offsetX < -5){
swipeLeft();
System.out.println("Left");
}else if(offsetX > 5){
swipeRight();
System.out.println("Right");
}
} else{
if(offsetY < -5){
swipeUp();
System.out.println("Up");
}else if(offsetY > 5){
swipeDown();
System.out.println("Down");
}
}
break;
}
return true;
}
});
}
//當布局的尺寸發生變化時,會執行該方法
protected void onSizeChanged(
int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int cardWidth = (Math.min(w, h)-10)/4;
addCards(cardWidth, cardWidth);
startGame();
}
//添加卡片的方法
private void addCards( int cardWidth,int cardHeight){
Card c;
//遍歷每一張卡片
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
c = new Card(getContext());
c.setNum(0);
addView(c, cardWidth, cardHeight);
cardsMap[x][y] = c;
}
}
}
//開始游戲的方法
public void startGame(){
MainActivity.getMainActivity().clearScore();
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
cardsMap[x][y].setNum(0);
emptyPoint.add(new Point(x,y));
}
}
addRandomNum();
addRandomNum();
}
//添加亂數
private void addRandomNum() {
emptyPoint.clear();
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
if (cardsMap[x][y].getNum()<=0) {
emptyPoint.add(new Point(x,y));
}
}
}
Point p = emptyPoint
.remove((int) (Math.random() * emptyPoint.size()));
cardsMap[p.x][p.y].setNum(Math.random() > 0.1 ? 2 : 4);
}
//向左滑動
public void swipeLeft(){
boolean meger = false;
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
for (int x1 = x+1; x1 < 4; x1++) {
if(cardsMap[x1][y].getNum()>0){
if(cardsMap[x][y].getNum()<=0){
/*
* 將下標為(x,y)所在位置的卡片上的數字
* 設定為,坐標為(x1,y)所在位置的卡片上的值;
* 第二步,將坐標(x1,y)所在位置的卡片上的數字
* 設定為0
* (即:變成空卡片)
*/
cardsMap[x][y].setNum(
cardsMap[x1][y].getNum());
cardsMap[x1][y].setNum(0);
x--;
meger =true;
break;
}else if(cardsMap[x][y].equals(cardsMap[x1][y])){
cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
cardsMap[x1][y].setNum(0);
MainActivity.getMainActivity().
addScore(cardsMap[x][y].getNum());
meger = true;
}
break;
}
}
}
}
if(meger){
addRandomNum();
checkComplete();
}
}
//向右滑動
public void swipeRight(){
boolean meger = false;
for (int y = 0; y < 4; y++) {
for (int x = 3; x >=0; x--) {
for (int x1 = x-1; x1 >= 0; x1--) {
if(cardsMap[x1][y].getNum()>0){
if(cardsMap[x][y].getNum()<=0){
/*
* 將下標為(x,y)所在位置的卡片上的數字
* 設定為,坐標為(x1,y)所在位置的卡片上的值;
* 第二步,將坐標(x1,y)所在位置的卡片上的數字
* 設定為0
* (即:變成空卡片)
*/
cardsMap[x][y].setNum(
cardsMap[x1][y].getNum());
cardsMap[x1][y].setNum(0);
x++;
meger =true;
break;
}else if(cardsMap[x][y].equals(cardsMap[x1][y])){
cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
cardsMap[x1][y].setNum(0);
MainActivity.getMainActivity().
addScore(cardsMap[x][y].getNum());
meger =true;
}break;
}
}
}
}
if(meger){
addRandomNum();
checkComplete();
}
}
//向上滑動
public void swipeUp(){
boolean meger = false;
for (int x= 0; x< 4; x++) {
for (int y = 0; y < 4; y++) {
for (int y1 = y+1; y1 < 4; y1++) {
if(cardsMap[x][y1].getNum()>0){
if(cardsMap[x][y].getNum()<=0){
/*
* 將下標為(x,y)所在位置的卡片上的數字
* 設定為,坐標為(x1,y)所在位置的卡片上的值;
* 第二步,將坐標(x1,y)所在位置的卡片上的數字
* 設定為0
* (即:變成空卡片)
*/
cardsMap[x][y].setNum(
cardsMap[x][y1].getNum());
cardsMap[x][y1].setNum(0);
y--;
meger =true;
break;
}else if(cardsMap[x][y].equals(cardsMap[x][y1])){
cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
cardsMap[x][y1].setNum(0);
MainActivity.getMainActivity().
addScore(cardsMap[x][y].getNum());
meger =true;
}
break;
}
}
}
}
if(meger){
addRandomNum();
checkComplete();
}
}
//向下滑動
public void swipeDown(){
boolean meger = false;
for (int x = 0; x< 4; x++) {
for (int y = 3; y>= 0;y--) {
for (int y1 = y-1; y1 >=0; y1--) {
if(cardsMap[x][y1].getNum()>0){
if(cardsMap[x][y].getNum()<=0){
/*
* 將下標為(x,y)所在位置的卡片上的數字
* 設定為,坐標為(x1,y)所在位置的卡片上的值;
* 第二步,將坐標(x1,y)所在位置的卡片上的數字
* 設定為0
* (即:變成空卡片)
*/
cardsMap[x][y].setNum(
cardsMap[x][y1].getNum());
cardsMap[x][y1].setNum(0);
y++;
meger =true;
break;
}else if(cardsMap[x][y].equals(cardsMap[x][y1])){
cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
cardsMap[x][y1].setNum(0);
MainActivity.getMainActivity().
addScore(cardsMap[x][y].getNum());
meger =true;
}
break;
uj5u.com熱心網友回復:
debug呀,步進分分鐘的事uj5u.com熱心網友回復:
怎么讓他顯示轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/75612.html
標籤:Android
上一篇:android 求助 我要使用 jwccx這個界面的帶資料arraylist傳遞到第二個界面我該怎么寫,我試了注冊清單也不行,求大神指教。
