java赛马游戏
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
赛马游戏源代码:(截图)
package com.guigu.king.core;
public interface HandleListener
{
/**
* 完成比赛
*/
public void gameHourse(HourseEvent e);
/**
* 比赛结束(线程结局)
*/
public void gameOver(HourseEvent e); }
package com.guigu.king.core;
public class HourseEvent
{
private Object source=null;
private boolean error=false;
private Exception exception=null;
public HourseEvent(Object source)
{
this.source=source;
}
public Object getSource() {
return source;
}
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public Exception getException() {
return exception;
}
public void setException(Exception exception) { this.exception = exception;
}
}
package com.guigu.king.core;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
/**
* 赛马类
* @author Administrator
*/
public class HourseRace implements Runnable
{
public HandleListener handle=null;
public void addHandleListener(HandleListener handle)
{
this.handle=handle;
}
private String name="";//赛马名称
private Image img=null;//赛马图片
private int length=0;//赛马的步长(每跑一步的距离)
private int[] rate=null;//赛马的步频(每一次奔跑的时间间隔)
public HourseRace(String name,String imgPath,int length,int[] rate) {
=name;
this.img=Toolkit.getDefaultToolkit().getImage(imgPath);
this.length=length;
this.rate=rate;
}
private int x=0;//赛马当前赛跑的位置
private int range=0;//赛跑的距离
private int stop=0;//赛跑的距离
public void setRaceData(int range,int stop)
{
this.x=0;
this.range=range;
this.stop=stop;
}
public void run()
{
HourseEvent event=new HourseEvent(this);
try
{
boolean b=true;
Random r=new Random();
while(true)
{
int n=r.nextInt(rate.length);
if (b && this.x>=this.range-116)
{
b=false;
if (handle!=null) handle.gameHourse(event);
}
this.x=this.x+this.length;
Thread.sleep(rate[n]);
if (x>=this.stop)
{
break;
}
}
}
catch(Exception e)
{
event.setError(true);
event.setException(e);
}
this.x=0;
if (handle!=null) handle.gameOver(event);
}
public String getName() {
return name;
}
public Image getImg() {
return img;
}
public int getX() {
return x;
}
}
package com.guigu.king.core;
/**
* 赛场类
* @author Administrator
*/
public class Match
{
private String name="";//赛场的名称
private int range=0;//赛跑的距离