仍然有很多不足和需要优化的地方 如有大佬可以随时指教一下我 才学了Swing做的小文字游戏
package NormalVersion;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Game {
static int counter = 0;
static int specialCounter = 0;
static boolean flag = false; // for the control subday's numbers and do not over array
static boolean flagBGM = true;
static boolean flagMiddle01 = false;
static boolean flagMiddle02 = false;
static WeekDay[] weekDays = {
new WeekDay(10, 1, -1, -1, -2, 1, "Monday",
"When are you going to be in the office today?\n",
"A: on time\n"
+ "B: be late\n"
+ "C: be early"),
new WeekDay(3,2,-1,-1,-3,0, "Tuesday",
"It's 06:00 PM, the boss asks you to work overtime to 08:00 PM, what would you do?\n",
"A: work overtime to 08:00 PM\n"
+ " B: go home directly\n"
+ " C: work overtime to 06:30 PM, since then the boss will be gone"),
new SpecialDay(300, 2, -1, -1, -3, 0, "SpecialDay",
"You are in the fake world, and I will help you to leave, if you take the red pill\n",
"A: take the red pill\n"
+ "B: take the blue pill\n"
+ "C: don't make decision",
new WeekDay[] {
new WeekDay(300,-10,3,-1,-2,0, "Real World",
"welcome to the real world! and you need to fix your sinew and organizations\n",
"A:let he fix my body\n"
+ " B: do not fix body\n"
+ " C: do not believe it is true and go to died"),
new WeekDay(3,2,3,1,2,0, "Real World",
"Now your body is better, would you want back with us and do a assignment\n",
"A: Yes I want to\n"
+ " B: go and loving trinity \n"
+ " C: Having consider and get into"),
new WeekDay(-3,2,3,-1,-2,0, "Real World",
"The agent is coming! Move! and agent shot on you\n",
"A: run and escape the bullet\n"
+ " B: try to control the bullet \n"
+ " C: fight with him"),
new WeekDay(-3,2,3,-1,-2,0, "Real World",
"now they are might back and you have to choose C to get out of here!\n",
"A: try to find trinity\n"
+ " B: contact with captain\n"
+ " C: jump from window"),
new WeekDay(-3,2,3,-1,-2,0, "Real World",
"They damaged windows and The agent is coming again! Move! and agent shot on you! go to cotact telephone\n",
"A: fight with him again\n"
+ " B: try to fly\n"
+ " C: go to contact telephone and out of matrix"),
new WeekDay(3000,2,300,-1,-300,0, "Real World",
"You were killed by agent I'm sorry you are not the one\n",
"A: \n"
+ " B: \n"
+ " C: ")
}, "A"),
new WeekDay(-3,2,3,-1,-2,0, "Wednessday",
"Oh ! You had a wonderful dream and that was too real you think go to psychologist\n",
"A: go to doctor\n"
+ " B: having work and go to hospital\n"
+ " C: work overtime to 06:30 PM, go to the hospital"),
new WeekDay(-2,2,-1,-1,-2,0, "Friday",
"Your boss asks you to join the team dinner, where you will be asked to have alcohol, what would you do?\n",
"A: go to the dinner\n"
+ " B: go home directly\n"
+ " C: go to the dinner and try to leave early"),
new WeekDay(-3,2,2,-2,1,-1, "Thursday",
"Doctor said that you were too stressful and You have to eat medicine on a daily basis\n",
"A: take the blue pills every day\n"
+ " B: try to have that kind of dream again\n"
+ " C: ask for half day off"),
};
public static void main(String[] args) {
Game game = new Game();
game.begin();
}
SalaryMan salaryMan = new SalaryMan();
static WeekDay[] subdays;
public void begin() {
//插入图片
ImageIcon icon1=new ImageIcon("/Users/mark/Desktop/Start.png" );
//添加JLabel 放置图片
JLabel label1=new JLabel(icon1);
//设置label的位置、大小,label大小为图片的大小
label1.setBounds(0,0,icon1.getIconWidth(),icon1.getIconHeight());
//JFrame框架
JFrame frame = new JFrame("The Matrix SalaryMan");
//在frame的底层容器添加label
frame.getLayeredPane().add(label1,new Integer(Integer.MIN_VALUE));
//panel
JPanel panel = new JPanel();
JLabel labelText= new JLabel("Press the button and Start The Game !");
labelText.setBackground(Color.lightGray);
labelText.setPreferredSize(new Dimension(300,200));
panel.add(labelText);
panel.setOpaque(true);
//panelTop,顶层容器
JPanel panelTop = new JPanel();
panelTop=(JPanel)frame.getContentPane();
JButton button = new JButton("Start Game!");
panel.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
days();// start the game once user clicked
flagBGM = false; // stop the bgm!
}
});
//panel和panelTop设置透明
panelTop.setOpaque(false);
panel.setOpaque(false);
frame.add(panel);
//添加panel,设置大小,可视
frame.setSize(800, 300);
frame.setVisible(true);
// 播放BGM
new Thread(()->{while(flagBGM) {BGM.playBegin();}
}).start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
JLabel label = new JLabel();
JPanel panel = new JPanel();
JFrame frame = new JFrame("SalaryMan");
public void days (){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SalaryMan salaryMan = new SalaryMan();
String info = Game.weekDays[0].getName() + ", " + Game.weekDays[0].getInfoTitle() + Game.weekDays[0].getInfoChoice();
JLabel status = new JLabel();
frame.setContentPane(panel);
panel.add(status);
label.setText(info);
panel.add(label);
JButton buttonA = new JButton("A");
JButton buttonB = new JButton("B");
JButton buttonC = new JButton("C");
panel.add(buttonA);
panel.add(buttonB);
panel.add(buttonC);
buttonA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choiceA();
}
});
buttonB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choiceB();
}
});
buttonC.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choiceC();
}
});
frame.setVisible(true);
frame.setSize(800, 300);
}
public void choiceA() {
if (weekDays[counter] instanceof SpecialDay && specialCounter == 0) {
flag = true;
}
if (flag) {
SpecialDay specialDay = (SpecialDay) weekDays[counter];
WeekDay[] subDays = specialDay.getSubDaysByChoice("A");
Game.subdays = subDays;
specialChoice(Game.subdays);
salaryMan.setHealthPoint(Game.subdays[specialCounter].getHealthPointA());
salaryMan.setWorkPoint(Game.subdays[specialCounter].getWorkPointA());
} else {
System.out.println("worked");
choice();
salaryMan.setHealthPoint(Game.weekDays[counter].getHealthPointA());
salaryMan.setWorkPoint(Game.weekDays[counter].getWorkPointA());
}
}
public void choiceB() {
if (weekDays[counter] instanceof SpecialDay && specialCounter == 0) {
flag = false;
}
if (weekDays[counter] instanceof SpecialDay && flag) {
SpecialDay specialDay = (SpecialDay) weekDays[counter];
specialChoice(Game.subdays);
salaryMan.setHealthPoint(Game.subdays[specialCounter].getHealthPointB());
salaryMan.setWorkPoint(Game.subdays[specialCounter].getWorkPointB());
} else {
if (weekDays[counter] instanceof SpecialDay) {
flag = false;
}
choice();
salaryMan.setHealthPoint(Game.weekDays[counter].getHealthPointB());
salaryMan.setWorkPoint(Game.weekDays[counter].getWorkPointB());
}
}
public void choiceC() {
if (weekDays[counter] instanceof SpecialDay && specialCounter == 0) {
flag = false;
}
if (specialCounter == 4) {// total five and cut down one for close subday before last day
flag = false;
flagMiddle01 = false;
flagMiddle02 = false;
}
if (weekDays[counter] instanceof SpecialDay && flag) { // use flag to control whether the subdays should be close.
specialChoice(Game.subdays);
SpecialDay specialDay = (SpecialDay) weekDays[counter];
salaryMan.setHealthPoint(Game.subdays[specialCounter].getHealthPointC());
salaryMan.setWorkPoint(Game.subdays[specialCounter].getWorkPointC());
} else {
choice();
salaryMan.setHealthPoint(Game.weekDays[counter].getHealthPointC());
salaryMan.setWorkPoint(Game.weekDays[counter].getWorkPointC());
}
}
public void choice() {
Game.counter += 1;
salaryMan.showStatus();
String name = Game.weekDays[counter].getName();
String title = Game.weekDays[counter].getInfoTitle();
String choice = Game.weekDays[counter].getInfoChoice();
String status = " Health: " + health() + " Work: " + work();
String strMsg = "<html><body>" + name + "<br>" + title + "<br>"+ choice + "<br>" + status + "<body></html>";
label.setText(strMsg);
panel.add(label);
if (counter == 2) {
flagMiddle01 = true;
new Thread(() -> {
while (flagMiddle01) {
BGM.playMiddle01();
}
}).start();
}
ending();
}
public void specialChoice(WeekDay[] subDays) {
Game.specialCounter += 1;
salaryMan.showStatus();
String name = subDays[specialCounter].getName();
String title = subDays[specialCounter].getInfoTitle();
String choice = subDays[specialCounter].getInfoChoice();
String status = " Health: " + health() + " Work: " + work();
String strMsg = "<html><body>" + name + "<br>" + title + "<br>" + choice + "<br>" + status + "<body></html>";
label.setText(strMsg);
panel.add(label);
if (specialCounter == 6) {
flagMiddle02 = false; // close second BGM in middle part
flag = false;//control and close the subdays
}
if (specialCounter == 3) {
flagMiddle01 = false;
flagMiddle02 = true; // open second BGM in middle part
new Thread(()->{while(flagMiddle02) {BGM.playMiddle02();}
}).start();
}
ending();
}
public void ending() { // for identify whether the game should be over
if (counter > 10 || health() < 0 || counter == 5 || specialCounter == 5) {
//插入图片
ImageIcon icon1=new ImageIcon("/Users/mark/Desktop/Start.png" );
//添加JLabel 放置图片
JLabel label1=new JLabel(icon1);
//设置label的位置、大小,label大小为图片的大小
label1.setBounds(0,0,icon1.getIconWidth(),icon1.getIconHeight());
//JFrame框架
JFrame frame = new JFrame("The Matrix SalaryMan");
//在frame的底层容器添加label
frame.getLayeredPane().add(label1,new Integer(Integer.MIN_VALUE));
//panel
JPanel panel = new JPanel();
String points = "Your health point is " + health() + " Your work point is " + work();
String score = "Your total score is " + (health() + work()) * 5;
String info = "You are died but unfortunately, you are still in THE MATRIX It wasn't real world!";
String strMsg = "<html><body>" + points + "<br>" + score + "<br>" + info + "<body></html>"; //here is for let them divide into three paragraph
JLabel labelText= new JLabel(strMsg);
labelText.setBackground(Color.lightGray);
labelText.setPreferredSize(new Dimension(300,200));
panel.add(labelText);
panel.setOpaque(true);
//panelTop,顶层容器
JPanel panelTop = new JPanel();
panelTop=(JPanel)frame.getContentPane();
//panel和panelTop设置透明
panelTop.setOpaque(false);
panel.setOpaque(false);
frame.add(panel);
//添加panel,设置大小,可视
frame.setSize(800, 300);
frame.setVisible(true);
// 播放BGM
new Thread(()->{while(true) {BGM.playBegin();}
}).start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public int health() {
System.out.println(salaryMan.getHealthPoint());
return salaryMan.getHealthPoint();
}
public int work() {
System.out.println(salaryMan.getWorkPoint());
return salaryMan.getWorkPoint();
}
}
以上是主程序
package NormalVersion;
public class SalaryMan {
private int healthPoint = 5;
private int workPoint = 0;
public int getHealthPoint() {
return healthPoint;
}
public void setHealthPoint(int healthPoint) {
this.healthPoint += healthPoint;
}
public int getWorkPoint() {
return workPoint;
}
public void setWorkPoint(int workPoint) {
this.workPoint += workPoint;
}
public boolean isNotAlive() {
if (this.healthPoint <= 0) {
return true;
}
return false;
}
public boolean isWorking() {
if (isNotAlive() && this.workPoint >= 0) {
return true;
}
return false;
}
public void showStatus() {
System.out.println("Your healthPoint is " + this.healthPoint);
System.out.println("Your workPoint is " + this.workPoint);
}
}
package NormalVersion;
public class WeekDay {
private int healthPointA, healthPointB, healthPointC;
private int workPointA, workPointB, workPointC;
String name, infoTitle, infoChoice;
public WeekDay(int healthPointA, int healthPointB, int healthPointC, int workPointA, int workPointB, int workPointC, String name, String infoTitle, String infoChoice) {
this.healthPointA = healthPointA;
this.healthPointB = healthPointB;
this.healthPointC = healthPointC;
this.workPointA = workPointA;
this.workPointB = workPointB;
this.workPointC = workPointC;
this.name = name;
this.infoTitle = infoTitle;
this.infoChoice = infoChoice;
}
public int getHealthPointA() {
return healthPointA;
}
public int getHealthPointB() {
return healthPointB;
}
public int getHealthPointC() {
return healthPointC;
}
public int getWorkPointA() {
return workPointA;
}
public int getWorkPointB() {
return workPointB;
}
public int getWorkPointC() {
return workPointC;
}
public String getName() {
return name;
}
public String getInfoTitle() {
return infoTitle;
}
public String getInfoChoice() {
return infoChoice;
}
}
package NormalVersion;
public class SpecialDay extends WeekDay {
private WeekDay[] subDays;
private String subDayTrigger;
public SpecialDay(int healthPointA, int workPointA,
int healthPointB, int workPointB,
int healthPointC, int workPointC,
String name, String infoTitle, String infoChoice,
WeekDay[] subDays, String subDayTrigger) {
super(healthPointA, workPointA,
healthPointB, workPointB,
healthPointC, workPointC,
name, infoTitle, infoChoice);
// add the easter eggs
this.subDays = subDays;
/* set the trigger */
this.subDayTrigger = subDayTrigger;
}
public WeekDay[] getSubDaysByChoice(String choice) {
if (choice.equals(subDayTrigger)) {
return subDays;
}
return new WeekDay[] {};
}
}
package NormalVersion;
import javax.sound.sampled.*;
import java.io.File;
public class BGM {
static void playBegin() { //开头曲
// 背景音乐播放
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("/Users/mark/Desktop/BGM/StartBGM.wav"));
AudioFormat aif = ais.getFormat();
final SourceDataLine sdl;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, aif);
sdl = (SourceDataLine) AudioSystem.getLine(info);
sdl.open(aif);
sdl.start();
FloatControl fc = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
// value可以用来设置音量,从0-2.0
double value = 2;
float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
fc.setValue(dB);
int nByte = 0;
int writeByte = 0;
final int SIZE = 1024 * 64;
byte[] buffer = new byte[SIZE];
while (nByte != -1) {// 判断 播放/暂停 状态
if(Game.flagBGM) {
nByte = ais.read(buffer, 0, SIZE);
sdl.write(buffer, 0, nByte);
}else {
nByte = ais.read(buffer, 0, 0);
}
}
sdl.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
static void playMiddle01() { //开头曲
// 背景音乐播放
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("/Users/mark/Desktop/BGM/Middle01.wav"));
AudioFormat aif = ais.getFormat();
final SourceDataLine sdl;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, aif);
sdl = (SourceDataLine) AudioSystem.getLine(info);
sdl.open(aif);
sdl.start();
FloatControl fc = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
// value可以用来设置音量,从0-2.0
double value = 2;
float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
fc.setValue(dB);
int nByte = 0;
int writeByte = 0;
final int SIZE = 1024 * 64;
byte[] buffer = new byte[SIZE];
while (nByte != -1) {// 判断 播放/暂停 状态
if(Game.flagMiddle01) {
nByte = ais.read(buffer, 0, SIZE);
sdl.write(buffer, 0, nByte);
}else {
nByte = ais.read(buffer, 0, 0);
}
}
sdl.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
static void playMiddle02() { //开头曲
// 背景音乐播放
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("/Users/mark/Desktop/BGM/Middle02.wav"));
AudioFormat aif = ais.getFormat();
final SourceDataLine sdl;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, aif);
sdl = (SourceDataLine) AudioSystem.getLine(info);
sdl.open(aif);
sdl.start();
FloatControl fc = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
// value可以用来设置音量,从0-2.0
double value = 2;
float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
fc.setValue(dB);
int nByte = 0;
int writeByte = 0;
final int SIZE = 1024 * 64;
byte[] buffer = new byte[SIZE];
while (nByte != -1) {// 判断 播放/暂停 状态
if(Game.flagMiddle02) {
nByte = ais.read(buffer, 0, SIZE);
sdl.write(buffer, 0, nByte);
}else {
nByte = ais.read(buffer, 0, 0);
}
}
sdl.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}