首页 > 代码库 > gui界面2048小游戏

gui界面2048小游戏

坑的地方

JLabel色块要调透明

方向键要用press方法

主界面

技术分享
package game;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.Color;

public class Game extends JFrame implements KeyListener{

    private JPanel contentPane;
    Block[][] block1=new Block[4][4];//色块
    GameSrc game=new GameSrc();//游戏类
    int[][] pane=new int[4][4];//游戏类的数据
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Game frame = new Game();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Game() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(4, 4, 5, 5));
        
        game.newgame();
        pane=game.getPane();
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                block1[i][j]=new Block();
                block1[i][j].setValue(pane[i][j]);
                contentPane.add(block1[i][j]);
            }
        }
        this.addKeyListener(this);

    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO 自动生成的方法存根
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            game.left();
            pane=game.getPane();
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    block1[i][j].setValue(pane[i][j]);
                }
            }
            if(game.gameOver()==1){
                JOptionPane.showMessageDialog(this, "you lose");
            }
        }
        else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            game.right();
            pane=game.getPane();
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    block1[i][j].setValue(pane[i][j]);
                }
            }
            if(game.gameOver()==1){
                JOptionPane.showMessageDialog(this, "you lose");
            }
        }
        else if(e.getKeyCode()==KeyEvent.VK_DOWN){
            game.low();
            pane=game.getPane();
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    block1[i][j].setValue(pane[i][j]);
                }
            }
            if(game.gameOver()==1){
                JOptionPane.showMessageDialog(this, "you lose");
            }
        }
        else if(e.getKeyCode()==KeyEvent.VK_UP){
            game.up();
            pane=game.getPane();
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    block1[i][j].setValue(pane[i][j]);
                }
            }
            if(game.gameOver()==1){
                JOptionPane.showMessageDialog(this, "you lose");
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO 自动生成的方法存根
        
    }

}
View Code

本来逻辑代码

技术分享
package game;

import java.util.Random;

