android - 做一个2048的游戏,左右移动没有问题,但是无法上下移动,求解

 打个的故事 发布于 2022-10-29 11:23

Card.class

package com.example.myroom.androidgame2048;

import android.content.Context;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.w3c.dom.Text;

/**

  • Created by myroom on 2016/12/2.
    */

public class Card extends FrameLayout {

public Card(Context context){
    super(context);
    label=new TextView(getContext());
    label.setTextSize(32);
    label.setBackgroundColor(0x33ffffff);
    label.setGravity(Gravity.CENTER);
    LayoutParams lp= new LayoutParams(-1,-1);

// GridLayout.LayoutParams ll=new GridLayout.LayoutParams(lp);

    lp.setMargins(20,20,0,0);
    addView(label,lp);
    setNum(0);
}
private int num=0;
private TextView label;
public int getNum() {
    return num;
}

public void setNum(int num) {
    this.num=num;
    if(num<=0) {
        label.setText("");//要是字符串
    }else{
        label.setText(num+"");
    }
    }
public boolean equals(Card card) {
    return getNum()==card.getNum();//判断数字是否相同
}

}

GameView.class

package com.example.myroom.androidgame2048;

import android.content.Context;
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.Toast;

import java.util.ArrayList;
import java.util.List;

/**

  • Created by myroom on 2016/12/2.
    */

public class GameView extends GridLayout {

public GameView(Context context) {
    super(context);
    InitGameView();
}

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    InitGameView();
}

public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    InitGameView();
}

private void InitGameView(){
    setColumnCount(4);//指明是4列
    setBackgroundColor(0xffbbada0);
    setOnTouchListener(new View.OnTouchListener() {
        private float startX,startY,offsetX,offsetY;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = motionEvent.getX();
                    startY = motionEvent.getY();
                    break;
                case MotionEvent.ACTION_UP:
                    offsetX = motionEvent.getX() - startX;
                    offsetY = motionEvent.getY() - startY;
                    if (Math.abs(offsetX) >Math.abs(offsetY)) {
                        if (offsetX < -5) {//向左滑动
                            swipeLeft();
                        } else if (offsetX > 5) {//向右滑动
                            swipeRight();
                        }else {
                            if(offsetY< -5){//向上

                                swipeup();
                            }else if(offsetY> 5){//向下
                                swipeDown();
                            }
                        }
                        break;
                    }
            }

            return true;
        }
    });
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {//位置变化时
    super.onSizeChanged(w, h, oldw, oldh);
    int cardWidth=(Math.min(w,h)-20)/4;//像素间隔
    addCard(cardWidth,cardWidth);
    startgame();
}

private void startgame() {
    for(int y=0;y<4;y++){
        for(int x=0;x<4;x++){
            cardsmap[x][y].setNum(0);
        }
    }
    addRandomNum();
    addRandomNum();
    addRandomNum();
    addRandomNum();
    addRandomNum();
    addRandomNum();
    addRandomNum();
    addRandomNum();
    addRandomNum();
    addRandomNum();
    addRandomNum();

}

private void addCard(int cardWidth,int cardheight){
    Card c;
    for(int y=0;y<4;y++){//添加16张卡片
        for(int x=0;x<4;x++){
            c=new Card(getContext());
            c.setNum(0);
            addView(c,cardWidth,cardheight);
            cardsmap[x][y]=c;//记录数据

        }
    }
}
private void  addRandomNum() {
    emptyPoints.clear();
    for (int y = 0; y < 4; y++) {
        for (int x = 0; x < 4; x++) {
            if (cardsmap[x][y].getNum() <= 0) {
                emptyPoints.add(new Point(x, y));
            }
        }
    }

    Point p = emptyPoints.remove((int)( Math.random() * emptyPoints.size()));
    cardsmap[p.x][p.y].setNum(Math.random() > 0.1 ? 2 : 4);

}
private void swipeLeft(){
    boolean merge=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){
                        cardsmap[x][y].setNum(cardsmap[x1][y].getNum());
                        cardsmap[x1][y].setNum(0);
                        x--;
                        merge=true;
                    }else if(cardsmap[x][y].equals(cardsmap[x1][y])){
                        cardsmap[x][y].setNum(cardsmap[x][y].getNum()*2);
                        cardsmap[x1][y].setNum(0);
                        merge=true;

                    }
                    break;
                }
            }
        }
    }
    if(merge){
        addRandomNum();
    }
}
private void swipeRight(){
    Toast.makeText(getContext(),"right",Toast.LENGTH_SHORT).show();
    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){
                        cardsmap[x][y].setNum(cardsmap[x1][y].getNum());
                        cardsmap[x1][y].setNum(0);
                        x++;
                        break;
                    }else if(cardsmap[x][y].equals(cardsmap[x1][y])){
                        cardsmap[x][y].setNum(cardsmap[x][y].getNum()*2);
                        cardsmap[x1][y].setNum(0);
                        break;
                    }
                }
            }
        }
    }
}
private void swipeup(){
    Toast.makeText(getContext(),"up",Toast.LENGTH_SHORT).show();
    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){
                        cardsmap[x][y].setNum(cardsmap[x][y1].getNum());
                        cardsmap[x][y1].setNum(0);
                        y--;
                        break;
                    }else if(cardsmap[x][y].equals(cardsmap[x][y1])){
                        cardsmap[x][y].setNum(cardsmap[x][y].getNum()*2);
                        cardsmap[x][y1].setNum(0);
                        break;
                    }
                }
            }
        }
    }
}
private void swipeDown(){
    Toast.makeText(getContext(),"down",Toast.LENGTH_SHORT).show();
    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){
                        cardsmap[x][y].setNum(cardsmap[x][y1].getNum());
                        cardsmap[x][y1].setNum(0);

                        y++;
                        break;
                    }else if(cardsmap[x][y].equals(cardsmap[x][y1])){
                        cardsmap[x][y].setNum(cardsmap[x][y].getNum()*2);
                        cardsmap[x][y1].setNum(0);
                        break;
                    }
                }
            }
        }
    }
}
private Card[][] cardsmap=new Card[4][4];
private ListemptyPoints=new ArrayList();

}

activity_main.xml

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"
tools:context="com.example.myroom.androidgame2048.MainActivity">

    
    

1 个回答
  • 惭愧,括号上下移动嵌到判断左右的操作了,粗心大意还找了这么久[羞愧]

    2022-10-30 16:38 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有