用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

Java 2048游戏源码

2015-04-18 作者: java源代码大全举报

[java]代码库

package my2048;
 
import javax.swing.*;
import java.awt.*;
 
@SuppressWarnings("serial")
public class Block extends JLabel {
    private int value;
 
    public Block() {
        value = 0;
        setFont(new Font("font", Font.PLAIN, 40));
        setBackground(Color.gray);
 
    }
 
    public int getValue() {
        return value;
    }
 
    public void setValue(int value) {
        this.value = value;
        String text = String.valueOf(value);
        if (value != 0)
            setText(text);
        else
            setText("");
        setColor();
    }
 
    public void setColor() {
            switch (value) {
        case 0:
            setBackground(Color.gray);
            break;
        case 2:
            setBackground(new Color(238, 228, 218));
            break;
        case 4:
            setBackground(new Color(238, 224, 198));
            break;
        case 8:
            setBackground(new Color(243, 177, 116));
            break;
        case 16:
            setBackground(new Color(243, 177, 116));
            break;
        case 32:
            setBackground(new Color(248, 149, 90));
            break;
        case 64:
            setBackground(new Color(249, 94, 50));
            break;
        case 128:
            setBackground(new Color(239, 207, 108));
            break;
        case 256:
            setBackground(new Color(239, 207, 99));
            break;
        case 512:
            setBackground(new Color(239, 203, 82));
            break;
        case 1024:
            setBackground(new Color(239, 199, 57));
            break;
        case 2048:
            setBackground(new Color(239, 195, 41));
            break;
        case 4096:
            setBackground(new Color(255, 60, 57));
            break;
    }
}


package my2048;
import java.awt.event.*;
import javax.swing.*;
 
public class Operation implements KeyListener{
    Block[] block;
    JPanel panel;
    int moveFlag;
    boolean numFlag;
 
    public Operation(JFrame frame) {
        this.panel = (JPanel)frame.getContentPane();
        block = new Block[16];
        numFlag = true;
        moveFlag = 0;
        addBlock();
        for (int i = 0; i < 2; i++)
            appearBlock();
        frame.addKeyListener(this);
 
    }
 
    private void addBlock() {
        for (int i = 0; i < 16; i++) {
            block[i] = new Block();
            block[i].setHorizontalAlignment(JLabel.CENTER);
            // 不透明
            block[i].setOpaque(true);
            panel.add(block[i]);
             
        }
    }
 
    public void appearBlock() {
        while (numFlag) {
            int index = (int) (Math.random() * 16);
            if (block[index].getValue() == 0) {
                if (Math.random() < 0.5)
                    block[index].setValue(2);
                else
                    block[index].setValue(4);
                break;
            }
        }
    }
 
    public void judgeAppear() {
        int sum = 0;
        for (int i = 0; i < 16; i++) {
            if (block[i].getValue() != 0)
                sum++;
        }
        if (sum == 16)
            numFlag = false;
 
    }
 
    public void upBlock() {
 
        for (int i = 12; i < 16; i++) {
            int index = i;
            for (int j = i - 4; j >= i - 12; j -= 4) {
                int valueI = block[index].getValue(), valueJ = block[j]
                        .getValue();
                if (valueJ == 0) {
                    block[index].setValue(0);
                    block[j].setValue(valueI);
                } else {
                    if (valueI == valueJ) {
                        block[index].setValue(0);
                        block[j].setValue(valueI + valueJ);
                        if (valueI + valueJ == 4096)
                            win();
                        numFlag = true;
                        moveFlag = 0;
                         
                         
                        if(j>i-12)
                            if(block[j-4].getValue()==(valueI+valueJ))
                        break;
 
                    } else if (numFlag == false)
                        moveFlag += 1;
                }
                index = j;
            }
        }
 
    }
 
