java2048⼩游戏源码及解析_200⾏java代码实现2048⼩游戏本⽂实例为⼤家分享了java实现2048⼩游戏的具体代码,供⼤家参考,具体内容如下
效果图:
游戏介绍:
1.2048是⼀款益智类⼩游戏,刚开始随机出现两个数字,可以上下左右控制数字的移动。
2.当选择⼀个⽅向移动后,所有数字都会沿该⽅向移动到表格尽头,并且空余表格会随机出现2或4,当碰到相同的两个数字时,该两个数字会合并相加成⼀个数字,直到最⼤的数字变成2048游戏成功
3.否则当数字填满表格且不能再移动时游戏失败。
游戏代码:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Game2048 extends JPanel {
enum State {
start, won, running, over
}
final Color[] colorTable = { new Color(0x701710), new Color(0xFFE4C3), new Color(0xfff4d3), new Color(0xffdac3), new Color(0xe7b08e), new Color(0xe7bf8e), new Color(0xffc4c3), new Color(0xE7948e), new Color(0xbe7e56), new Color(0xbe5e56), new Color(0x9c3931), new Color(0x701710) };
final static int target = 2048;
static int highest;
static int score;
private Color gridColor = new Color(0xBBADA0);
private Color emptyColor = new Color(0xCDC1B4);
private Color startColor = new Color(0xFFEBCD);
private Random rand = new Random();
private Tile[][] tiles;
private int side = 4;
private State gamestate = State.start;
private boolean checkingAvailableMoves;
public Game2048() {
setPreferredSize(new Dimension(900, 700));
setBackground(new Color(0xFAF8EF));
setFont(new Font("SansSerif", Font.BOLD, 48));
setFocusable(true);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startGame();
repaint();
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyPressedtkOejs(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
moveUp();
break;
case KeyEvent.VK_DOWN:
moveDown();
break;
case KeyEvent.VK_LEFT:
moveLeft();
break;
case KeyEvent.VK_RIGHT:
moveRight();
break;
}
repaint();
}
});
}
@Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); drawGrid(g);
}
python转java代码void startGame() {
if (gamestate != State.running) {
score = 0;
highest = 0;
gamestate = State.running;
tiles = new Tile[side][side];
addRandomTile();
addRandomTile();
}
}
void drawGrid(Graphics2D g) {
g.setColor(gridColor);
g.fillRoundRect(200, 100, 499, 499, 15, 15);
if (gamestate == State.running) {
for (int r = 0; r < side; r++) {
for (int c = 0; c < side; c++) {
if (tiles[r][c] == null) {
g.setColor(emptyColor);
g.fillRoundRect(215 + c * 121, 115 + r * 121, 106, 106, 7, 7); } else {
drawTile(g, r, c);
}
}
}
} else {
g.setColor(startColor);
tkOejSitle("2048");
f.setResizable(true);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class Tile {
private boolean merged;
private int value;
Tile(int val) {
value = val;
}
int getValue() {
return value;
}
void setMerged(boolean m) {
merged = m;
}
boolean canMergeWith(Tile other) {
return !merged && other != null && !d && value == Value(); }
int mergeWith(Tile other) {
if (canMergeWith(other)) {
value *= 2;
merged = true;
return value;
}
return -1;
}
}
更多有趣的经典⼩游戏实现专题,分享给⼤家:
C++经典⼩游戏汇总
python经典⼩游戏汇总
python俄罗斯⽅块游戏集合
java经典⼩游戏汇总
javascript经典⼩游戏汇总
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持我们。
本⽂标题: 200⾏java代码实现2048⼩游戏
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论