1.代码:
package com.jxyy;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* 2048小游戏
*
* @author 17移动互联01 石原酒酒
* @date: November 23, 2020
* @fileName: Game2048Test.java
*
*/
public class Game2048Test {
public static void main(String[] args) {
new Game2048();
}
}
class Game2048 extends JFrame {
private static final long serialVersionUID = 1L;
private GameRect[][] arr = new GameRect[4][4];
private Random r = new Random();
private boolean flag = false;
public Game2048() {
setTitle("TwoZeroFourEight(version1.2-3)");
setSize(1200, 800);
setLocation(220, 100);
setDefaultCloseOperation(3);
setResizable(false);
setVisible(true);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = new GameRect(new Color(224 ,255 ,255),"");
}
}
addGameRect();
addGameRect();
this.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == 38) {
up();
go();
} else if (keyCode == 40) {
down();go();
} else if (keyCode == 37) {
left();go();
} else if (keyCode == 39) {
right();go();
}else if(keyCode==112) {
repaint();
G();/*按f1键生成多个1024,快速结束游戏*/
}
}
public void up() {
boolean upHasNext = true;
while (upHasNext) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr[i].length; j++) {
GameRect up = arr[i][j];
GameRect down = arr[i + 1][j];
if (up.cont.equals("") && !down.cont.equals("")) {
GameRect temp = null;
temp = arr[i][j];
arr[i][j] = arr[i + 1][j];
arr[i + 1][j] = temp;
} else if (!up.cont.equals("") && up.cont.equals(down.cont)) {
String cont = up.cont;
int parseInt = Integer.parseInt(cont) * 2;
arr[i][j].cont = String.valueOf(parseInt);
arr[i + 1][j].cont = "";
arr[i + 1][j].color = new Color(224 ,255 ,255);
flag = true;
}
}
}
upHasNext = upHasNext();
}
}
public void down() {
boolean downHasNext = true;
while (downHasNext) {
for (int i = 3; i >= 1; i--) {
for (int j = 0; j < arr[i].length; j++) {
GameRect down = arr[i][j];
GameRect up = arr[i - 1][j];
if (!up.cont.equals("") && down.cont.equals("")) {
GameRect temp = null;
temp = arr[i][j];
arr[i][j] = arr[i - 1][j];
arr[i - 1][j] = temp;
} else if (!up.cont.equals("") && up.cont.equals(down.cont)) {
String cont = up.cont;
int parseInt = Integer.parseInt(cont) * 2;
arr[i][j].cont = String.valueOf(parseInt);
arr[i - 1][j].cont = "";
arr[i - 1][j].color = new Color(224 ,255 ,255);
flag = true;
}
}
}
downHasNext = downHasNext();
}
}
public void left() {
boolean leftHasNext = true;
while (leftHasNext) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length - 1; j++) {
GameRect left = arr[i][j];
GameRect right = arr[i][j + 1];
if (left.cont.equals("") && !right.cont.equals("")) {
GameRect temp = null;
temp = arr[i][j];
arr[i][j] = arr[i][j + 1];
arr[i][j + 1] = temp;
} else if (!left.cont.equals("") && left.cont.equals(right.cont)) {
String cont = left.cont;
int parseInt = Integer.parseInt(cont) * 2;
arr[i][j].cont = String.valueOf(parseInt);
arr[i][j + 1].cont = "";
arr[i][j + 1].color = new Color(224 ,255 ,255);
flag = true;
}
}
}
leftHasNext = leftHasNext();
}
}
public void right() {
boolean rightHasNext = true;
while (rightHasNext) {
for (int i = 0; i < arr.length; i++) {
for (int j = 3; j > 0; j--) {
GameRect right = arr[i][j];
GameRect left = arr[i][j - 1];
if (right.cont.equals("") && !left.cont.equals("")) {
GameRect temp = null;
temp = arr[i][j];
arr[i][j] = arr[i][j - 1];
arr[i][j - 1] = temp;
} else if (!left.cont.equals("") && left.cont.equals(right.cont)) {
String cont = left.cont; // 获取游戏块内容
int parseInt = Integer.parseInt(cont) * 2;
arr[i][j].cont = String.valueOf(parseInt);
arr[i][j - 1].cont = "";
arr[i][j - 1].color = new Color(224 ,255 ,255);
flag = true;
}
}
}
rightHasNext = rightHasNext();
}
}
});
}
public boolean upHasNext() {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr[i].length; j++) {
GameRect up = arr[i][j];
GameRect down = arr[i + 1][j];
if (up.cont.equals("") && !down.cont.equals("")) {
return true;
} else if (!up.cont.equals("") && up.cont.equals(down.cont)) {
String cont = up.cont;
int parseInt = Integer.parseInt(cont) * 2;
arr[i][j].cont = String.valueOf(parseInt);
arr[i + 1][j].cont = "";
arr[i + 1][j].color = new Color(224 ,255 ,255);
flag = true;
}
}
}return false;
}
public void go() {
int state = next();
if (state == 2) {
if (!flag) {
addGameRect();
}
repaint();
flag = false;
} else if (state == 0) {
repaint();
JOptionPane.showMessageDialog(Game2048.this, "恭喜挑战成功!");
} else if (state == 1) {
JOptionPane.showMessageDialog(Game2048.this, "挑战失败!");
}
} public boolean downHasNext() {
for (int i = 3; i >= 1; i--) {
for (int j = 0; j < arr[i].length; j++) {
GameRect down = arr[i][j];
GameRect up = arr[i - 1][j];
if (!up.cont.equals("") && down.cont.equals("")) {
return true;
} else if (!up.cont.equals("") && up.cont.equals(down.cont)) {
String cont = up.cont;
int parseInt = Integer.parseInt(cont) * 2;
arr[i][j].cont = String.valueOf(parseInt);
arr[i - 1][j].cont = "";
arr[i - 1][j].color = new Color(224 ,255 ,255);
flag = true;
}
}
}return false;
}
public boolean leftHasNext() {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length - 1; j++) {
GameRect left = arr[i][j];
GameRect right = arr[i][j + 1];
if (left.cont.equals("") && !right.cont.equals("")) {
return true;
} else if (!left.cont.equals("") && left.cont.equals(right.cont)) {
String cont = left.cont;
int parseInt = Integer.parseInt(cont) * 2;
arr[i][j].cont = String.valueOf(parseInt);
arr[i][j + 1].cont = "";
arr[i][j + 1].color = new Color(224 ,255 ,255);
return false;
}
}
}return false;
}
public boolean rightHasNext() {
for (int i = 0; i < arr.length; i++) {
for (int j = 3; j > 0; j--) {
GameRect right = arr[i][j];
GameRect left = arr[i][j - 1];
if (right.cont.equals("") && !left.cont.equals("")) {
return true;
} else if (!left.cont.equals("") && left.cont.equals(right.cont)) {
String cont = left.cont;
int parseInt = Integer.parseInt(cont) * 2;
arr[i][j].cont = String.valueOf(parseInt);
arr[i][j - 1].cont = "";
arr[i][j - 1].color = new Color(224 ,255 ,255);
return false;
}
}
}
return false;
}
public int next() {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
GameRect gr = arr[i][j];
if (gr.cont.equals("2048")) {
return 0;
}
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
GameRect gr = arr[i][j];
if (gr.cont.equals("")) {
return 2;
}
}
}
return 1;
}
public void G() {
while (true) {
int i = r.nextInt(4);
int j = r.nextInt(4);
if (arr[i][j].cont.equals("")) {
int con = 1024;
arr[i][j] = new GameRect(new Color(92 ,172 ,238), String.valueOf(con));
break;
}
}
}
public void addGameRect() {
while (true) {
int i = r.nextInt(4);
int j = r.nextInt(4);
if (arr[i][j].cont.equals("")) {
int con = (r.nextInt(2) + 1) * 2;
arr[i][j] = new GameRect(new Color(92 ,172 ,238), String.valueOf(con));
break;
}
}
}
public void paint(Graphics g) {
super.paint(g);
File file = new File("d.jpg");
BufferedImage img = null;
try {
img = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(img, 1000, 600, this);
File file1 = new File("file.jpg");
BufferedImage img1 = null;
try {
img1 = ImageIO.read(file1);
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(img1, -31, 700, this);
g.setColor(new Color(107 ,133 ,108));
g.fillRect(0, 0, 1000, 700);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
GameRect gameRect = arr[i][j];
g.setColor(gameRect.color);
g.drawOval(40 + j * 240, 80+ i * 140+90, 200, 100);
g.setColor(new Color(144,238,144));
g.setFont(new Font("Minion Pro", Font.ITALIC, 50));
g.drawString(gameRect.cont, 40 + j * 240 + 80, 80 + i * 140 + 65+90);
}
}
g.setColor(Color.red);
g.setFont(new Font("华文彩云", Font.ITALIC, 80));
g.drawString("My Game 2048", 260, 130);
g.setColor(Color.yellow);
g.setFont(new Font("华文彩云", Font.ITALIC, 79));
g.drawString("My Game 2048", 260, 130);
g.setColor(new Color(240,255 ,240));
g.fillRect(1010, 100, 150, 80);
g.setColor(Color.pink);
g.setFont(new Font("楷体", Font.BOLD, 35));
g.drawString("restart", 1020, 100+50);
g.setColor(new Color(240,255 ,240));
g.fillRect(1010, 300, 150, 80);
g.setColor(Color.pink);
g.setFont(new Font("楷体", Font.BOLD, 35));
g.drawString("quit", 1040, 200+150);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
int x=e.getX();
int y=e.getY();
if(x>=1010&&x<=1160) {
if(y>=100&&y<=180) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = new GameRect(new Color(224 ,255 ,255),"");
}
}addGameRect();
addGameRect();
Game2048.this.repaint();
}else if(x>=1010&&x<=1160){
if(y>=300&&y<=380) {
dispose();
}}
}
}
});
}
}
class GameRect {
Color color;
String cont;
public GameRect(Color color, String cont) {
this.color = color;
this.cont = cont;
}
}
2.游戏玩法
1)上下左右键控制
2)按f1键有惊喜