跪求JAVA写的小游戏源代码。
导入Java . applet . *;
导入Java . awt . event . *;
导入Java . awt . image . *;
导入Java . awt . geom . *;
导入Java . util . *;
汽车等级{
图像img
int x,y;
维度dim
汽车(图像图像){
this.img = img
}
汽车(图像图像,整数x,整数y){
this.img = imgthis.x = xthis.y = y
}
汽车(图像图像,中间x,中间y,尺寸尺寸){
this(img,x,y);
this.dim = dim
}
void draw(Graphics g,ImageObserver observer){
g.drawImage(img,x,y,observer);
}
int getX(){ return x;}
int getY(){ return y;}
void setX(int x){ this . x = x;}
void setY(int y){ this . y = y;}
void setLocation(int x,int y){
this.x = xthis.y = y
}
int getWidth(){ return img . getWidth(null);}
int getHeight(){ return img . getHeight(null);}
Rectangle2D getRectangle(){
返回新的矩形2D。Float(x,y,getWidth(),getHeight());
}
无效移动(int dx,int dy){
x+= dx;
y+= dy;
如果(dim!=null){
if(x & lt;0)x = 0;
if(x+getWidth()>;dim.getWidth())
x =(int)dim . getWidth()-getWidth();
}
}
布尔相交(汽车汽车){
返回getRectangle()。intersects(car . get rectangle());
}
布尔交集(int x,int y){
返回getRectangle()。intersects(x,y,getWidth(),getHeight());
}
}
公共类CarRace扩展Applet实现KeyListener,Runnable{
图像缓冲器;
帆布屏幕;
Graphics2D gs,GB;
汽车redCar
车[]敌=新车[20];
按钮bStart
线程游戏;
布尔循环=真;
Dimension dim =新维度(200,300);
int road
Random rnd = new Random();
public void init(){
prepare resource();
set background(color . blue);
initScreen();
添加(屏幕);
BStart=new Button("巴老焦磊");
添加(bStart);
bStart.addActionListener(新的ActionListener(){
public void action performed(action event AE){
screen . request focus();
如果(!game.isAlive())
game . start();
}
});
}
void prepareResource(){
image imgRed = getImage(getCodeBase()," images/red _ car . gif));
image img blue = getImage(getCodeBase()," images/blue _ car . gif));
image img green = getImage(getCodeBase()," images/green _ car . gif));
media tracker mt = new media tracker(this);
尝试{
mt.addImage(imgRed,0);
mt.addImage(imgBlue,1);
mt.addImage(imgGreen,2);
mt . waitforall();
}catch(异常e){}
buff = create image((int)dim . getwidth(),(int)dim . getheight());
GB =(graphics 2d)buff . get graphics();
redCar =新车(imgRed,80,250,dim);
for(int I = 0;我& lt10;i++){
敌人[I]=新车(imgBlue,0,0);
}
for(int I = 10;我& lt敌人.长度;i++){
敌人[I]=新车(imgGreen,0,0);
}
for(int I = 0;我& lt敌人.长度;i++){
setEnemy(一);
}
游戏=新帖(这个);
}
公共无效站点(){
loop = false
}
公共无效运行(){
while(循环){
draw screen();
试试{ thread . sleep(50);}catch(异常e){}
}
}
void initScreen(){
screen=new Canvas(){
公共空白油漆(图形g){
if(gs==null){
GS =(graphics 2d)screen . get graphics();
}
draw screen();
}
};
screen . setsize(dim);
screen . addkey listener(this);
}
void敌国(int en){
int x,y;
下一个:while(true){
x = rnd . nextint((int)dim . getwidth()-enemy[en]。getWidth());
y =-rnd . nextint(5000)-200;
for(int j = 0;j & lt敌人.长度;j++){
如果(j!= en & amp& amp敌人[j]。intersects(x,y))继续下一步;
}
敌人。setLocation(x,y);
打破;
}
}
作废支票(汽车)
if(redCar.intersects(en)){
if(red car . getx()& gt;en.getX()){
en.move(-20,0);
redCar.move(20,0);
}
否则{
en.move(20,0);
redCar.move(-20,0);
}
}
}
同步void drawScreen(){
gb.clearRect(0,0,(int)dim.getWidth(),(int)dim . getheight());
gb.setPaint(新颜色(100,100,100));
gb.fillRect(0,0,(int)dim.getWidth(),(int)dim . getheight());
draw road();
for(int I = 0;我& lt敌人.长度;i++){
敌人[我]。移动(0,15);
敌人[我]。draw(gb,屏幕);
如果(敌人[我]。getY()& gt;dim.getHeight())
setEnemy(一);
检查(敌人[我]);
}
redCar.draw(gb,屏幕);
gs.drawImage(buff,0,0,screen);
}
void drawRoad(){
道路+= 80;
GB . setpaint(color . yellow);
GB . fill rect((int)dim . getwidth()/2,road,10,150);
if(road & gt;= dim . getheight())road =-150;
}
公共void按键(按键事件ke){
if(ke.getKeyCode()==KeyEvent。VK _左){
redCar.move(-10,0);
}
else if(ke . get key code()= = key event。VK _右){
redCar.move(10,0);
}
}
public void key released(key event ke){ }
public void key typed(key event ke){ }
}
这是一个典型的小程序,文件名应该是CarRace.java,我需要汽车等图片。我也是别人给的,但我需要一个。html文件来运行它,代码是:
& ltapplet code = " carrace . class " width = 250 height = 350 & gt;
& lt/Applet & gt;
求楼主采纳