java2048屌丝逆袭版

编程入门 行业动态 更新时间:2024-10-15 18:28:39

java2048屌丝逆袭版

java2048屌丝逆袭版

java2048屌丝逆袭版

本着对游戏的热忱,下定决心自己做了一个简单的2048小游戏,感觉大致功能已经实现,就是一些算法上的bug还有待改进,那个,添加的声音有点屌丝了,大家下载听后,海涵,海涵!好了,废话不多说了,代码奉上:

package test;import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;import javax.swing.*;
public class My2048 extends JFrame  {
private boolean flag=false;
private  AudioClip clip;
private JTextField time,score;
private TimeThread tt;
private JCheckBox jc;
Block[] block;
JPanel panel;
boolean numFlag;
int moveFlag,max=0;
String[] string = new String[]{"单身","搭讪","约会","表白","恋爱","牵手","拥抱","接吻","爱爱","结婚"};
//2048游戏界面
public void Game() {
numFlag = true;
moveFlag = 0;
block = new Block[16];
setTitle("2048(全屏更爽)");
setSize(500, 500);
setLocationRelativeTo(null);
//设置图标
setIconImage(new ImageIcon("2048.jpg").getImage());
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 3,3));
addBlock();
for (int i = 0; i < 3; i++)
appearBlock();
add(panel);
//添加键盘监听器
addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
upBlock();
judgeAppear();
appearBlock();
over();
break;
case KeyEvent.VK_DOWN:
downBlock();
judgeAppear();
appearBlock();
over();
break;
case KeyEvent.VK_LEFT:
leftBlock();
judgeAppear();
appearBlock();
over();
break;
case KeyEvent.VK_RIGHT:
rightBlock();
judgeAppear();
appearBlock();
over();
break;
}
}
public void keyPressed(KeyEvent e) {
}
});setVisible(true);
}
//添加方块
public void addBlock() {
for (int i = 0; i < 16; i++) {
block[i] = new Block();
//方块内容居中显示
block[i].setHorizontalAlignment(JLabel.CENTER);
// 不透明
block[i].setOpaque(true);
panel.add(block[i]);
}
}
//初始化3个方块
public void appearBlock() {
while(numFlag) {
//0~16随机方块放数据
int index = (int) (Math.random() * 16);
if (block[index].getValue().isEmpty()) {
if (Math.random() < 0.7)
block[index].setValue("单身");
else
block[index].setValue("搭讪");
break;
}
}
}
//判断是否需要加方块
public void judgeAppear() {
int sum = 0;
for (int i = 0; i < 16; i++) {
if (!block[i].getValue().isEmpty())
sum++;
}
if (sum == 16)
numFlag = false;
}//向上键监听
public void upBlock() {
for (int i = 12; i < 16; i++) {
int index = i;
for (int j = i - 4; j >= i - 12; j -= 4) {
String valueI = block[index].getValue(), valueJ = block[j].getValue();
if (valueJ.isEmpty()) {
block[index].setValue("");
block[j].setValue(valueI);
} else {
if (valueI.equals(valueJ)) {
block[index].setValue("");
block[j].setValue(find(valueI));
if (find(valueI).equals("结婚"))
win();
} else if (numFlag == false)
moveFlag += 1;
}
index = j;
}
}
}
//向下键监听
public void downBlock() {
for (int i = 0; i < 4; i++) {
int index = i;
for (int j = i + 4; j <= i + 12; j += 4) {
String valueI = block[index].getValue(), valueJ = block[j]
.getValue();
if (valueJ.isEmpty()) {
block[index].setValue("");
block[j].setValue(valueI);
} else {
if (valueI.equals(valueJ)) {
block[index].setValue("");
block[j].setValue(find(valueI));
if (find(valueI).equals("结婚"))
win();
} else if (numFlag == false)
moveFlag += 1;
}
index = j;
}
}
}
//向右键监听
public void rightBlock() {
for (int i = 0; i <= 12; i += 4) {
int index = i;
for (int j = i + 1; j <= i + 3; j++) {
String valueI = block[index].getValue(), valueJ = block[j]
.getValue();
if (valueJ.isEmpty()) {
block[index].setValue("");
block[j].setValue(valueI);
} else {
if (valueI.equals(valueJ)) {
block[index].setValue("");
block[j].setValue(find(valueI));
if (find(valueI).equals("结婚"))
win();
} else if (numFlag == false)
moveFlag += 1;
}
index = j;
}
}
}
//向左键监听
public void leftBlock() {
for (int i = 3; i <= 15; i += 4) {
int index = i;
for (int j = i - 1; j >= i - 3; j--) {
String valueI = block[index].getValue(), valueJ = block[j]
.getValue();
if (valueJ.isEmpty()) {
block[index].setValue("");
block[j].setValue(valueI);
} else {
if (valueI.equals(valueJ)) {
block[index].setValue("");
block[j].setValue(find(valueI));
if (find(valueI).equals("结婚"))
win();
} else if (numFlag == false)
moveFlag += 1;
}
index = j;
}
}
}
//2个相同字符串合并成升级后的字符串
public String find(String str){
String s="";
for(int i =0;i<string.length;i++){
if(string[i].equals(str)){
s = string[i+1];
break;
}
}
return s;
}
//判断失败
public void over() {
if (!numFlag && moveFlag >= 36) {
block[4].setText("逆");
block[5].setText("袭");
block[6].setText("失");
block[7].setText("败");
block[8].setText("再");
block[9].setText("接");
block[10].setText("再");
block[11].setText("厉");tt.stop();
time.setText(time.getText());
}
}
//判断胜利
public void win() {block[0].setText("逆");
block[1].setText("袭");
block[2].setText("成");
block[3].setText("功");
block[13].setText("获");
block[14].setText("得");
block[15].setText("女");
block[15].setText("神");if(Integer.parseInt(time.getText())*moveFlag>max){
max =Integer.parseInt(time.getText())*moveFlag;
score.setText(""+max);
}
}
//重置游戏public void reStart(){numFlag=true;
moveFlag=0;
for(int i=0;i< 16;i++)
block[i].setValue("");for (int i = 0; i < 3; i++)
appearBlock();}
//系统操作框 
public My2048() {
JFrame jf = new JFrame("2048屌丝逆袭版");
jf.setSize(400,113);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
jf.setResizable(false);
jf.setIconImage(new ImageIcon("2048.jpg").getImage());
JPanel menur = new JPanel();
menur.setLayout(new GridLayout(3,3));
ImageIcon image = new ImageIcon("2048.png");
JLabel jla2048=new JLabel(image);
jc = new JCheckBox("声音");
jc.setMnemonic(KeyEvent.VK_1);//快捷键ALT+1
//监听声音
jc.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1){
try {
File f =new File("1.wav");
clip=Applet.newAudioClip(f.toURL());
clip.loop();
}catch (Exception ex) {
ex.printStackTrace();
} 
}
else clip.stop();
}
});JButton help = new JButton("关于");
JButton start = new JButton("开始游戏");
JButton restart = new JButton("重置游戏");
JButton HTGame = new JButton("游戏玩法");JLabel timel = new JLabel("时间:",JLabel.CENTER);
JLabel scourel = new JLabel("最高得分:",JLabel.CENTER);time = new JTextField();
score = new JTextField();
time.setEditable(false);
score.setEditable(false);menur.add(timel);
menur.add(time);
menur.add(help);
menur.add(scourel);
menur.add(score);
menur.add(jc);
menur.add(start);
menur.add(restart);
menur.add(HTGame);jf.add(jla2048,BorderLayout.WEST);
jf.add(menur,BorderLayout.CENTER);
jf.setVisible(true);
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "作者:Mr.CT(~.~).玩得开心!");
}
});
HTGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "按动键盘上的上下左右键,相同的2个方框可以合成一个框,每次随机出现一个新的框,\n单身→搭讪→约会→表白→恋爱→牵手→拥抱→接吻→爱爱→结婚,合成结婚就算胜利!");
}
});
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!flag==true){
Game();
//开启时间线程,开始计时
tt = new TimeThread(time);
tt.start();
flag=true;
}else JOptionPane.showMessageDialog(null, "游戏已经开始!");
}
});
restart.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
if(time.getText().isEmpty())
JOptionPane.showMessageDialog(null, "游戏尚未开始!");
else{
reStart();
time.setText("");
tt.stop();
flag = false;
}
}
});
}
/**
* 主函数
* @param args
*/public static void main(String args[]) {new My2048();
}

 

