求助,java如何合并2048和俄罗斯方块
2048代码
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Game2048 extends JFrame {
// 移动方向
final public static int MOVE_UP = 0xf37;
final public static int MOVE_DOWN = 0xf36;
final public static int MOVE_LEFT = 0xf35;
final public static int MOVE_RIGHT = 0xf34;
// 游戏状态
final public static int GAME_OVER = 0xf33;
final public static int GAME_CONTINUE = 0xf32;
final public static int GAME_WIN = 0xf31;
// 按钮事件
final public static int BUTTON_NEW_GAME = 0xf30;
final public static int BUTTON_ABOUT = 0xf28;
final public static int BUTTON_EXIT = 0xf27;
/**
* 行
*/
private int column;
/**
* 列
*/
private int row;
/**
* 游戏状态
*/
private int gameState;
/**
* 网格集
*/
private HashMap<Point, Cube> viewList = new HashMap<>();
/**
* 计分板
*/
private JMenuItem scoreBoard;
/**
* 计步器
*/
private JMenuItem arithmometer;
/**
* 计步
*/
private int count;
/**
* 新游戏加流程
*/
private int gameLv;
/**
* main函数
*
* @param args
*/
public static void main(String[] args) {
Game2048 game = new Game2048(400, 500);
game.setTitle("2048");
game.setLocationRelativeTo(null);
game.setVisible(true);
game.newGame();
}
/**
* 构造一个默认大小的界面
*/
public Game2048() {
this(400, 500);
}
/**
* 构造一个指定宽高的界面
*
* @param width
* 宽
* @param height
* 高
*/
public Game2048(int width, int height) {
column = width / 100;
row = height / 100;
this.setLayout(new GridLayout(row, column));
// 事件监听
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.setSize(width, height);
// 利用button 绘制网格
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
viewList.put(new Point(i, j), new Cube());
this.add(getView(i, j).getView());
}
}
// 设置按键监听
this.addKeyListener(new MyKeyListener(this));
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("游戏");
JMenuItem item1 = new JMenuItem("新游戏");
item1.addMouseListener(new MyMouseListener(this,
Game2048.BUTTON_NEW_GAME));
JMenuItem item2 = new JMenuItem("退出");
item2.addMouseListener(new MyMouseListener(this, Game2048.BUTTON_EXIT));
jm.add(item1);
jm.add(item2);
JMenu jm2 = new JMenu("关于");
JMenuItem item3 = new JMenuItem("关于");
item3.addMouseListener(new MyMouseListener(this, Game2048.BUTTON_ABOUT));
jm2.add(item3);
scoreBoard = new JMenuItem();
arithmometer = new JMenuItem();
jmb.add(jm);
jmb.add(jm2);
jmb.add(scoreBoard);
jmb.add(arithmometer);
this.setJMenuBar(jmb);
}
/**
* 向上移动
*/
public void up() {
for (int x = 1; x < row; x++) {
for (int i = 0; i < column; i++) {
move(Game2048.MOVE_UP, x, i, true);
}
}
createCube();
for (int x = 1; x < row; x++) {
for (int i = 0; i < column; i++) {
move(Game2048.MOVE_UP, x, i, false);
}
}
addCount();
}
/**
* 向下移动
*/
public void down() {
for (int x = row - 2; x >= 0; x--) {
for (int y = 0; y < column; y++) {
move(Game2048.MOVE_DOWN, x, y, true);
}
}
createCube();
for (int x = row - 2; x >= 0; x--) {
for (int y = 0; y < column; y++) {
move(Game2048.MOVE_DOWN, x, y, false);
}
}
addCount();
}
/**
* 向左移动
*/
public void left() {
for (int y = 1; y < column; y++) {
for (int x = 0; x < row; x++) {
move(Game2048.MOVE_LEFT, x, y, true);
}
}
createCube();
for (int y = 1; y < column; y++) {
for (int x = 0; x < row; x++) {
move(Game2048.MOVE_LEFT, x, y, false);
}
}
addCount();
}
/**
* 向右移动
*/
public void right() {
for (int y = column - 2; y >= 0; y--) {
for (int x = 0; x < row; x++) {
move(Game2048.MOVE_RIGHT, x, y, true);
}
}
createCube();
for (int y = column - 2; y >= 0; y--) {
for (int x = 0; x < row; x++) {
move(Game2048.MOVE_RIGHT, x, y, false);
}
}
addCount();
}
/**
* 移动
*
* @param move_way
* 移动方向
* @param x
* 横坐标
* @param y
* 纵坐标
*/
private void move(int move_way, int x, int y, boolean isAdd) {
switch (move_way) {
case Game2048.MOVE_DOWN: {
for (; x < row - 1; x++) {
swap(getView(x + 1, y), getView(x, y), isAdd);
}
}
break;
case Game2048.MOVE_LEFT: {
for (; y > 0; y--) {
swap(getView(x, y - 1), getView(x, y), isAdd);
}
}
break;
case Game2048.MOVE_RIGHT: {
for (; y < column - 1; y++) {
swap(getView(x, y + 1), getView(x, y), isAdd);
}
}
break;
case Game2048.MOVE_UP: {
for (; x > 0; x--) {
swap(getView(x - 1, y), getView(x, y), isAdd);
}
}
break;
}
}
/**
* 单向交换实现移动
*
* @param next
* 移动至目标位置
* @param now
* 需要移动的目标
* @param isAdd
* 是否是第一次移动
*/
private void swap(Cube next, Cube now, boolean isAdd) {
if (isAdd) {
if (now.getNum() != 0 && next.getNum() == 0) {
next.setText(now.getNum());
now.setText(0);
next.setIsAdded(now.isAdded());
now.setIsAdded(false);
} else if (!now.isAdded() && !next.isAdded()
&& next.getNum() == now.getNum() && now.getNum() != 0) {
next.setText(now.getNum() * 2);
now.setText(0);
next.setIsAdded(true);
now.setIsAdded(false);
}
} else {
if (next.getNum() == 0) {
next.setText(now.getNum());
now.setText(0);
}
now.setIsAdded(false);
next.setIsAdded(false);
}
}
/**
* 获取指定控件
*
* @param x
* @param y
* @return Cube
*/
private Cube getView(int x, int y) {
return viewList.get(new Point(x, y));
}
/**
* 生成随机控件 随机位置
*/
private void createCube() {
int x;
int y;
do {
x = (int) (Math.random() * 1000 % row);
y = (int) (Math.random() * 1000 % column);
} while (getView(x, y).getNum() != 0);
getView(x, y).setText(Math.random() > 0.5 ? 2 : 4);
isOverGame();
}
/**
* 检测游戏状态
*/
private void isOverGame() {
int score = 0;
int state = Game2048.GAME_OVER;
for (int x = 0; x < row; x++) {
for (int y = 0; y < column; y++) {
// 计算得分
score += getView(x, y).getNum();
if (getView(x, y).getNum() == 0) {
state = Game2048.GAME_CONTINUE;
} else if (getView(x, y).getNum() == 2048 * (gameLv + 1)) {
state = Game2048.GAME_WIN;
}
}
}
if (state != Game2048.GAME_CONTINUE && state != Game2048.GAME_WIN) {
gameState = Game2048.GAME_OVER;
} else {
gameState = state;
}
scoreBoard.setText("得分:" + score);
}
/**
* 计步
*/
private void addCount() {
count++;
arithmometer.setText("计步:" + count);
}
/**
* 获取游戏状态
*
* @return int
*/
public int getGameState() {
return gameState;
}
/**
* 初始化游戏数据
*/
private void initialise() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
getView(i, j).setText(0);
}
}
createCube();
createCube();
count = 0;
arithmometer.setText("计步:" + count);
gameLv = 0;
this.setTitle("2048");
}
/**
* 开启新游戏
*/
public void newGame() {
if (gameState == Game2048.GAME_CONTINUE) {
int jop = JOptionPane.showConfirmDialog(null, "是否开始新一轮游戏?", "Tips",
JOptionPane.YES_NO_OPTION);
if (jop == JOptionPane.YES_OPTION) {
initialise();
}
} else {
initialise();
}
}
/**
* 重载窗体事件-控制关闭
*/
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
if (getGameState() == Game2048.GAME_CONTINUE) {
int jop = JOptionPane.showConfirmDialog(null, "是否退出游戏?",
"Tips", JOptionPane.YES_NO_OPTION);
if (jop == JOptionPane.YES_OPTION) {
super.processWindowEvent(e);
}
} else {
super.processWindowEvent(e);
}
}
}
/**
* 进入下一难度游戏
*/
public void nextLv() {
gameLv++;
this.setTitle(2048 * (gameLv + 1) + "");
}
/**
* 关于
*/
public void about() {
JOptionPane.showMessageDialog(null, "E-mail:czh60601@126.com");
}
}
class Cube {
private int num;
private JButton btn;
private boolean isAdded;
/**
* 构造一个方块
*/
public Cube() {
btn = new JButton();
btn.setFont(new Font("微软雅黑", Font.BOLD, 24));
btn.setEnabled(false);
num = 0;
isAdded = false;
}
/**
* 设置文本内容
*
* @param n
* 数值
*/
public void setText(int n) {
num = n;
btn.setText(n != 0 ? n + "" : "");
}
/**
* 获取控件
*
* @return JButton
*/
public JButton getView() {
return btn;
}
/**
* 获取数值
*
* @return int
*/
public int getNum() {
return num;
}
/**
* 是否是相加而成 限当前移动次有效,移动结束后改回默认值-false
*
* @return`
*/
public boolean isAdded() {
return isAdded;
}
/**
* 修改生成方式
*
* @param b
* true-相加而成
*/
public void setIsAdded(boolean b) {
isAdded = b;
}
}
class MyKeyListener implements KeyListener {
/*
* 键盘代码 w/87 s/83 a/65 d/68 up/38 down/40 left/37 right/39 f1/112 f2/113
* f3/114
*/
final public static int KEY_W = 0xf57;
final public static int KEY_S = 0xf53;
final public static int KEY_A = 0xf41;
final public static int KEY_D = 0xf44;
final public static int KEY_UP = 0xf26;
final public static int KEY_DOWN = 0xf28;
final public static int KEY_LEFT = 0xf25;
final public static int KEY_RIGHT = 0xf27;
private Game2048 game;
/**
* 构造一个键盘监听器
*
* @param game
* 主界面
*/
public MyKeyListener(Game2048 game) {
this.game = game;
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode() + 0xf00;
switch (game.getGameState()) {
case Game2048.GAME_CONTINUE: {
switch (keyCode) {
case MyKeyListener.KEY_W:
case MyKeyListener.KEY_UP: {
game.up();
}
break;
case MyKeyListener.KEY_S:
case MyKeyListener.KEY_DOWN: {
game.down();
}
break;
case MyKeyListener.KEY_A:
case MyKeyListener.KEY_LEFT: {
game.left();
}
break;
case MyKeyListener.KEY_D:
case MyKeyListener.KEY_RIGHT: {
game.right();
}
break;
}
}
break;
case Game2048.GAME_OVER: {
int jop = JOptionPane
.showConfirmDialog(null, "很遗憾,你没能达成本次目标,是否开启新游戏?", "游戏结束",
JOptionPane.YES_NO_OPTION);
if (jop == JOptionPane.YES_OPTION) {
game.newGame();
}
}
break;
case Game2048.GAME_WIN: {
int jop = JOptionPane.showConfirmDialog(null,
"你已完成本次目标:" + game.getTitle() + ",是否进入更高难度游戏?", "恭喜晋级",
JOptionPane.YES_NO_OPTION);
if (jop == JOptionPane.YES_OPTION) {
game.nextLv();
}
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
class MyMouseListener implements MouseListener {
private Game2048 game;
private int btnEvn;
public MyMouseListener(Game2048 game, int btnEvn) {
this.game = game;
this.btnEvn = btnEvn;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
switch (btnEvn) {
case Game2048.BUTTON_NEW_GAME: {
game.newGame();
}
break;
case Game2048.BUTTON_ABOUT: {
game.about();
}
break;
case Game2048.BUTTON_EXIT: {
game.processWindowEvent(new WindowEvent(game,
WindowEvent.WINDOW_CLOSING));
}
break;
}
}
}
俄罗斯方块代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ELS extends JFrame {
public ELS() {
TetrisPanel a = new TetrisPanel();
addKeyListener(a.listener);
add(a);
JMenuBar menu = new JMenuBar();
JMenu game = new JMenu("游戏");
game.add("新游戏");
game.add("暂停");
game.add("继续");
game.add("退出");
JMenu help = new JMenu("帮助");
help.add("关于");
menu.add(game);
menu.add(help);
this.setJMenuBar(menu);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(220, 275);
setTitle("Tetris内测版");
setResizable(false);
}
public static void main(String[] args) {
new ELS().setVisible(true);
}
}
// 创建一个俄罗斯方块类
class TetrisPanel extends JPanel {
public TimerListener listener = new TimerListener();
// blockType 代表方块类型
// turnState代表方块状态
private int blockType;
private int score = 0;
private int turnState;
private int x;
private int y;
int flag = 0;
// 定义已经放下的方块x=0-11,y=0-21;
int[][] map = new int[13][23];
// 方块的形状 第一组代表方块类型有S、Z、L、J、I、O、T 7种 第二组 代表旋转几次 第三四组为 方块矩阵
private final int shapes[][][] = new int[][][] {
// I
{ { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
// T
{ { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } };
// 初始化构造方法
public TetrisPanel() {
nextBlock();
newGame();
new Timer(1000, listener).start();
}
// 生成新方块的方法
private void nextBlock() {
blockType = (int) (Math.random() * 1000) % 7;
turnState = (int) (Math.random() * 1000) % 4;
x = 4;
y = 0;
if (crash(x, y, blockType, turnState) == 0) {
JOptionPane.showMessageDialog(null, "GAME OVER");
newGame();
}
}
// 初始化地图
private void newGame() {
score = 0;
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 22; j++) {
map[i][j] = 0;
map[11][j] = map[0][j] = 3;
}
map[i][21] = 3;
}
}
// 旋转的方法
private void turn() {
turnState = (crash(x, y, blockType, (turnState + 1) % 4) + turnState) % 4;
repaint();
}
// 左移的方法
private void left() {
x -= crash(x - 1, y, blockType, turnState);
repaint();
}
// 右移的方法
private void right() {
x += crash(x + 1, y, blockType, turnState);
repaint();
}
// 下落的方法
private void down() {
y += crash(x, y + 1, blockType, turnState);
if (crash(x, y + 1, blockType, turnState) == 0) {
add(x, y, blockType, turnState);
nextBlock();
}
repaint();
}
// 是否碰撞的方法
private int crash(int x, int y, int blockType, int turnState) {
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if ((shapes[blockType][turnState][a * 4 + b] & map[x + b + 1][y
+ a]) == 1) {
return 0;
}
}
}
return 1;
}
// 尝试消行的方法
private void tryDelLine() {
for (int b = 0; b < 21; b++) {
int c = 1;
for (int a = 0; a < 12; a++) {
c &= map[a][b];
}
if (c == 1) {
score += 10;
for (int d = b; d > 0; d--) {
for (int e = 0; e < 11; e++) {
map[e][d] = map[e][d - 1];
}
}
}
}
}
// 把当前添加map
private void add(int x, int y, int blockType, int turnState) {
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
map[x + b + 1][y + a] |= shapes[blockType][turnState][a * 4 + b];
}
}
tryDelLine();
}
// 画方块的的方法
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 画当前方块
for (int j = 0; j < 16; j++) {
if (shapes[blockType][turnState][j] == 1) {
g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10);
}
}
// 画已经固定的方块
for (int j = 0; j < 22; j++) {
for (int i = 0; i < 12; i++) {
if (map[i][j] == 1) {
g.fillRect(i * 10, j * 10, 10, 10);
} else if (map[i][j] == 3) {
g.drawRect(i * 10, j * 10, 10, 10);
}
}
}
g.drawString("score=" + score, 125, 10);
g.drawString("抵制不良游戏,", 125, 50);
g.drawString("拒绝盗版游戏。", 125, 70);
g.drawString("注意自我保护,", 125, 90);
g.drawString("谨防受骗上当。", 125, 110);
g.drawString("适度游戏益脑,", 125, 130);
g.drawString("沉迷游戏伤身。", 125, 150);
g.drawString("合理安排时间,", 125, 170);
g.drawString("享受健康生活。", 125, 190);
}
// 定时器监听和键盘监听
class TimerListener extends KeyAdapter implements ActionListener {
public void actionPerformed(ActionEvent e) {
down();
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_UP:
turn();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_LEFT:
left();
}
}
}
}