Java程序之赛马游戏

这是当时做的一个小游戏,大概就是程序开始后,会进入用户登录界面,用户输入自己的姓名和密码后,选择登录会进入到赛马比赛的界面,这时可以看见赛马场和马匹的编号,用户可以选择任何一个编号的马进行投注,输入有效的投注金额(因为系统设置了初始金额,所以不得投注的超过初始金额)。投注完成后用户可以选择开始比赛,赛马期间用户不得进行任何操作,赛马结束后,用户可以重新进行新一轮的赛马比赛,方法上同。

程序流程图:

一、赛马比赛模块
其中此模块包括对画出马匹和赛道部分,通过使用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);
}
}

二、投注区模块
a.投注马匹模块
主要是实现马匹的选择及投注的模拟,增加JRadioButton单选按钮选择马匹。
b. 投注金额模块
TextField来输入投注金额,tfget接收后与总金额进行比较。
c. 赌金变化模块
同时变量tz控制金额改变。

代码:
public class Run extends JFrame implements ActionListener
{
……
JPanel p3;//投注区
JButton jb;//开始按钮
JFrame frame;
DateUtil d1=new DateUtil();
boolean cotrol;//对投注额改变的变量
int no;//名次编号
String s;//投注单选事件的变量符号
JRadioButton b1,b2,b3,b4;//投注的单选按钮
ButtonGroup bg;//按钮注,使其为单选
JTextArea ta;//赛马的文字显示
JLabel money;//投注总金额标签
TextField tf;//投注金额的输入窗口
int tz;//投注金额变化的变量
String tfget;//投注总金额的中转变量
FlowLayout ly;//布局变量
}
public void actionPerformed(ActionEvent e)
{
if(bg.getSelection()!=null)
{//投注单选按钮选择确认
if(e.getActionCommand()"开始")
{
tfget=tf.getText();
try{
if(Integer.parseInt(tfget)>0&&Integer.parseInt(tfget)<=tz)//string转换成int
{//投注金额确认
ta.setText("比赛开始了"+' ');
new racing(h1,this).start();
new racing(h2,this).start();
new racing(h3,this).start();
new racing(h4,this).start();
jb.setText("重新开始");//赛马按键标签改变
b1.setEnabled(false);//赛马开始后,禁止操作
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
tf.setEditable(false);
jb.setEnabled(false);
}
else
{
JOptionPane.showMessageDialog(null, "投注金额错误,请重新投注");//信息对话框
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "投注格式错误,请重新投注");
}
}
}
else
{
JOptionPane.showMessageDialog(null, "请选号确认投注");
}
三、线程启动及结束模块
a. 线程启动及结束部分模块
模块代码:
public void actionPerformed(ActionEvent e)
{
……
ta.setText("比赛开始了"+' ');
new racing(h1,this).start();
new racing(h2,this).start();
new racing(h3,this).start();
new racing(h4,this).start();
jb.setText("重新开始");//赛马按键标签改变
b1.setEnabled(false);//赛马开始后,禁止操作
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
tf.setEditable(false);
jb.setEnabled(false);
}
……
if(e.getActionCommand()
"重新开始")
{//重新开始,相应的控制重置
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
{//马运动的线程控制
Draw a;
Run r ;
DateUtil d=new DateUtil();
racing(Draw h,Run b)
{
this.a=h;
this.r=b;
}
b. 获取比赛初始系统时间模块
线程run启动时,通过使用Calendar类来获取比赛开始时的系统时间,其中使用getNow_HMS()方法来获取比赛的初始时间。
模块代码:
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. 获取比赛结束系统时间模块
当比赛结束,线程终止时,通过使用Calendar类来获取比赛结束时的系统时间,其中使用getNow_HMS()方法来获取比赛的结束时间。
模块代码:
public void run()
{ int t;
int o,y;
o=r.Time();
……
y=d.getNow_HMS();
int z=y-o;
ta.append("用时:"+z+"秒"+' ');
}

四、比赛结果显示区模块
在JTextArea()分布的空间内,通过方法append()显示马匹的名次,用时等信息。

五、赛马游戏运行结果

原文地址:https://www.cnblogs.com/jingxinbk/p/12416402.html