2048朝代版
展示一下窗口界面,最为一个窗口程序,友好的界面让人耳目一新。
学习了一学期的java语言,学校的实训内容是要做一个窗口游戏,2048朝代版。整个游戏没有什么难度,唯一有难度的也就是算法方面了,不断进行着清空格、合并的重复操作。我的游戏思路很简单,通过键盘操作一个二维数组,界面根据二维数组进行不断的重画 ,就是这么简单。直接上代码,如果有想要源代码即图片,请通过主页下载。
WindowView类:
public class WindowView extends JFrame{
private static final long serialversionuid = 1L;
//游戏状态
public static boolean gameState = false;
//游戏分数
public static int gameScore = 0;
//窗体大小 9a866d
public static final int VIEW_WIDTH = 520;
public static final int VIEW_HEIGHT = 800;
//面板显示
private GamePanel gamePanel = new GamePanel();
private WelcomPanel welcomPanel = new WelcomPanel(gamePanel);
public static void main(String[] args) {
new WindowView().init();
}
//
public void init() {
this.setSize(VIEW_WIDTH, VIEW_HEIGHT);
this.setlayout(null);
this.setResizable(false);
this.setLocationRelativeTo(null);
//this.setUndecorated(true);
this.addKeyListener(new KeyControl(gamePanel.arithmetic));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(gamePanel);
gamePanel.setVisible(false);
this.add(welcomPanel);
this.setVisible(true);
}
}
WelcomPanel类:
public class WelcomPanel extends JPanel{
private static final long serialVersionUID = 1L;
//面板大小
private final int VIEW_WIDTH = WindowView.VIEW_WIDTH;
private final int VIEW_HEIGHT = WindowView.VIEW_HEIGHT;
//控件大小
private final int JL_WIDTH = 200;
private final int JL_HEIGHT = 50;
//控件实例化
private JLabel background = new JLabel(new ImageIcon("img/hello.png"));
private ImageIcon start1 = new ImageIcon("img/开始游戏.png");
private ImageIcon start2 = new ImageIcon("img/开始游戏经过.png");
private JLabel start = new JLabel(start1);
private ImageIcon record1 = new ImageIcon("img/最高纪录.png");
private ImageIcon record2 = new ImageIcon("img/最高纪录经过.png");
private JLabel record = new JLabel(record1);
private ImageIcon about1 = new ImageIcon("img/关于游戏.png");
private ImageIcon about2 = new ImageIcon("img/关于游戏经过.png");
private JLabel about = new JLabel(about1);
private ImageIcon exit1 = new ImageIcon("img/退出游戏.png");
private ImageIcon exit2 = new ImageIcon("img/退出游戏经过.png");
private JLabel exit = new JLabel(exit1);
//实例化鼠标监听器
private JLableListener JLableListener = new JLableListener(this);
private GamePanel gamePanel;
//构造器
public WelcomPanel(GamePanel gamePanel) {
this.gamePanel = gamePanel;
this.init();
}
//面板属性设置
public void init() {
this.setLayout(null);
this.setBounds(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
start.setBounds((VIEW_WIDTH - JL_WIDTH)/2, VIEW_HEIGHT - 9 * JL_HEIGHT, JL_WIDTH, JL_HEIGHT);
start.addMouseListener(JLableListener);
this.add(start);
record.setBounds((VIEW_WIDTH - JL_WIDTH)/2, VIEW_HEIGHT - 7 * JL_HEIGHT, JL_WIDTH, JL_HEIGHT);
record.addMouseListener(JLableListener);
this.add(record);
about.setBounds((VIEW_WIDTH - JL_WIDTH)/2, VIEW_HEIGHT - 5 * JL_HEIGHT, JL_WIDTH, JL_HEIGHT);
about.addMouseListener(JLableListener);
this.add(about);
exit.setBounds((VIEW_WIDTH - JL_WIDTH)/2, VIEW_HEIGHT - 3 * JL_HEIGHT, JL_WIDTH, JL_HEIGHT);
exit.addMouseListener(JLableListener);
this.add(exit);
background.setBounds( 0, 0, VIEW_WIDTH, VIEW_HEIGHT);
this.add(background);
}
//匿名内部鼠标监听器
private class JLableListener implements MouseListener{
private WelcomPanel welcomPanel;
public JLableListener(WelcomPanel welcomPanel) {
this.welcomPanel = welcomPanel;
}
//点击并释放
public void mouseClicked(MouseEvent e) {
if(e.getSource() == start) {
welcomPanel.removeAll();
gamePanel.setVisible(true);
WindowView.gameState = true;
}else if(e.getSource() == record) {
new Record();
}else if(e.getSource() == about) {
new AboutView();
}else if(e.getSource() == exit) {
System.exit(0);
}
}
//鼠标进入调用
public void mouseEntered(MouseEvent e) {
if(e.getSource() == start) {
start.setIcon(start2);
}else if(e.getSource() == record) {
record.setIcon(record2);
}else if(e.getSource() == about) {
about.setIcon(about2);
}else if(e.getSource() == exit) {
exit.setIcon(exit2);
}
}
//离开时调用
public void mouseExited(MouseEvent e) {
if(e.getSource() == start) {
start.setIcon(start1);
}else if(e.getSource() == record) {
record.setIcon(record1);
}else if(e.getSource() == about) {
about.setIcon(about1);
}else if(e.getSource() == exit) {
exit.setIcon(exit1);
}
}
//按下调用
public void mousePressed(MouseEvent e) {}
//释放调用
public void mouseReleased(MouseEvent e) {}
}
}
Record类:
public class Record extends JFrame{
//面板大小
private final int VIEW_HEIGHT = 400;
private final int VIEW_WIDTH = 300;
//面板背景图
private JLabel background = new JLabel(new ImageIcon("img/排行.png"));
private ImageIcon back1 = new ImageIcon("img/未标题-2.png");
private ImageIcon back2 = new ImageIcon("img/未标题-4.png");
private JLabel back = new JLabel(back1);
private JLabel score = new JLabel("",JLabel.CENTER);
//本类的对象用于内部类使用
private Record record = this;
//构造器
public Record() {
this.init();
score.setText(GamePanel.mostScore);
}
//面板属性
public void init() {
this.setSize(VIEW_WIDTH, VIEW_HEIGHT);
this.setLayout(null);
this.setResizable(false);
this.setUndecorated(true);
this.setLocationRelativeTo(null);
this.setBackground(new color(0, 0, 0, 0));
score.setFont(new Font("微软雅黑", 1, 30));
score.setForeground(new Color(154,134,109));
score.setBounds(VIEW_WIDTH-125, 20, 100, 100);
score.setBounds(0,0, VIEW_WIDTH, VIEW_HEIGHT);
this.add(score);
back.setBounds(VIEW_WIDTH - 50,20, 30, 30);
back.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
record.dispose();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
back.setIcon(back2);
}
public void mouseExited(MouseEvent e) {
back.setIcon(back1);
}
});
this.add(back);
background.setBounds(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
this.add(background);
this.setVisible(true);
}
private File file;
public void readFile() {
file = new File("d:\\score.txt");
fileinputstream in = null;
try {
in = new FileInputStream(file);
byte byt[] = new byte[1024];
int len = in.read(byt);
GamePanel.mostScore = new String(byt, 0, len);
this.score.setText(new String(byt, 0, len));
}catch(Exception e) {
e.printstacktrace();
}finally {
try {
if(in != null) in.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
PaintPanel类:
public class PaintPanel extends JPanel{
private static final long serialVersionUID = 1L;
private Toolkit kit = Toolkit.getDefaultToolkit();
//位图
private Image ImageBuffer = null;
private Graphics GraImage = null;
//面板大小
public final int VIEW_WIDTH = 500;
public final int VIEW_HEIGHT = 500;
//背景
private Image background = kit.getImage("img/gamebackground.png");
//创建算法类引用对象
private Arithmetic arithmetic;
private PaintPanel paintPanel = this;
//构造器
public PaintPanel(Arithmetic arithmetic) {
new Thread() {
public void run(){
while(true) {
paintPanel.repaint();
}
}
}.start();
this.arithmetic = arithmetic;
}
//画图
public void paint(Graphics g) {
g.drawImage(background, 0, 0, VIEW_WIDTH, VIEW_HEIGHT, this);
this.drawImg(g);
}
//双缓冲解决游戏闪烁的问题
public void update(Graphics g) {
//创建图形缓冲区
ImageBuffer = createImage(VIEW_WIDTH, VIEW_HEIGHT);
//获取图形缓冲区的图形上下文
GraImage = ImageBuffer.getGraphics();
//用paint方法中编写的绘图过程对图形缓冲区绘图
paint(GraImage);
//释放图形上下文资源
GraImage.dispose();
//将图形缓冲区绘制到屏幕上
g.drawImage(ImageBuffer, 0, 0, this);
}
//私有方法实现画格
private void drawImg(Graphics g) {
for(int i = 0; i < arithmetic.getData().length; i++) {
for(int n = 0; n < arithmetic.getData()[i].length; n++) {
switch(arithmetic.getData()[i][n]) {
case 0:
g.drawImage(kit.getImage("img/空.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 1:
g.drawImage(kit.getImage("img/夏.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 2:
g.drawImage(kit.getImage("img/商.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 4:
g.drawImage(kit.getImage("img/周.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 8:
g.drawImage(kit.getImage("img/秦.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 16:
g.drawImage(kit.getImage("img/汉.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 32:
g.drawImage(kit.getImage("img/三国.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 64:
g.drawImage(kit.getImage("img/晋.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 128:
g.drawImage(kit.getImage("img/南北朝.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 256:
g.drawImage(kit.getImage("img/隋.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 512:
g.drawImage(kit.getImage("img/唐.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 1024:
g.drawImage(kit.getImage("img/五代.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 2048:
g.drawImage(kit.getImage("img/辽.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 4096:
g.drawImage(kit.getImage("img/宋.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 8192:
g.drawImage(kit.getImage("img/金.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 16384:
g.drawImage(kit.getImage("img/元.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 32768:
g.drawImage(kit.getImage("img/明.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
case 65536:
g.drawImage(kit.getImage("img/清.png"), i*100+(i+1)*20, n*100+(n+1)*20, this);
break;
}
}
}
}
}
Hint类:
public class Hint extends JFrame{
//面板大小
private final int VIEW_HEIGHT = 400;
private final int VIEW_WIDTH = 300;
//面板背景图
private JLabel background = new JLabel(new ImageIcon("img/排行.png"));
//重新开始按钮
private ImageIcon start1 = new ImageIcon("img/重新开始.png");
private ImageIcon start2 = new ImageIcon("img/重新开始2.png");
private JLabel start = new JLabel(start1);
//退出按钮
private ImageIcon exit1 = new ImageIcon("img/退出1.png");
private ImageIcon exit2 = new ImageIcon("img/退.png");
private JLabel exit = new JLabel(exit1);
//本类的对象用于内部类使用
private Hint hint = this;
//鼠标监听事件
private Mouse mouse = new Mouse();
//创建 对象
Arithmetic arithmetic;
//构造器
public Hint(Arithmetic arithmetic) {
this.arithmetic = arithmetic;
this.init();
this.writeScore();
}
//面板属性
public void init() {
this.setSize(VIEW_WIDTH, VIEW_HEIGHT);
this.setLayout(null);
this.setResizable(false);
this.setUndecorated(true);
this.setLocationRelativeTo(null);
this.setBackground(new Color(0, 0, 0, 0));
this.addMouseListener(mouse);
start.setBounds((VIEW_WIDTH - 150)/2, 160, 150, 40);
start.addMouseListener(mouse);
this.add(start);
exit.setBounds((VIEW_WIDTH - 150)/2, 230, 150, 40);
exit.addMouseListener(mouse);
this.add(exit);
background.setBounds(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
this.add(background);
this.setVisible(true);
}
private File file;
public void writeScore() {
file = new File("d:\\score.txt");
Fileoutputstream out = null;
try {
out = new FileOutputStream(file);
byte buy[] =(""+WindowView.gameScore).getBytes();
out.write(buy);
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(out != null) out.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
//鼠标监听事件内部类
private class Mouse implements MouseListener {
private Random random = new Random();
public void mouseClicked(MouseEvent e) {
if(e.getSource() == start) {
//清空数组信息
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
arithmetic.getData()[i][j] = 0;
}
}
WindowView.gameScore = 0;
arithmetic.getData()[random.nextint(3)][random.nextInt(3)] = 1;
WindowView.gameState = true;
//释放当前窗口资源
hint.dispose();
}else if(e.getSource() == exit){
//结束程序进程
System.exit(0);
}
}
//按下调用
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
if(e.getSource() == start) {
start.setIcon(start2);
}else if(e.getSource() == exit){
exit.setIcon(exit2);
}
}
public void mouseExited(MouseEvent e) {
if(e.getSource() == start) {
start.setIcon(start1);
}else if(e.getSource() == exit){
exit.setIcon(exit1);
}
}
}
}
GamePanel类:
public class GamePanel extends JPanel {
private static final long serialVersionUID = 1L;
//面板大小
private final int VIEW_WIDTH = WindowView.VIEW_WIDTH;
private final int VIEW_HEIGHT = WindowView.VIEW_HEIGHT;
//全局颜色
private final Color COLOR_BACKGROUND = new Color(154,134,109);
//面板顶部控件
private JLabel title = new JLabel(new ImageIcon("img/topBackgtound.png"));
//显示分数控件
public static JLabel score = new JLabel(WindowView.gameScore + "",JLabel.CENTER);
//最高分数
public static String mostScore = "";
//算法类
public Arithmetic arithmetic = new Arithmetic();
//画板实例化
public PaintPanel paintPanel = new PaintPanel(arithmetic);
private JLabel newGame= new JLabel(new ImageIcon("img/重玩1.png"));
//构造器
public GamePanel() {
this.init();
}
//面板属性设置
public void init() {
this.setLayout(null);
this.setBackground(COLOR_BACKGROUND);
this.setBounds(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
this.setBackground(new Color(255,239,213));
score.setFont(new Font("微软雅黑", 1, 30));
score.setForeground(new Color(154,134,109));
score.setBounds(VIEW_WIDTH-125, 20, 100, 100);
this.add(score);
title.setBounds(0, 0, VIEW_WIDTH, 150);
this.add(title);
paintPanel.setBounds(10, 170, paintPanel.VIEW_WIDTH, paintPanel.VIEW_HEIGHT);
this.add(paintPanel);
newGame.setBounds(10, paintPanel.getY()+paintPanel.getHeight()+20, 70, 40);
this.add(newGame);
}
}
AboutView类:
public class AboutView extends JFrame{
private final int VIEW_HEIGHT = 400;
private final int VIEW_WIDTH = 550;
private JLabel background = new JLabel(new ImageIcon("img/关于弹窗.png"));
private ImageIcon back1 = new ImageIcon("img/未标题-2.png");
private ImageIcon back2 = new ImageIcon("img/未标题-4.png");
private JLabel back = new JLabel(back1);
private AboutView aboutView = this;
public AboutView() {
this.init();
}
public void init() {
this.setSize(VIEW_WIDTH, VIEW_HEIGHT);
this.setLayout(null);
this.setResizable(false);
this.setUndecorated(true);
this.setLocationRelativeTo(null);
this.setBackground(new Color(0, 0, 0, 0));
back.setBounds(VIEW_WIDTH - 50,20, 30, 30);
back.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
aboutView.dispose();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
back.setIcon(back2);
}
public void mouseExited(MouseEvent e) {
back.setIcon(back1);
}
});
this.add(back);
background.setBounds(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
this.add(background);
this.setVisible(true);
}
}
KeyControl类:
public class KeyControl implements KeyListener{
private Arithmetic arithmetic;
public KeyControl(Arithmetic arithmetic) {
this.arithmetic = arithmetic;
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(WindowView.gameState) {
switch(e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
arithmetic.moveRight();
break;
case KeyEvent.VK_DOWN:
arithmetic.moveDown();
break;
case KeyEvent.VK_LEFT:
arithmetic.moveLeft();
break;
case KeyEvent.VK_UP:
arithmetic.moveUp();
break;
}
GamePanel.score.setText(""+WindowView.gameScore);
}
}
public void keyReleased(KeyEvent e) {
}
}
Arithmetic类:
public class Arithmetic {
//产生随机数
private Random random = new Random();
//表示地图的二维数组
private int data[][] = new int[4][4];
//构造器
public Arithmetic() {
this.newNumber();
}
//初始化数组数据
public void newNumber() {
int x;
int y;
int num = 0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(data[i][j] == 0) {
num++;
}
}
}
if(num == 0) {
WindowView.gameState = false;
new Hint(this);
return;
}
while(true) {
x = random.nextInt(4);
y = random.nextInt(4);
if(data[x][y] == 0) {
if(random.nextInt(5) == 1) {
data[x][y] = 2;
}else {
data[x][y] = 1;
}
break;
}
}
}
// 左移
public void moveUp(){
for(int i=0;i<4;i++){
for(int j=0;j<3;j++){
if(data[i][j] == data[i][j+1]){
data[i][j] += data[i][j+1];
data[i][j+1] = 0 ;
WindowView.gameScore++;
}
clearUp();
}
}
newNumber();
}
// 清除左边的0
public void clearUp(){
for(int i = 0 ; i < 4 ; i++){
for(int j = 0 ; j < 4 ; j++){
for(int k = j ; k >= 0 ; k-- ){
if(data[i][j]!= 0 && data[i][k] == 0){
data[i][k] = data[i][j];
data[i][j] = 0 ;
}
}
}
}
}
// 右移
public void moveDown(){
for(int i=0;i<4;i++){
for(int j=0;j<3;j++){
if(data[i][j] == data[i][j+1]){
data[i][j+1] += data[i][j];
data[i][j] = 0 ;
WindowView.gameScore++;
}
clearDown();
}
}
newNumber();
}
// 清除右边的0
public void clearDown(){
for(int i = 0 ; i < 4 ; i++){
for(int j = 3 ; j >= 0 ; j-- ){
for(int k = j ; k < 4 ; k++ ){
if(data[i][j]!= 0 && data[i][k] == 0){
data[i][k] = data[i][j];
data[i][j] = 0 ;
}
}
}
}
}
// 上移
public void moveLeft(){
for(int j=0;j<4;j++){
for(int i=0;i<3;i++){
if(data[i][j] == data[i+1][j]){
data[i][j] += data[i+1][j];
data[i+1][j] = 0 ;
WindowView.gameScore++;
}
clearLeft();
}
}
newNumber();
}
// 清除上方的0
public void clearLeft(){
for(int j = 0 ; j < 4 ; j++){
for(int i = 0 ; i <4 ; i++){
for(int k = i ; k >=0 ; k-- ){
if(data[i][j]!= 0 && data[k][j] == 0){
data[k][j] = data[i][j];
data[i][j] = 0 ;
}
}
}
}
}
// 下移
public void moveRight (){
for(int j=0;j<4;j++){
for(int i=3;i>0;i--){
if(data[i][j] == data[i-1][j]){
data[i][j] += data[i-1][j];
data[i-1][j] = 0 ;
WindowView.gameScore++;
}
clearRight();
}
}
newNumber();
}
// 清除下方的0
public void clearRight(){
for(int j = 0 ; j < 4 ; j++){
for(int i = 3 ; i >= 0 ; i--){
for(int k = i ; k < 4 ; k++ ){
if(data[i][j]!= 0 && data[k][j] == 0){
data[k][j] = data[i][j];
data[i][j] = 0 ;
}
}
}
}
}
//访问器
public int[][] getData() {
return data;
}
public void setData(int data[][]) {
this.data = data;
}
}