    public void downBlock() {
 
        for (int i = 0; i < 4; i++) {
            int index = i;
            for (int j = i + 4; j <= i + 12; j += 4) {
                int valueI = block[index].getValue(), valueJ = block[j]
                        .getValue();
                if (valueJ == 0) {
                    block[index].setValue(0);
                    block[j].setValue(valueI);
                } else {
                    if (valueI == valueJ) {
                        block[index].setValue(0);
                        block[j].setValue(valueI + valueJ);
                        if (valueI + valueJ == 4096)
                            win();
                        numFlag = true;
                        moveFlag = 0;
                         
                        if(j<i+12)
                            if(block[j+4].getValue()==(valueI+valueJ))
                        break;
                         
                    } else if (numFlag == false)
                        moveFlag += 1;
                }
                index = j;
            }
        }
 
    }
    public void rightBlock() {
 
        for (int i = 0; i <= 12; i += 4) {
            int index = i;
            for (int j = i + 1; j <= i + 3; j++) {
                int valueI = block[index].getValue(), valueJ = block[j]
                        .getValue();
                if (valueJ == 0) {
                    block[index].setValue(0);
                    block[j].setValue(valueI);
                } else {
                    if (valueI == valueJ) {
                        block[index].setValue(0);
                        block[j].setValue(valueI + valueJ);
                        if (valueI + valueJ == 4096)
                            win();
                        numFlag = true;
                        moveFlag = 0;
 
                        if(j<i+3)
                            if(block[j+1].getValue()==(valueI+valueJ))
                        break;
                    } else if (numFlag == false)
                        moveFlag += 1;
                }
                index = j;
            }
        }
 
    }
    public void leftBlock() {
 
        for (int i = 3; i <= 15; i += 4) {
            int index = i;
            for (int j = i - 1; j >= i - 3; j--) {
                int valueI = block[index].getValue(), valueJ = block[j]
                        .getValue();
                if (valueJ == 0) {
                    block[index].setValue(0);
                    block[j].setValue(valueI);
                } else {
                    if (valueI == valueJ) {
                        block[index].setValue(0);
                        block[j].setValue(valueI + valueJ);
                        if (valueI + valueJ == 4096)
                            win();
                        numFlag = true;
                        moveFlag = 0;
                         
                        if(j<i-3)
                            if(block[j-1].getValue()==(valueI+valueJ))
                        break;
                    } else if (numFlag == false)
                        moveFlag += 1;
                }
                index = j;
            }
        }
 
    }
    public void over() {
        if (!numFlag && moveFlag >= 36) {
            block[4].setText("G");
            block[5].setText("A");
            block[6].setText("M");
            block[7].setText("E");
            block[8].setText("O");
            block[9].setText("V");
            block[10].setText("E");
            block[11].setText("R");
             
            block[11].addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    reStart();
                }
            });
        }
    }
     
    public void win() {
         
        block[0].setText("Y");
        block[1].setText("O");
        block[2].setText("U");
        block[13].setText("W");
        block[14].setText("I");
        block[15].setText("N");
         
        block[15].addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                reStart();
            }
        });
 
    }
    public void reStart(){
        numFlag=true;
        moveFlag=0;
        for(int i=0;i<16;i++)
            block[i].setValue(0);
        for (int i = 0; i < 2; i++)
            appearBlock();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            upBlock();
            judgeAppear();
            appearBlock();
            over();
            break;
        case KeyEvent.VK_DOWN:
            downBlock();
            judgeAppear();
            appearBlock();
            over();
            break;
        case KeyEvent.VK_LEFT:
            leftBlock();
            judgeAppear();
            appearBlock();
            over();
            break;
        case KeyEvent.VK_RIGHT:
            rightBlock();
            judgeAppear();
            appearBlock();
            over();
            break;
        }
 
    }
 
     
    @Override
    public void keyTyped(KeyEvent e) {
 
    }
 
    @Override
    public void keyReleased(KeyEvent e) {
 
    }
 
}

package my2048;
 
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class My2048 extends JFrame {
 
    public My2048() {
        setTitle("2048");
        setSize(400, 400);
        setLocation(500, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(4, 4, 5, 5));
        new Operation(this);
        this.setVisible(true);
    }
 
    public static void main(String args[]) {
        try {
            UIManager
                    .setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException e) {
 
            e.printStackTrace();
        }
        JFrame.setDefaultLookAndFeelDecorated(true);
        new My2048();
    }
 
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...