public class GameSrc{
    private int pane[][]=new int[4][4];    //棋盘
    int gameOver()    //显示棋盘
    {
        int gameOver=1;//   判断游戏结束条件
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                if(pane[i][j]==0){
                    gameOver=0;
                }
            }
        }
        return gameOver;
    }
    int[][] getPane(){//返回数据
        return pane;
    }
    void newgame()    //开始游戏
    {
        for(int i=0;i<=3;i++)    //初始化棋盘
            for(int j=0;j<=3;j++)
                pane[i][j]=0;
        
        Random random = new Random();
        int m=random.nextInt(4);//产生一个0到4(不包括4的随机数)
        int n=random.nextInt(4);
        int p=random.nextInt(4);
        int q=random.nextInt(4);
        
        //在随机的位置上产生初值2
        pane[m][n]=pane[p][q]=2;
    }
    void up(){//向上操作
        int i,j,three;
        boolean move=false;
        for(j=0;j<4;j++){  //上移动
            three=0;
            for(i=0;i<4;i++){
                if(pane[i][j]>0){
                    if(i!=three)
                        move=true;
                    int t=pane[i][j];
                    pane[i][j]=0;
                    pane[three][j]=t;
                    three++;
                }
            }
        }
        for(j=0;j<4;j++){  //合并数
            for(i=0;i<3;i++){
                if(pane[i][j]==pane[i+1][j]&&pane[i][j]!=0){
                    move=true;
                    pane[i][j]=pane[i][j]*2;
                    pane[i+1][j]=0;
                    int k=i+1;
                    for(;k+1<=3&&pane[k+1][j]>0;k++){
                        pane[k][j]=pane[k+1][j];
                        pane[k+1][j]=0;
                    }
                }
            }
        }
        if(move)
            random();//产生随机数
    }
    void low(){ //向下
        int i,j,three;
        boolean move=false;
        for(j=0;j<4;j++){//下移动 
            three=3;
            for(i=3;i>=0;i--){
                if(pane[i][j]>0){
                    if(i!=three)
                        move=true;
                    int t=pane[i][j];
                    pane[i][j]=0;
                    pane[i+three-i][j]=t;
                    three--;
                }
            }
        }
        for(j=0;j<4;j++){//合并数
            for(i=3;i>0;i--){
                if(pane[i][j]==pane[i-1][j]&&pane[i][j]!=0){
                    move=true;
                    pane[i][j]=pane[i][j]*2;
                    pane[i-1][j]=0;
                    int k=i-1;
                    for(;k-1>=0&&pane[k-1][j]>0;k--){
                        pane[k][j]=pane[k-1][j];
                        pane[k-1][j]=0;
                    }
                }
            }
        }
        if(move)
            random();    
    }
    void left(){//向左
        int i,j,three;
        boolean move=false;
        for(i=0;i<4;i++){//左移动
            three=0;
            for(j=0;j<4;j++){
                if(pane[i][j]>0){
                    if(j!=three)
                        move=true;
                    int t=pane[i][j];
                    pane[i][j]=0;
                    pane[i][three]=t;
                    three++;
                }
            }
        }
        for(i=0;i<4;i++){//合并数
            for(j=0;j<3;j++){
                if(pane[i][j]==pane[i][j+1]&&pane[i][j]!=0){
                    move=true;
                    pane[i][j]=pane[i][j]*2;
                    pane[i][j+1]=0;
                    int k=j+1;
                    for(;k+1<=3&&pane[i][k+1]>0;k++){
                        pane[i][k]=pane[i][k+1];
                        pane[i][k+1]=0;
                    }
                }
            }
        }
        if(move)
            random();
        
    }
    void right(){//右移
        int i,j,three;
        boolean move=false;
        for(i=0;i<4;i++){
            three=3;
            for(j=3;j>=0;j--){//右移动
                if(pane[i][j]>0){
                    if(j!=three)
                        move=true;
                    int t=pane[i][j];
                    pane[i][j]=0;
                    pane[i][three]=t;
                    three--;
                }
            }
        }
        for(i=0;i<4;i++){//合并数
            for(j=3;j>0;j--){
                if(pane[i][j]==pane[i][j-1]&&pane[i][j]!=0){
                    move=true;
                    pane[i][j]=pane[i][j]*2;
                    pane[i][j-1]=0;
                    int k=j-1;
                    for(;k-1>=0&&pane[i][k-1]>0;k--){
                        pane[i][k]=pane[i][k-1];
                        pane[i][k-1]=0;
                    }
                }
            }
        }
        if(move)
            random();
    }
    void random(){//产生随机数
        Random random = new Random();
        int m=random.nextInt(4);//产生一个0到4(不包括4的随机数)
        int n=random.nextInt(4);
        for(;pane[m][n]!=0;){
            m=random.nextInt(4);//产生一个0到4(不包括4的随机数)
            n=random.nextInt(4);
        }
        if(random.nextBoolean())
            pane[m][n]=2;
        else 
            pane[m][n]=4;
    }
    

}
View Code

色块代码

技术分享
package game;

import javax.swing.*; 
import java.awt.*; 
public class Block extends JLabel  
{ 
  private int value; 
  public Block()  
  { 
    value = 0;//初始化值为0 
    setFont(new Font("font", Font.PLAIN, 40));//设定字体 
    setBackground(Color.gray);//设定初始颜色为灰色 
  } 
   
  public int getValue()//获取值 
  { 
    return value; 
  } 
   
  public void setValue(int value) 
  { 
    this.value =http://www.mamicode.com/ value; 
    String text = String.valueOf(value); 
    if (value != 0) 
      setText(text); 
    else
      setText("");//如果值为0则不显示
    setOpaque(true);
    setColor(); 
  } 
   
  public void setColor() //根据值的不同设定不同的背景颜色、label字体 
  {
    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; 
      } 
  } 
}
View Code

 

gui界面2048小游戏