用java简单实现2048

第一次写除了学生信息管理系统之类的程序之外的东西,一共用了差不多四天时间,主要是没写过,所以各方面都用了很久,相信如果再写一个可能只用不到一天就够了
这个程序我分为两个类来写,一个是主类,还要一个是继承JPanel的类,在类中重写了paint方法用来画出游戏界面的背景图案等

package temp2048;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Sur {
	// ----------------------------------------------以下是声明全局变量
	public static int[][] number = new int[4][4];
	public static int wer;
	public static int randomnumber0;
	public static Random random = new Random();
	public static myPanel p14 = new myPanel();
	public static int score1 = 0;
	public static int score2 = 0;
	
	public static void main(String[] args) {
		File file = new File("C:\\Users\\86178\\Documents\\Tencent Files\\582861996\\FileRecv\\2048");
		Reader in = null;
		try {
			in = new BufferedReader(new FileReader(file));
			StreamTokenizer stok = new StreamTokenizer(in);
			stok.parseNumbers();
			double sum = 0;
			stok.nextToken();
			while (stok.ttype != StreamTokenizer.TT_EOF) {
				if (stok.ttype == StreamTokenizer.TT_NUMBER)
					sum += stok.nval;
				stok.nextToken();
			}
			score2 = (int) sum;
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		JFrame permary = new JFrame("2048");
		Container c = permary.getContentPane();
		// ----------------------------------------以下为p面板
		JPanel p = new JPanel();
		CardLayout card = new CardLayout();
		p.setLayout(card);

		permary.add(p);
		// -----------------------以下为p0面板
		JPanel p0 = new JPanel();

		p0.setLayout(null);
		ImageIcon image = new ImageIcon("2048图片.jpg");
		JLabel l1 = new JLabel(image);
		JButton b1 = new JButton("开始");
		JButton b2 = new JButton("最高分");
		JButton b3 = new JButton("退出");
		l1.setSize(800, 400);
		b1.setSize(500, 170);
		b2.setSize(500, 170);
		b3.setSize(500, 170);
		l1.setLocation(0, 10);
		b1.setLocation(150, 430);
		b2.setLocation(150, 620);
		b3.setLocation(150, 790);
		p0.add(l1);
		p0.add(b1);
		p0.add(b2);
		p0.add(b3);
		p.add("0", p0);
		// -------------------------------------------------------以上为主界面布局
		JPanel p1 = new JPanel();
		p1.setLayout(null);
		ImageIcon image1 = new ImageIcon("2048图片.jpg");
		image1.setImage(image1.getImage().getScaledInstance(805, 150, Image.SCALE_DEFAULT));
		JLabel l2 = new JLabel(image1);
		l2.setSize(805, 150);
		l2.setLocation(0, 0);
		p14.setFocusable(true);
		p14.setLocation(0, 150);
		p14.setSize(800, 850);
		p1.add(l2);
		p1.add(p14);
		p.add("1", p1);// 将游戏面板加入内容窗格,内容窗格为卡片布局
		// ------------------------------------------------------以上为游戏界面布局
		JPanel p2 = new JPanel();
		JButton b21 = new JButton("返回");
		JLabel l12 = new JLabel("当前的最高分数为:");
		l12.setFont(new Font("黑体", Font.HANGING_BASELINE, 80));
		JTextField field2 = new JTextField(6);
		field2.setSize(6, 80);
		field2.setFont(new Font("黑体", Font.HANGING_BASELINE, 80));
		field2.setText(Integer.toString(score2));
		p2.add(l12);
		p2.add(field2);
		p2.add(b21);
		p.add("2", p2);
		// ------------------------------------------------------------------------以上为历史记录界面布局
		permary.setLocation(500, 0);
		permary.setSize(805, 1030);
		permary.setVisible(true);
		permary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		permary.setResizable(false);

		// -------------------------------------------------------------------------
		ActionListener cut = new ActionListener() {// 切换界面的布局管理器
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == b1) {// 开始按钮
					card.show(p, "1");
					for (int i = 0; i < 4; i++) {// 对数组赋初值
						for (int j = 0; j < 4; j++) {
							number[i][j] = 0;
						}
					}
					pp();
					pp();
				}
				if (e.getSource() == b2) {// 最高分按钮
					card.show(p, "2");
				}
				if (e.getSource() == b3) {// 退出按钮
					System.exit(-1);
				}
				if (e.getSource() == b21) {
					card.show(p, "0");
				}
			}
		};

		b1.addActionListener(cut);
		b2.addActionListener(cut);
		b3.addActionListener(cut);
		b21.addActionListener(cut);
	}

	public static void pp() {
		p14.requestFocus(true);
		randomnumber0 = random.nextInt(10);
		if (randomnumber0 < 8) {
			randomnumber0 = 2;
		} else {
			randomnumber0 = 4;
		}
		boolean key0 = true;
		while (key0) {
			wer = random.nextInt(16);
			int x = wer / 4;
			int y = wer % 4;
			if (y == 0)
				y = 4;
			if (number[x][y - 1] == 0) {
				number[x][y - 1] = randomnumber0;
				key0 = false;
			}
		}
	}
}

