Java perfect 2048 small game
- 2020-04-01 03:47:20
- OfStack
Perfect imitation of the 2048 game, is based on a netizen's 2048 change.
Block. The Java
import javax.swing.*;
import java.awt.*;
public class Block extends JLabel
{
private int value;
public Block()
{
value = 0;//The initialization value is 0
setFont(new Font("font", Font.PLAIN, 40));//Set the font
setBackground(Color.gray);//Set the initial color to gray
}
public int getValue()//Get the value
{
return value;
}
public void setValue(int value)
{
this.value = value;
String text = String.valueOf(value);
if (value != 0)
setText(text);
else
setText("");//If the value is 0, it is not displayed
setColor();
}
public void setColor() //Set different background color and label font according to different values
{
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;
}
}
}
My2048. Java
import java.awt.*;
import javax.swing.*;
public class My2048 extends JFrame
{
public My2048()//The constructor
{
setTitle("2048");//Set the title
setSize(400, 400);//Set window size
setLocation(500, 200);//Sets the starting position of the window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(4, 4, 5, 5));//Set the layout as GridLayout
new Operation(this);
this.setVisible(true);//As a visual
}
public static void main(String args[]) //Program entry point
{
try
{
UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");//Set the UI
} //Accept the exception thrown
catch (ClassNotFoundException | InstantiationException| IllegalAccessException | UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(true);//Sets the default appearance of the Frame
new My2048();
}
}
Operation. The Java
import java.awt.event.*;
import javax.swing.*;
public class Operation implements KeyListener
{
Block[] block;//It is used to store 16 data
JPanel panel;
public boolean up,down,left,right;
int moveFlag;//Used to accumulate the number of moves
boolean numFlag;//To see if new Numbers can be added
public Operation(JFrame frame)
{
this.panel = (JPanel)frame.getContentPane();//Constructing a panel
block = new Block[16];//Construct an array of length 16
numFlag = true;//Initialize the
moveFlag = 0;
up=true;down=true;left=true;right=true;
addBlock();
for (int i = 0; i < 2; i++)
appearBlock();
frame.addKeyListener(this);
}
private void addBlock()
{
for (int i = 0; i < 16; i++) //Add a block to the panel
{
block[i] = new Block();
block[i].setHorizontalAlignment(JLabel.CENTER);//Opaque label
block[i].setOpaque(true);
panel.add(block[i]);
}
}
public void appearBlock()
{
while (numFlag) //When a random new value can be added
{
int index = (int) (Math.random() * 16);//Take a random integer from 0 to 15 and use it as a random addition to the 2 or 4 on the disk
if (block[index].getValue() == 0)//If the value in the block array is 0, a 2 or 4 number is added when it is empty
{
if (Math.random() < 0.5)
{
block[index].setValue(2);
}
else
{
block[index].setValue(4);
}
break;//Jump out of the while
}
}
}
public void judgeAppear() //Count whether the block array contains elements with a value of 0. If not, numFlag becomes false
{
int sum = 0;
for (int i = 0; i < 16; i++)
{
if (block[i].getValue() != 0)
{
sum++;
}
}
if (sum == 16)
numFlag = false;
}
public int Find(int i,int j,int a,int b)
{
while(i<b&&i>=a)
{
if(block[i].getValue()!=0)
{
return i;
}
i=i+j;
}
return -1;
}
public void upBlock()
{
int i=0,j=0;int t=0;int valueJ=0;int valueI=0;int index=0;
for(i=0;i<4;i++)
{
index=i;
for(j=i+4;j<16;j+=4)
{
valueJ=0; valueI=0;
if(block[index].getValue()==0)
{
t=Find(index,4,0,16);
if(t!=-1)
{
block[index].setValue(block[t].getValue());
block[t].setValue(0);
}
else
{
break;
}
}
valueI=block[index].getValue();
if(block[j].getValue()==0)
{
t=Find(j,4,0,16);
if(t!=-1)
{
block[j].setValue(block[t].getValue());
block[t].setValue(0);
}
else
{
break;
}
}
valueJ=block[j].getValue();
if(valueI==valueJ&&valueI!=0&&valueJ!=0)
{
block[index].setValue(valueI+valueJ);
block[j].setValue(0);
numFlag = true;
}
index=j;
}
}
}
public void downBlock() {
int i=0,j=0;int t=0;int valueJ=0;int valueI=0;int index=0;
for(i=12;i<16;i++)
{
index=i;
for(j=i-4;j>=0;j-=4)
{
valueJ=0; valueI=0;
if(block[index].getValue()==0)
{
t=Find(index,-4,0,16);
if(t!=-1)
{
block[index].setValue(block[t].getValue());
block[t].setValue(0);
}
else
{
break;
}
}
valueI=block[index].getValue();
if(block[j].getValue()==0)
{
t=Find(j,-4,0,16);
if(t!=-1)
{
block[j].setValue(block[t].getValue());
block[t].setValue(0);
}
else
{
break;
}
}
valueJ=block[j].getValue();
if(valueI==valueJ&&valueI!=0&&valueJ!=0)
{
block[index].setValue(valueI+valueJ);
block[j].setValue(0);
numFlag = true;
}
index=j;
}
}
}
public void rightBlock()
{
int i=0,j=0;int t=0;int valueJ=0;int valueI=0;int index=0;
for(i=3;i<16;i+=4)
{
index=i;
for(j=i-1;j>i-4;j--)
{
valueJ=0; valueI=0;
if(block[index].getValue()==0)
{
t=Find(index,-1,i-3,index+1);
if(t!=-1)
{
block[index].setValue(block[t].getValue());
block[t].setValue(0);
}
else
{
break;
}
}
valueI=block[index].getValue();
if(block[j].getValue()==0)
{
t=Find(j,-1,i-3,j+1);
if(t!=-1)
{
block[j].setValue(block[t].getValue());
block[t].setValue(0);
}
else
{
break;
}
}
valueJ=block[j].getValue();
if(valueI==valueJ&&valueI!=0&&valueJ!=0)
{
block[index].setValue(valueI+valueJ);
block[j].setValue(0);
numFlag = true;
}
index=j;
}
}
}
public void leftBlock()
{
int i=0,j=0;int t=0;int valueJ=0;int valueI=0;int index=0;
for(i=0;i<16;i+=4)
{
index=i;
for(j=i+1;j<i+4;j++)
{
valueJ=0; valueI=0;
if(block[index].getValue()==0)
{
t=Find(index,1,index,i+4);
if(t!=-1)
{
block[index].setValue(block[t].getValue());
block[t].setValue(0);
}
else
{
break;
}
}
valueI=block[index].getValue();
if(block[j].getValue()==0)
{
t=Find(j,1,j,i+4);
if(t!=-1)
{
block[j].setValue(block[t].getValue());
block[t].setValue(0);
}
else
{
break;
}
}
valueJ=block[j].getValue();
if(valueI==valueJ&&valueI!=0&&valueJ!=0)
{
block[index].setValue(valueI+valueJ);
block[j].setValue(0);
numFlag = true;
}
index=j;
}
}
}
public void over()
{
if (numFlag ==false&& up==false&&down==false&&left==false&&right==false) //GAMEOVER is displayed in the center of the disk when an element cannot be added and the number of unmovable steps exceeds 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() {public void mousePressed(MouseEvent e){reStart();}});
}
}
public void win() //With OVER
{
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() {
public void mousePressed(MouseEvent e) {
reStart();
}
});
}
public void reStart()//Restart the game, similar to the constructor, is not covered
{
numFlag=true;
moveFlag=0;
up=true;down=true;left=true;right=true;
for(int i=0;i<16;i++)
block[i].setValue(0);
for (int i = 0; i < 2; i++)
appearBlock();
}
public void keyPressed(KeyEvent e) //Determine the key to press, and then call the move function, judge function, add function, judge whether to lose the function
{
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if(up){
upBlock();}
judgeAppear();
appearBlock();
over();
if(numFlag==false)
{
up=false;
}
else
{
up=true;down=true;left=true;right=true;
}
break;
case KeyEvent.VK_DOWN:
if(down){
downBlock();}
judgeAppear();
appearBlock();
over();
if(numFlag==false)
{
down=false;
}
else
{
up=true;down=true;left=true;right=true;
}
break;
case KeyEvent.VK_LEFT:
if(left){
leftBlock();}
judgeAppear();
appearBlock();
over();
if(numFlag==false)
{
left=false;
}
else
{
up=true;down=true;left=true;right=true;
}
break;
case KeyEvent.VK_RIGHT:
if(right){
rightBlock();}
judgeAppear();
appearBlock();
over();
if(numFlag==false)
{
right=false;
}
else
{
up=true;down=true;left=true;right=true;
}
break;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
Above is the article to share with you about Java perfect 2048 small game all the code, I hope to help you learn Java.