2020年的春节新型冠状病毒引发了肺炎。
寒假延长,在家心血来潮写了一个崩坏3抽卡模拟程序(单机而已,仅供娱乐)
只有极简的功能,上代码:
1 package Analogy; 2 3 import javax.swing.*; //图形用户界面 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.io.*; 7 import java.text.SimpleDateFormat; 8 import java.util.*;//已经包含util.random跟date跟scanner 9 10 class Edge extends JFrame implements ActionListener{ 11 JButton btonOnce; //单抽 12 JButton btonTen; //十连 13 JButton btonKrypton;//氪尼玛个锤子 14 JButton probability;//概率显示 15 JLabel bergCrystal, img, list, remainder;//list没有用到 16 JTextArea txt; //出货显示 17 JScrollPane pane; //滚动条 18 public static Integer crystal; //水晶数 19 JPanel panelImg, panelTop, panelBottom, panelList, panelRelate; 20 public static int total; //究极大保底标记 21 public static int cnt; //抽取次数(取模10) 22 23 public Edge(){//构造方法 24 setTitle("崩坏3抽卡单机模拟"); 25 crystal = 28000; 26 total = 0; //总已抽取次数(取模100) 27 cnt = 0; 28 btonOnce = new JButton("单抽"); 29 btonTen = new JButton("十连"); 30 btonKrypton = new JButton("648"); 31 probability = new JButton("概率显示"); 32 33 bergCrystal = new JLabel("水晶: " + Integer.toString(crystal), JLabel.CENTER); 34 Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 24);//斜体加粗 35 Font fntp = new Font("Serief", Font.PLAIN + Font.BOLD, 20);//非斜体 36 btonKrypton.setFont(fnt); //设置氪金按钮中的字体 37 btonKrypton.setForeground(Color.black); 38 probability.setFont(fntp); //设置概率按钮中的字体 39 probability.setForeground(Color.black); 40 btonOnce.setFont(fntp); //设置单抽按钮中的字体 41 btonOnce.setForeground(Color.black); 42 btonTen.setFont(fntp); //设置十连按钮中的字体 43 btonTen.setForeground(Color.black); 44 45 bergCrystal.setFont(fnt); //设置水晶数标签中的字体 46 bergCrystal.setForeground(Color.BLUE); //设置字体颜色 47 bergCrystal.setToolTipText("亮晶晶就剩这么点啦"); 48 //list = new JLabel("角色补给出货列表"); 49 remainder = new JLabel("剩余\n" + Integer.toString(100 - total) + "\n次必出S卡", JLabel.CENTER); 50 //剩余次数必出S 51 remainder.setFont(fnt); //设置剩余补给必出数标签中的字体 52 remainder.setForeground(Color.darkGray); 53 remainder.setToolTipText("剩余次数必出S"); 54 txt = new JTextArea(20, 15);//20行15列 55 Font fntxt = new Font("Serief", Font.PLAIN , 16); 56 txt.setFont(fntxt); //设置文本域中的字体样式 57 txt.setForeground(Color.DARK_GRAY); 58 txt.setText("历史事件:\n"); 59 txt.setCaretPosition(txt.getText().length());//光标自动显示到最后一行 60 pane = new JScrollPane(txt); //文本域带滚动条 61 62 Container cp = getContentPane();//总容器 63 panelList = new JPanel(); //出货列表 64 panelList.setLayout(new FlowLayout()); 65 //panelList.add(list); //tips 66 panelList.add(pane, BorderLayout.CENTER); //滚动条 67 //panelList.add(txt); //历史事件文本框(抽卡/概率显示) 68 69 panelTop = new JPanel(); //顶部面板 70 panelTop.setLayout(new GridLayout(0, 2)); 71 panelTop.add(bergCrystal); //水晶数量 72 panelTop.add(btonKrypton); //“氪金”按钮 73 74 Icon pic = new ImageIcon(CardDrawing.class.getResource("/resource/tmp.png")); 75 //图片路径。导出之后的jar总是图片丢失,找了好几篇博客最终在下面的文章下解决了 76 //https://blog.csdn.net/only06/article/details/54319752 77 img = new JLabel("", pic, JLabel.CENTER); 78 79 /*https://www.cnblogs.com/yangf428/p/11066406.html 80 * 另一种导入图片方法,较适用于图像是从一个不确定的输入流(如数据库中读)中导入 81 File file = new File(picPath); // 实例化File类的对象 82 InputStream input = null; 83 byte b[] = new byte[(int) file.length()]; //根据图片大小设置字节数组的长度 84 try{ 85 input = new FileInputStream(file); //实例化文件字节输入流 86 input.read(b); //从图片中读取字节到字节数组中 87 input.close(); //关闭文件流 88 } 89 catch (Exception e){ 90 e.printStackTrace(); 91 } 92 Icon icon = new ImageIcon(b); //使用字节数组,实例化ImageIcon对象 93 // 实例化标签对象:带图片,居中对齐 94 img = new JLabel("", icon, JLabel.CENTER); //窗口中间大图片 95 */ 96 img.setToolTipText("只要2w8"); 97 98 panelImg = new JPanel(); //图片面板 99 panelImg.setLayout(new GridLayout(0 ,1)); 100 panelImg.add(img); //骗氪立绘 101 102 panelRelate = new JPanel(); //池子相关(概率详情/剩余次数) 103 panelRelate.setLayout(new GridLayout(2, 0)); 104 panelRelate.add(remainder); //剩余次数必出S 105 panelRelate.add(probability); //概率详情显示按钮 106 107 panelBottom = new JPanel(); //底部面板 108 panelBottom.setLayout(new GridLayout(0 ,2)); 109 panelBottom.add(btonOnce); //单抽按钮 110 panelBottom.add(btonTen); //十连按钮 111 112 //将上面5个面板添加到总容器中 113 cp.add(panelTop, BorderLayout.NORTH); 114 cp.add(panelImg, BorderLayout.CENTER); 115 cp.add(panelBottom, BorderLayout.SOUTH); 116 cp.add(panelList, BorderLayout.WEST); 117 cp.add(panelRelate, BorderLayout.EAST); 118 119 AL listener = new AL(); //事件监听器 120 //bergCrystal.addActionListener(listener); 121 btonKrypton.addActionListener(listener); 122 probability.addActionListener(listener); 123 btonOnce.addActionListener(listener); 124 btonTen.addActionListener(listener); 125 126 setVisible(true); 127 setBounds(360, 240, 960, 600); 128 //以屏幕左上角为原点,右移360下移240构造窗口 129 //宽960,高600 130 setResizable(false);//不能修改窗口大小 131 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 132 } 133 134 @Override 135 public void actionPerformed(ActionEvent arg0) { 136 // TODO Auto-generated method stub 137 } 138 139 class AL implements ActionListener{ 140 Integer rand; 141 142 public void actionPerformed(ActionEvent e){ 143 JButton tmp = (JButton)e.getSource(); 144 SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 145 //设置日期格式 146 String nowTime = now.format(new Date()); 147 // new Date()获取当前系统时间 148 if (tmp == btonKrypton){ 149 crystal += 6480; 150 bergCrystal.setText(Integer.toString(crystal)); 151 txt.append("水晶数增加了!\n " + nowTime + "\n\n"); 152 } //点击氪金按钮,水晶数增加 153 else if((tmp == btonOnce && crystal < 280)||(tmp == btonTen && crystal < 2800)){ 154 txt.append("舰长水晶不够啦(氪嘛氪嘛~)\n " + nowTime + "\n\n"); 155 } //水晶数不够抽取 156 else if(tmp == btonOnce && crystal >= 280){ 157 crystal -= 280; 158 bergCrystal.setText(Integer.toString(crystal)); 159 for (int i = 0; i <= 0; i++){ 160 rand = new Random().nextInt(10000) + 1; 161 //随机数1到1w,除以100即为百分数 162 if (total == 99){ 163 txt.append("S-Card\n " + nowTime + "\n\n"); 164 cnt = -1; 165 total = -1; 166 remainder.setText("剩余\n" + Integer.toString(100 - total) + "\n次必出S卡"); 167 break; 168 } 169 if (cnt == 9) //保底 170 rand /= 100; 171 if (rand <= 15){ 172 txt.append("S-Card\n " + nowTime + "\n"); 173 cnt = -1; 174 total = -1; 175 } 176 else if (rand > 1500) 177 txt.append("Rubbish\n " + nowTime + "\n"); 178 else{ 179 txt.append("A-Card\n " + nowTime + "\n"); 180 cnt = -1; 181 } 182 } 183 cnt = (cnt + 1)%10; 184 total ++; 185 remainder.setText("剩余\n" + Integer.toString(100 - total) + "\n次必出S卡"); 186 } //点击单抽按钮 187 else if(tmp == btonTen && crystal >= 2800){ 188 crystal -= 2800; 189 bergCrystal.setText(Integer.toString(crystal)); 190 for (int i = 0; i <= 9; i++){ 191 rand = new Random().nextInt(10000) + 1; 192 //随机数1到1w,除以100即为百分数 193 if (total == 99){ 194 txt.append("S-Card\n"); 195 cnt = 0; 196 total = 0; 197 remainder.setText("剩余\n" + Integer.toString(100 - total) + "\n次必出S卡"); 198 continue; 199 } 200 if (cnt == 9) //保底 201 rand /= 100; 202 if (rand <= 15){ 203 txt.append("S-Card\n"); 204 cnt = 0; 205 total = 0; 206 } 207 else if (rand > 1500){ 208 txt.append("Rubbish\n"); 209 cnt = (cnt + 1)%10; 210 total ++; 211 } 212 else{ 213 txt.append("A-Card\n"); 214 cnt = 0; 215 total ++; 216 } 217 } 218 txt.append(" " + nowTime + "\n\n"); 219 remainder.setText("剩余\n" + Integer.toString(100 - total) + "\n次必出S卡"); 220 } //点击十连按钮 221 else if(tmp == probability) //点击概率详情按钮 222 txt.append("概率详情:\nS-Card: 1.5%\nA-Card: 13.5%\nOthers: 85%\n " + nowTime + "\n\n"); 223 } 224 } 225 } 226 227 public class CardDrawing { 228 public static void main(String[] args) { 229 new Edge(); 230 } 231 }
原文地址:https://www.cnblogs.com/tinfy/p/12247436.html
时间: 2024-10-07 18:42:51