Java2048游戏源代码
2048 ⼩游戏
⼀.主要功能:
1. 游戏初始化:新建游戏4×4的16宫格画布,随机格⼦上⽣成2或者4两个数字
2. 格⼦的移动:先判断能否移动,移动后判断能否合并,合并后改变格⼦颜⾊和数字
3. 新格⼦的⽣成:移动⼀次,就在剩余的空格⼦中随机⽣成⼀个2或者4
4. 判赢:16宫格中合并出了“2048”则为游戏胜利
5. 判输:16宫格中没有剩余空格⼦且不能再向任何⽅向移动则为游戏失败
⼆.项⽬的主要结构:
在项⽬2018游戏中,有4个源⽂件,此外,还有3个.png和两个.wav格式的⾳乐⽂件。⼀个⾳乐是按键移动的声⾳,另外⼀个是碰撞后的消除的声⾳。然后游戏的最⾼分保存是在Recording源⽂件中,之后附上所需的图⽚⽂件和⾳乐⽂件:
来了来了,
三.代码
1. Game.java
package shixun;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Game extends JFrame {
private static final long serialVersionUID =1L;
public static void main(String[] args){
Game UI =new Game();
UI.IntUI();
}
/
/ ⽤于存放数据的数组,构成游戏4*4界⾯的数值
private int Numbers[][]=new int[4][4];
private void IntUI(){
this.setTitle("2048⼩游戏");
this.setLocation(450,100);
this.setSize(400,500);
this.setLayout(null);
// 开始游戏按钮
ImageIcon imgicon =new ImageIcon("res/start.png");
JButton bt =new JButton(imgicon);
bt.setFocusable(false);
bt.setBorderPainted(false);
bt.setFocusPainted(false);
bt.setContentAreaFilled(false);
bt.setBounds(5,10,120,30);// 设置按钮的x,y坐标位置和宽度与⾼度this.add(bt);
//后退⼀步按钮
ImageIcon backicon =new ImageIcon("res/backicon.png");
JButton back =new JButton(backicon);
back.setFocusable(false);
back.setBorderPainted(false);
back.setFocusPainted(false);
back.setContentAreaFilled(false);
back.setBounds(270,10,120,30);// 设置按钮的x,y坐标位置和宽度与⾼度this.add(back);
// 关于按钮
ImageIcon imgicon2 =new ImageIcon("res/about.png");
JButton about =new JButton(imgicon2);
about.setFocusable(false);
about.setBorderPainted(false);
about.setFocusPainted(false);
about.setContentAreaFilled(false);
about.setBounds(160,10,70,30);
this.add(about);
// 分数显⽰
JLabel lb =new JLabel("分数:0");
lb.setBounds(40,45,120,30);
lb.setFont(new Font("幼圆", Font.CENTER_BASELINE,18));
lb.setForeground(new Color(0x000000));
this.add(lb);
//最⾼分数
int maxscore = Maxscore_2048();
JLabel M=new JLabel("最⾼分:0");
M.setBounds(150,45,120,30);
M.setFont(new Font("幼圆", Font.CENTER_BASELINE,18));
M.setForeground(new Color(0x000000));
this.add(M);
//静⾳
JCheckBox isSoundBox=new JCheckBox("静⾳");
isSoundBox.setBounds(290,45,120,30);
isSoundBox.setFont(new Font("幼圆", Font.CENTER_BASELINE,18));
isSoundBox.setFocusable(false);
isSoundBox.setBorderPainted(false);
isSoundBox.setFocusPainted(false);
isSoundBox.setContentAreaFilled(false);
this.add(isSoundBox);
this.add(isSoundBox);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setVisible(true);// 显⽰界⾯
// 创建事件处理类
MyListener cl =new MyListener(this,Numbers,lb,M, bt, about,back,isSoundBox);  bt.addActionListener(cl);
about.addActionListener(cl);
back.addActionListener(cl);
isSoundBox.addActionListener(cl);
this.addKeyListener(cl);
}
// 重写窗体
@Override
public void paint(Graphics g){
super.paint(g);
g.setColor(new Color(0xBBADA0));
g.fillRoundRect(15,110,370,370,15,15);// ⼤矩形框
g.setColor(new Color(0xCDC1B4));
for(int i =0; i <4; i++){
for(int j =0; j <4; j++){
g.fillRoundRect(25+ i *90,120+ j *90,80,80,15,15);// ⼩矩形框
}
}
// 调整数字的位置并上⾊
for(int i =0; i <4; i++){
for(int j =0; j <4; j++){
if(Numbers[j][i]!=0){
int FontSize =30;
int MoveX =0, MoveY =0;
switch(Numbers[j][i]){
case2:
g.setColor(new Color(0xeee4da));
FontSize =30;
MoveX =0;
MoveY =0;
break;
case4:
g.setColor(new Color(0xede0c8));
FontSize =30;
MoveX =0;
MoveY =0;
break;
case8:
g.setColor(new Color(0xf2b179));
FontSize =30;
MoveX =0;
MoveY =0;
break;
case16:
g.setColor(new Color(0xf59563));
FontSize =29;
MoveX =-5;
MoveY =0;
break;
case32:
g.setColor(new Color(0xf67c5f));
FontSize =29;
MoveX =-5;
MoveY =0;
break;
case64:
g.setColor(new Color(0xf65e3b));
FontSize =29;
MoveX =-5;
MoveY =0;
MoveY =0;
break;
case128:
g.setColor(new Color(0xedcf72));
FontSize =28;
MoveX =-10;
MoveY =0;
break;
case256:
g.setColor(new Color(0xedcc61));
FontSize =28;
MoveX =-10;
MoveY =0;
break;
case512:
g.setColor(new Color(0xedc850));
FontSize =28;
MoveX =-10;
MoveY =0;
break;
case1024:
g.setColor(new Color(0xedc53f));
FontSize =27;
MoveX =-15;
MoveY =0;
break;
case2048:
g.setColor(new Color(0xedc22e));
FontSize =27;
MoveX =-15;
MoveY =0;
break;
default:
g.setColor(new Color(0x000000));
break;
}
g.fillRoundRect(25+ i *90,120+ j *90,80,80,15,15);// ⼩矩形框上⾊    g.setColor(new Color(0x000000));简单的java游戏代码
g.setFont(new Font("Kristen ITC", Font.PLAIN, FontSize));
g.drawString(Numbers[j][i]+"",25+ i *90+30+ MoveX,
120+ j *90+50+ MoveY);
}
}
}
}
}
2.MyListener.java
package shixun;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class MyListener extends KeyAdapter implements ActionListener { /**
* KevinWu
*/
private Game UI;// 界⾯对象
private Game UI;// 界⾯对象
private int Numbers[][];// 存放数据的数组
private Random rand =new Random();
private int BackUp[][]=new int[4][4];//⽤于备份数组,供回退时使⽤
private int BackUp2[][]=new int[4][4];//⽤于备份数组,供起死回⽣时使⽤
public JLabel lb;
public JLabel M;
int score =0;
int Maxscore_2048();
int tempscore,tempscore2;//记录回退isWin的分数值
public JButton bt,about,back;
public JCheckBox isSoundBox;
private boolean isWin=false,relive=false,hasBack=false,isSound=true;
public MyListener(Game UI,int Numbers[][], JLabel lb,JLabel M ,JButton bt,JButton about,JButton back,JCheckBox isSoundBox){ this.UI = UI;
this.Numbers = Numbers;
this.lb = lb;
this.bt=bt;
this.about=about;
this.back=back;
this.isSoundBox=isSoundBox;
this.M=M;
}
@Override
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
Source()==bt ){
isWin=false;
for(int i =0; i <4; i++)
for(int j =0; j <4; j++)
Numbers[i][j]=0;
score =0;// 保证每次重置游戏都是0分开始
lb.setText("分数:"+ score);
M.setText("最⾼分:"+maxscore);
int r1 = Int(4);
int r2 = Int(4);
int c1 = Int(4);
int c2 = Int(4);
while(r1 == r2 && c1 == c2){
r2 = Int(4);
c2 = Int(4);
}
// ⽣成数字(2或者4)
int value1 = Int(2)*2+2;
int value2 = Int(2)*2+2;
// 把数字存进对应的位置
Numbers[r1][c1]= value1;
Numbers[r2][c2]= value2;
UI.Graphics());
}
else Source()==about){
JOptionPane.showMessageDialog(UI,"游戏规则:\n"
+"开始时棋盘内随机出现两个数字,出现的数字仅可能为2或4\n"
+"玩家可以选择上下左右四个⽅向,若棋盘内的数字出现位移或合并,视为有效移动\n"
+"玩家选择的⽅向上若有相同的数字则合并,每次有效移动可以同时合并,但不可以连续合并\n"
+"合并所得的所有新⽣成数字想加即为该步的有效得分\n"
+"玩家选择的⽅向⾏或列前⽅有空格则出现位移\n"
+"每有效移动⼀步,棋盘的空位(⽆数字处)随机出现⼀个数字(依然可能为2或4)\n"
+"棋盘被数字填满,⽆法进⾏有效移动,判负,游戏结束\n"
+"棋盘上出现2048,判胜,游戏结束。\n"
);
}
else Source()==back&&hasBack==false){
hasBack=true;
if(relive==false){
score=tempscore;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。