赞
踩
1.团队名称 团队成员 任务分配
成员 | 负责模块 |
---|---|
苑先淼(组长) | 编码规范窗口编写游戏主体编写生成GUI界面 |
2项目简介
计算机产生随机数,猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜到,给出所用时间和评语。保留用户测试成绩,做出成绩排行榜。排行榜存放到文件中。
项目功能架构图
程序流程图
UML类图
git提交截图和地址
git地址
运行截图
主要代码
窗口编写
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Guider { public static void main(String[] args) { JFrame frame = new JFrame("猜数字游戏"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); JPanel panel = new JPanel(); panel.setLayout(null); frame.add(panel); JButton startBtn = new JButton("A 开始游戏"); startBtn.setBounds(120, 100, 100, 30); panel.add(startBtn); JButton exitBtn = new JButton("B 开始游戏"); exitBtn.setBounds(280, 100, 100, 30); panel.add(exitBtn); startBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); new Main("A"); } }); exitBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); new Main("B"); } }); } }
游戏主体
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.ArrayList; import java.util.Date; public class Main { int count = 1; // 计时 Date startTime ; music bgm = new music(); Main(String name) { bgm.playMusic(); JFrame frame = new JFrame("猜数字游戏"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); Container con = frame.getContentPane(); con.setBackground(Color.yellow); JPanel panel = new JPanel(); panel.setLayout(null); frame.add(panel); JLabel label = new JLabel("请输入一个1-100的数字"); label.setBounds(180, 20, 200, 30); panel.add(label); JTextField textField = new JTextField(); textField.setBounds(180, 60, 100, 30); panel.add(textField); JButton submitBtn = new JButton("提交"); submitBtn.setBounds(180, 100, 100, 30); panel.add(submitBtn); JButton exitBtn = new JButton("退出游戏"); exitBtn.setBounds(180, 140, 100, 30); panel.add(exitBtn); JLabel resultLabel = new JLabel(); resultLabel.setBounds(180, 180, 200, 30); panel.add(resultLabel); int random = (int) (Math.random() * 100 + 1); System.out.println(random); submitBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 开始计时 if (count == 1) { startTime = new Date(); } String text = textField.getText(); int num = Integer.parseInt(text); if (num > random) { resultLabel.setText("第" + count + "次回答,猜大了"); } else if (num < random) { resultLabel.setText("第" + count + "次回答,猜小了"); } else { resultLabel.setText("第" + count + "次回答,猜对了"); // 结束计时 Date endTime = new Date(); long time = endTime.getTime() - startTime.getTime(); JOptionPane.showMessageDialog(null, "恭喜你,猜对了!用时" + time / 1000 + "秒"); pushRankingList(name, time / 1000, count); } count++; } }); exitBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); new Guider().main(null); } }); } // 生成排行榜文件 public void pushRankingList(String name, long time, int count) { // 创建文件 File file = new File("排行榜.txt"); if (!file.exists()) { try { file.createNewFile(); FileWriter fw = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(fw); bw.write("排名-姓名-用时(秒)-次数\n"); bw.close(); fw.close(); } catch (Exception e) { e.printStackTrace(); } } try { // 获取行数 FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); int line = 0; while (br.readLine() != null) { line++; } br.close(); fr.close(); FileWriter fw = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(fw); bw.write(line + "-" + name + "-" + time + "-" + count + "次\n"); bw.close(); fw.close(); } catch (Exception e) { e.printStackTrace(); } sortRankinList(); } // 给排行榜排序 public void sortRankinList() { // 读取文件 ArrayList<String> list = new ArrayList<>(); File file = new File("排行榜.txt"); int lineNumber = 1; try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line = br.readLine(); while (line != null) { if (lineNumber > 1 && !line.isEmpty()) { list.add(line); } line = br.readLine(); lineNumber++; } br.close(); fr.close(); } catch (Exception e) { e.printStackTrace(); } // 给排行榜排序 list.sort((o1, o2) -> { String[] split1 = o1.split("-"); String[] split2 = o2.split("-"); return Integer.parseInt(split1[2]) - Integer.parseInt(split2[2]); }); // 重新写入文件 try { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write("排名-姓名-用时(秒)-次数\n"); for (int i = 0; i < list.size(); i++) { String name = list.get(i).split("-")[1]; String time = list.get(i).split("-")[2]; String count = list.get(i).split("-")[3]; bw.write((i + 1) + "-" + name + "-" + time + "-" + count + "\n"); } bw.close(); fw.close(); } catch (Exception e) { e.printStackTrace(); } } }
背景音乐播放
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.FloatControl; import java.io.File; public class music { static Clip clip; public static void playMusic() { try { File musicPath = new File("D:\\CloudMusic\\music.wav"); if(musicPath.exists()) { AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath); clip = AudioSystem.getClip(); clip.open(audioInput); FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(-20.0f); clip.start(); clip.loop(Clip.LOOP_CONTINUOUSLY); } else { } } catch(Exception ex) { ex.printStackTrace(); } } }
6课程设计感想
还有很多难的内容实现不了,在实际设计中才发现,书本上理论性的东西与在实际运用中的还是有一定的出入的,所以有些问题不但要深入地理解,而且要不断地更正以前的错误思维。一切问题必须要靠自己一点一滴的解决,而在解决的过程当中你会发现自己在飞速的提升。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。