主类:
import javax.swing.*;
public class GameMain2048 {
public static JFrame gameframe;
public static void main(String[] args) {//输入psvm或者输入main回车
//1.显示窗体 swing技术 JFrame类
gameframe=new JFrame();
//窗口大小
gameframe.setSize(370,400);
gameframe.setTitle("2048小游戏");
gameframe.setLocationRelativeTo(null);//使窗体居中
//2.显示窗体上的内容,首先创建一个面板JPanel(容器:画纸)
GamePanel panel=new GamePanel();
//添加到窗体上
gameframe.add(panel);
//显示窗口
gameframe.setVisible(true);
}
}
面板类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class GamePanel extends JPanel {//面板,显示内容
Random rand = new Random();
//1. 初始化游戏界面信息,创建4*4所有的卡片:二维数组
Card[][] allCards = new Card[4][4];
public GamePanel()
{
//创建卡片的类
//1. 创建card对象,储存到allCards数组中
for (int i = 0; i <4 ; i++) {
for (int j = 0; j < 4; j++) {
Card card = new Card(i,j);
allCards[i][j]=card;
}
}
createNewCard();
//添加一个键盘监听事件
GameMain2048.gameframe.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch(code)
{
case KeyEvent.VK_UP:
case KeyEvent.VK_W:
moveUP();break;
case KeyEvent.VK_DOWN:
case KeyEvent.VK_S:
moveDOWN();break;
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_D:
moveRight();break;
case KeyEvent.VK_LEFT:
case KeyEvent.VK_A:
moveLeft();break;
}
createNewCard();
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
public void moveUP()
{
for (int i = 1; i <4 ; i++) {
for (int j = 0; j < 4; j++) {
Card card = allCards[i][j];
if(card.num!=0)
{
card.MoveU(allCards);
}
}
}
}
public void moveDOWN()
{
for (int i = 0; i <3 ; i++) {
for (int j = 0; j < 4; j++) {
Card card = allCards[i][j];
if(card.num!=0)
{
card.MoveD(allCards);
}
}
}
}
public void moveRight()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j <3 ; j++) {
Card card=allCards[i][j];
if(card.num!=0)
{
card.MoveR(allCards);
}
}
}
}
public void moveLeft()
{
for (int i = 0; i < 4; i++) {
for (int j = 1; j <4 ; j++) {
Card card=allCards[i][j];
if(card.num!=0)
{
card.MoveL(allCards);
}
}
}
}
//随机产生卡片以及这个数字
public void createNewCard(){
//数字2与4的概率
int num;
int id=rand.nextInt(5);
if(id==4){
//数字4
num=4;
}else{
//数字2
num=2;
}
//随机产生二维数组的下标,这个下标不能有数字
Card card=randCard();
//设置数字
card.num=num;
}
//创建随机可用的卡片位置
public Card randCard()
{
int i=rand.nextInt(4);
int j=rand.nextInt(4);
Card card =allCards[i][j];
if (card.num==0){
return card;
}else{
return randCard();
}
}
//绘图功能 画笔工具 Graphics g
@Override
public void paint(Graphics g) {//重写父类方法
super.paint(g);
//画16个矩形
for (int i = 0; i <4 ; i++) {
for (int j = 0; j < 4; j++) {
Card card = allCards[i][j];
g.setColor(getColor(card.num));//根据卡片上的数字决定颜色
g.fillRect(card.x,card.y,80,80);
//设置文字颜色
if(card.num!=0) {
g.setColor(new Color(125, 78, 51));
g.setFont(new Font("楷体", Font.BOLD, 35));
int x;
if (card.num < 10)
x = card.x + 32;
else if (card.num < 100)
x = card.x + 20;
else if (card.num < 1000)
x = card.x + 8;
else x = card.x - 2;
int y = card.y + 50;
g.drawString(card.num + "", x, y);//画数字
}
}
}
}
public Color getColor(int num)
{
Color color;
switch (num)
{
case 0: color=new Color(80,140,110);
break;
case 2: color=new Color(150,140,110);
break;
case 4: color=new Color(150,250,110);
break;
case 8: color=new Color(250,250,110);
break;
case 16: color=new Color(150,150,20);
break;
case 32: color=new Color(250,150,20);
break;
case 64: color=new Color(250,50,20);
break;
case 128: color=new Color(150,100,100);
break;
case 256: color=new Color(230,190,45);
break;
case 512: color=new Color(230,10,150);
break;
case 1024: color=new Color(100,10,150);
break;
case 2048: color=new Color(90,90,250);
break;
default:color=null;
}
return color;
}
}
卡片类:
public class Card {
//存储卡片的属性
int x,y;//窗体上坐标
int num=0;//卡片上的数字
int i,j;//数组中的二维下标
public Card(int i,int j)
{
this.i=i;
this.j=j;
this.x=5+j*80+5*(j+1);
this.y=5+i*80+5*(i+1);
}
public void MoveU(Card[][] allCards)
{
if(this.i==0) return;
Card preCard= allCards[i - 1][j];
if(preCard.num==0) {
preCard.num = this.num;
this.num = 0;
preCard.MoveU(allCards);}
else if(preCard.num==this.num) {
preCard.num = preCard.num + this.num;
this.num = 0;
preCard.MoveU(allCards);
}
}
public void MoveD(Card[][] allCards)
{
if(this.i==3) return;
Card preCard= allCards[i + 1][j];
if(preCard.num==0) {
preCard.num = this.num;
this.num = 0;
preCard.MoveD(allCards);}
else if(preCard.num==this.num) {
preCard.num = preCard.num + this.num;
this.num = 0;
preCard.MoveD(allCards);
}
}
public void MoveR(Card[][] allCards)
{
if(this.j==3) return;
Card preCard= allCards[i][j+1];
if(preCard.num==0) {
preCard.num = this.num;
this.num = 0;
preCard.MoveR(allCards);}
else if(preCard.num==this.num) {
preCard.num = preCard.num + this.num;
this.num = 0;
preCard.MoveR(allCards);
}
}
public void MoveL(Card[][] allCards)
{
if(this.j==0) return;
Card preCard= allCards[i][j-1];
if(preCard.num==0) {
preCard.num = this.num;
this.num = 0;
preCard.MoveL(allCards);}
else if(preCard.num==this.num) {
preCard.num = preCard.num + this.num;
this.num = 0;
preCard.MoveL(allCards);
}
}
}