Java小程序賽馬游戲?qū)崿F(xiàn)過程詳解
這是當(dāng)時(shí)做的一個(gè)小游戲,大概就是程序開始后,會(huì)進(jìn)入用戶登錄界面,用戶輸入自己的姓名和密碼后,選擇登錄會(huì)進(jìn)入到賽馬比賽的界面,這時(shí)可以看見賽馬場(chǎng)和馬匹的編號(hào),用戶可以選擇任何一個(gè)編號(hào)的馬進(jìn)行投注,輸入有效的投注金額(因?yàn)橄到y(tǒng)設(shè)置了初始金額,所以不得投注的超過初始金額)。投注完成后用戶可以選擇開始比賽,賽馬期間用戶不得進(jìn)行任何操作,賽馬結(jié)束后,用戶可以重新進(jìn)行新一輪的賽馬比賽,方法上同。
程序流程圖:
一、賽馬比賽模塊
其中此模塊包括對(duì)畫出馬匹和賽道部分,通過使用Draw類來向Jpanel面板中添加馬和賽道。
部分代碼:
class Draw extends JPanel{int x=0;String s;int w,h;public void paint(Graphics g){super.paint(g);this.setBackground(Color.WHITE);w=this.getWidth();h=this.getHeight();g.setColor(Color.BLACK);g.drawLine(66,h/2-44,666,h/2-44);g.drawLine(66,h/2+40,666,h/2+40);g.setColor(Color.BLACK);g.drawLine(66,0,66,h);g.setColor(Color.red);g.drawLine(666,0,666,h);g.drawRect(36+x,h/2-10,30,20);//馬的顯示g.setColor(Color.BLACK);//文字顯示g.drawString(s,26,h/2-12);}}
二、投注區(qū)模塊
a.投注馬匹模塊
主要是實(shí)現(xiàn)馬匹的選擇及投注的模擬,增加JRadioButton單選按鈕選擇馬匹。
b. 投注金額模塊
TextField來輸入投注金額,tfget接收后與總金額進(jìn)行比較。
c. 賭金變化模塊
同時(shí)變量tz控制金額改變。
代碼:
public class Run extends JFrame implements ActionListener{……JPanel p3;//投注區(qū)JButton jb;//開始按鈕JFrame frame;DateUtil d1=new DateUtil();Boolean cotrol;//對(duì)投注額改變的變量int no;//名次編號(hào)String s;//投注單選事件的變量符號(hào)JRadioButton b1,b2,b3,b4;//投注的單選按鈕ButtonGroup bg;//按鈕注,使其為單選JTextArea ta;//賽馬的文字顯示JLabel money;//投注總金額標(biāo)簽TextField tf;//投注金額的輸入窗口int tz;//投注金額變化的變量String tfget;//投注總金額的中轉(zhuǎn)變量FlowLayout ly;//布局變量}public void actionPerformed(ActionEvent e){if(bg.getSelection()!=null){//投注單選按鈕選擇確認(rèn)if(e.getActionCommand()=='開始'){tfget=tf.getText();try{if(Integer.parseint(tfget)>0&&Integer.parseint(tfget)<=tz)//string轉(zhuǎn)換成int{//投注金額確認(rèn)ta.setText('比賽開始了'+’n’);new racing(h1,this).start();new racing(h2,this).start();new racing(h3,this).start();new racing(h4,this).start();jb.setText('重新開始');//賽馬按鍵標(biāo)簽改變b1.setEnabled(false);//賽馬開始后,禁止操作b2.setEnabled(false);b3.setEnabled(false);b4.setEnabled(false);tf.setEditable(false);jb.setEnabled(false);}else{JOptionPane.showMessageDialog(null, '投注金額錯(cuò)誤,請(qǐng)重新投注');//信息對(duì)話框}}catch(Exception ex){JOptionPane.showMessageDialog(null, '投注格式錯(cuò)誤,請(qǐng)重新投注');}}}else{JOptionPane.showMessageDialog(null, '請(qǐng)選號(hào)確認(rèn)投注');}
三、線程啟動(dòng)及結(jié)束模塊
a. 線程啟動(dòng)及結(jié)束部分模塊
模塊代碼:
public void actionPerformed(ActionEvent e){……ta.setText('比賽開始了'+’n’);new racing(h1,this).start();new racing(h2,this).start();new racing(h3,this).start();new racing(h4,this).start();jb.setText('重新開始');//賽馬按鍵標(biāo)簽改變b1.setEnabled(false);//賽馬開始后,禁止操作b2.setEnabled(false);b3.setEnabled(false);b4.setEnabled(false);tf.setEditable(false);jb.setEnabled(false);}……if(e.getActionCommand()=='重新開始'){//重新開始,相應(yīng)的控制重置restar(h1,this);restar(h2,this);restar(h3,this);restar(h4,this);cotrol=true;no=0;ta.setText('');tf.setEditable(true);b1.setEnabled(true);b2.setEnabled(true);b3.setEnabled(true);b4.setEnabled(true);jb.setText('開始');}}class racing extends Thread{//馬運(yùn)動(dòng)的線程控制Draw a;Run r ;DateUtil d=new DateUtil();racing(Draw h,Run b){this.a=h;this.r=b;}
b. 獲取比賽初始系統(tǒng)時(shí)間模塊
線程run啟動(dòng)時(shí),通過使用Calendar類來獲取比賽開始時(shí)的系統(tǒng)時(shí)間,其中使用getNow_HMS()方法來獲取比賽的初始時(shí)間。
模塊代碼:
import java.util.;public class DateUtil{public static String getNow_M(){Calendar c=Calendar.getInstance();String minute=String.valueOf(c.get(Calendar.MINUTE));if(minute.length()==1){minute='0'+minute;}String ms1=minute;return ms1;}public static String getNow_S(){Calendar c=Calendar.getInstance();String second=String.valueOf(c.get(Calendar.SECOND));if(second.length()==1){second='0'+second;}String ms2=second;return ms2;}public int getNow_HMS(){String s1,s2;s1=getNow_M();s2=getNow_S();int a=Integer.parseint(s1)60+Integer.parseint(s2);return a;}public static void main(String[] args) {DateUtil d;d=new DateUtil();d.getNow_HMS();}}public int Time(){int oo=d1.getNow_HMS();return oo;}
c. 獲取比賽結(jié)束系統(tǒng)時(shí)間模塊
當(dāng)比賽結(jié)束,線程終止時(shí),通過使用Calendar類來獲取比賽結(jié)束時(shí)的系統(tǒng)時(shí)間,其中使用getNow_HMS()方法來獲取比賽的結(jié)束時(shí)間。
模塊代碼:
public void run(){int t;int o,y;o=r.Time();……y=d.getNow_HMS();int z=y-o;ta.append('用時(shí):'+z+'秒'+’n’);}
四、比賽結(jié)果顯示區(qū)模塊
在JTextArea()分布的空間內(nèi),通過方法append()顯示馬匹的名次,用時(shí)等信息。
五、賽馬游戲運(yùn)行結(jié)果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA2020.2.2創(chuàng)建Servlet方法及404問題2. docker /var/lib/docker/aufs/mnt 目錄清理方法3. Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置4. Python 多線程之threading 模塊的使用5. Python中Selenium模塊的使用詳解6. Python基于smtplib模塊發(fā)送郵件代碼實(shí)例7. CSS代碼檢查工具stylelint的使用方法詳解8. IDEA巧用Postfix Completion讓碼速起飛(小技巧)9. Python如何批量獲取文件夾的大小并保存10. CSS3中Transition屬性詳解以及示例分享