下面是第二个类

package temp2048;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class myPanel extends JPanel implements KeyListener {
	JTextField field1;

	public myPanel() {
		addKeyListener(this);
		JLabel l11 = new JLabel("分数:");
		l11.setFont(new Font("黑体", Font.HANGING_BASELINE, 23));
		field1 = new JTextField(6);
		field1.setFont(new Font("黑体", Font.HANGING_BASELINE, 23));
		field1.setPreferredSize(new Dimension(6, 40));
		field1.setText(Integer.toString(Sur.score1));
		this.add(l11);
		this.add(field1);
	}

	public void guess() {
		boolean win = false;
		boolean lose = true;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (Sur.number[i][j] == 0) {
					lose = false;
				}
				if (Sur.number[i][j] == 2048) {
					win = true;
				}
			}
		}
		if (win == true) {
			JOptionPane.showMessageDialog(this, "胜利");
			if (Sur.score1 > Sur.score2) {
				Sur.score2 = Sur.score1;
				try {
					FileOutputStream put = new FileOutputStream(
							"C:\\Users\\86178\\Documents\\Tencent Files\\582861996\\FileRecv\\2048");
					try {
						put.write(Sur.score2);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						put.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.exit(-1);
		}
		if (lose == true) {
			JOptionPane.showMessageDialog(this, "失败");
			if (Sur.score1 > Sur.score2) {
				Sur.score2 = Sur.score1;
			}
			try {
				FileOutputStream put = new FileOutputStream(
						"C:\\Users\\86178\\Documents\\Tencent Files\\582861996\\FileRecv\\2048");
				try {
					String s = Integer.toString(Sur.score2);
					byte[] b = s.getBytes();
					for (int i = 0; i < b.length; i++) {
						System.out.println();
					}

					put.write(b);
					put.flush();

				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					put.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.exit(-1);
		}

	}

	public void paint(Graphics g) {// 重写绘画方法
		super.paint(g);
		g.setColor(new Color(0xBBADA0));
		g.fillRoundRect(0, 50, 800, 800, 18, 18);
		g.setColor(new Color(0xCDC184));
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				g.fillRoundRect(5 + i * 200, 60 + j * 200, 180, 180, 25, 25);
			}
		}

		// ------------------------
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (Sur.number[j][i] != 0) {
					int FontSize = 30;
					int MoveX = 0, MoveY = 0;
					switch (Sur.number[j][i]) {
					case 2:
						g.setColor(new Color(0xeee4da));
						FontSize = 60;
						MoveX = 20;
						MoveY = 0;
						break;
					case 4:
						g.setColor(new Color(0xede0c8));
						FontSize = 60;
						MoveX = 20;
						MoveY = 0;
						break;
					case 8:
						g.setColor(new Color(0xf2b179));
						FontSize = 60;
						MoveX = 20;
						MoveY = 0;
						break;
					case 16:
						g.setColor(new Color(0xf59563));
						FontSize = 55;
						MoveX = 10;
						MoveY = 0;
						break;
					case 32:
						g.setColor(new Color(0xf67c5f));
						FontSize = 55;
						MoveX = 10;
						MoveY = 0;
						break;
					case 64:
						g.setColor(new Color(0xf65e3b));
						FontSize = 55;
						MoveX = 10;
						MoveY = 0;
						break;
					case 128:
						g.setColor(new Color(0xedcf72));
						FontSize = 50;
						MoveX = 5;
						MoveY = 0;
						break;
					case 256:
						g.setColor(new Color(0xedcc61));
						FontSize = 50;
						MoveX = 5;
						MoveY = 0;
						break;
					case 512:
						g.setColor(new Color(0xedc850));
						FontSize = 50;
						MoveX = 5;
						MoveY = 0;
						break;
					case 1024:
						g.setColor(new Color(0xedc53f));
						FontSize = 50;
						MoveX = -5;
						MoveY = 0;
						break;
					case 2048:
						g.setColor(new Color(0xedc22e));
						FontSize = 50;
						MoveX = -5;
						MoveY = 0;
						break;
					default:
						g.setColor(new Color(0x000000));
						break;
					}
					g.fillRoundRect(5 + i * 200, 60 + j * 200, 180, 180, 25, 25);
					g.setColor(new Color(0x000000));
					g.setFont(new Font("Arial", Font.PLAIN, FontSize));
					g.drawString(Sur.number[j][i] + "", 5 + i * 200 + 50 + MoveX, 120 + j * 200 + 50 + MoveY);
				}
			}
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			guess();
			boolean v = false;// 检查有没有进行移动或合并操作
			boolean key = true;
			boolean flag = true;
			boolean mark = true;
			while (key) {
				while (mark) {
					mark = false;
					for (int i = 0; i < 3; i++) {
						for (int j = 0; j < 4; j++) {
							if ((Sur.number[i][j] == 0) && (Sur.number[i + 1][j] != 0)) {
								v = true;
								mark = true;
								int temp = Sur.number[i][j];
								Sur.number[i][j] = Sur.number[i + 1][j];
								Sur.number[i + 1][j] = temp;
							}
						}
					}
				} // while(mark)
				key = false;
				while (flag) {
					flag = false;
					for (int i = 0; i < 3; i++) {
						for (int j = 0; j < 4; j++) {
							if ((Sur.number[i][j] == Sur.number[i + 1][j]) && Sur.number[i][j] != 0) {
								v = true;
								key = true;
								mark = true;
								Sur.number[i][j] = Sur.number[i][j] * 2;
								Sur.number[i + 1][j] = 0;
								Sur.score1 += Sur.number[i][j];
							}

						}
					}
				} // while(flag)
			} // while(key)
			while (v) {
				Sur.pp();
				v = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			guess();
			boolean key = true;
			boolean flag = true;
			boolean mark = true;
			boolean v = false;
			while (key) {
				while (mark) {
					mark = false;
					for (int i = 3; i > 0; i--) {
						for (int j = 0; j < 4; j++) {
							if ((Sur.number[i][j] == 0) && (Sur.number[i - 1][j] != 0)) {
								v = true;
								mark = true;
								int temp = Sur.number[i][j];
								Sur.number[i][j] = Sur.number[i - 1][j];
								Sur.number[i - 1][j] = temp;
							}
						}
					}
				} // while(mark)
				key = false;
				while (flag) {
					flag = false;
					for (int i = 3; i > 0; i--) {
						for (int j = 0; j < 4; j++) {
							if ((Sur.number[i][j] == Sur.number[i - 1][j]) && Sur.number[i][j] != 0) {
								v = true;
								key = true;
								mark = true;
								Sur.number[i][j] = Sur.number[i][j] * 2;
								Sur.number[i - 1][j] = 0;
								Sur.score1 += Sur.number[i][j];
							}

						}
					}
				} // while(flag)
			} // while(key)
			while (v) {
				Sur.pp();
				v = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			guess();
			boolean key = true;
			boolean flag = true;
			boolean mark = true;
			boolean v = false;
			while (key) {
				while (mark) {
					mark = false;
					for (int i = 0; i < 4; i++) {
						for (int j = 0; j < 3; j++) {
							if ((Sur.number[i][j] == 0) && (Sur.number[i][j + 1] != 0)) {
								v = true;
								mark = true;
								int temp = Sur.number[i][j];
								Sur.number[i][j] = Sur.number[i][j + 1];
								Sur.number[i][j + 1] = temp;
							}
						}
					}
				} // while(mark)
				key = false;
				while (flag) {
					flag = false;
					for (int i = 0; i < 4; i++) {
						for (int j = 0; j < 3; j++) {
							if ((Sur.number[i][j] == Sur.number[i][j + 1]) && Sur.number[i][j] != 0) {
								v = true;
								key = true;
								mark = true;
								Sur.number[i][j] = Sur.number[i][j] * 2;
								Sur.number[i][j + 1] = 0;
								Sur.score1 += Sur.number[i][j];
							}

						}
					}
				} // while(flag)
			} // while(key)
			while (v) {
				Sur.pp();
				v = false;
			}
		} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			guess();
			boolean key = true;
			boolean flag = true;
			boolean mark = true;
			boolean v = false;
			while (key) {
				while (mark) {
					mark = false;
					for (int i = 0; i < 4; i++) {
						for (int j = 3; j > 0; j--) {
							if ((Sur.number[i][j] == 0) && (Sur.number[i][j - 1] != 0)) {
								v = true;
								mark = true;
								int temp = Sur.number[i][j];
								Sur.number[i][j] = Sur.number[i][j - 1];
								Sur.number[i][j - 1] = temp;
							}
						}
					}
				} // while(mark)
				key = false;
				while (flag) {
					flag = false;
					for (int i = 0; i < 4; i++) {
						for (int j = 3; j > 0; j--) {
							if ((Sur.number[i][j] == Sur.number[i][j - 1]) && Sur.number[i][j] != 0) {
								v = true;
								key = true;
								mark = true;
								Sur.number[i][j] = Sur.number[i][j] * 2;
								Sur.number[i][j - 1] = 0;
								Sur.score1 += Sur.number[i][j];
							}

						}
					}
				} // while(flag)
			} // while(key)
			while (v) {
				Sur.pp();
				v = false;
			}
		}
		field1.setText(Integer.toString(Sur.score1));
		this.requestFocus(true);
		this.paint(this.getGraphics());
		// repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值