package test;
import javax.swing.*;import java.awt.*;
/*** * 方块类*/
public class Block extends JLabel {private String value;public Block() {value = "";//设置字体setFont(new Font("font", Font.BOLD, 40));setBackground(Color.BLACK);}public String getValue() {return value;}public void setValue(String value1) {this.value = value1;if (value!="")setText(value);elsesetText("");setColor();}//给不同方块添加不同颜色public void setColor(){if (this.value.isEmpty())setBackground(Color.BLACK);else if (this.value.equals("单身"))setBackground(new Color(238, 228, 218));else if (this.value.equals("搭讪"))setBackground(new Color(238, 200, 198));else if (this.value.equals("约会"))setBackground(new Color(243, 130, 110));else if (this.value.equals("表白"))setBackground(new Color(243, 177, 116));else if (this.value.equals("恋爱"))setBackground(new Color(248, 149, 90));else if (this.value.equals("牵手"))setBackground(new Color(249, 94, 50));else if (this.value.equals("拥抱"))setBackground(new Color(239, 207, 108));else if (this.value.equals("接吻"))setBackground(new Color(239, 207, 99));else if (this.value.equals("爱爱"))setBackground(new Color(239, 203, 82));else if (this.value.equals("结婚"))setBackground(new Color(239, 199, 57));}
}

 

package test;import javax.swing.JTextField;
/*** 时间线程类* @author ctong**/
public class TimeThread extends Thread {private JTextField time;private int count = 0;public TimeThread(JTextField time){this.time=time;}public void run(){while(true){try {Thread.sleep(1000);//每一秒显示一次} catch (InterruptedException e) {e.printStackTrace();}time.setText(""+count++);}}}

 

更多推荐

java2048屌丝逆袭版

本文发布于:2024-03-04 02:43:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1708057.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屌丝逆袭版

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!