java游戏--赛车模拟

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.*;
import java.util.*;
class Car{
 Image img;
 int x,y;
 Dimension dim;
 public Car(Image img){
  this.img=img;
 }
 public Car(Image img,int x,int y){
  this.x=x;
  this.y=y;
  this.img=img;
 }
 public Car(Image img,int x,int y,Dimension dim){
  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=x;
  this.y=y;
 }
 int getWidth(){return img.getWidth(null);}
 int getHeight(){return img.getHeight(null);}
 Rectangle2D getRectangle(){
  return new Rectangle2D.Float(x,y,getWidth(),getHeight());
 }
 void move(int dx,int dy){
  x+=dx;
  y+=dy;
  if(dim!=null){
   if(x<0) x=0;
   if((x+getWidth())>dim.getWidth())
    x=(int)dim.getWidth()-getWidth();
  }
 }
 boolean intersects(Car car){
  return getRectangle().intersects(car.getRectangle());
 }
 boolean intersects(int x,int y){
  return getRectangle().intersects(x,y,getWidth(),getHeight());
 }
}


public class CarRace extends Applet implements KeyListener,Runnable{
 Image buff;
 Canvas screen;
 Graphics2D gs,gb;
 Car redcar;
 Car[]enemy=new Car[20];
 Button bstart=new Button("start");
 Thread game;
 boolean loop=true;
 Dimension dim=new Dimension(200,300);
 int road;
 Random rnd=new Random();
 
 public void init(){
  prepareResource();
  setBackground(Color.blue);
  initScreen();
  add(screen);
  add(bstart);
  bstart.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    screen.requestFocus();
    if(!game.isAlive())
     game.start();
   }
  });
 }
 void prepareResource(){
   Image imgred=getImage(getCodeBase(),"image/red_car.gif");
   Image imgblue=getImage(getCodeBase(),"image/blue_car.gif");
   Image imggreen=getImage(getCodeBase(),"image/green_car.gif");
   MediaTracker mt=new MediaTracker(this);
   try{
    mt.addImage(imgred,0);
    mt.addImage(imgblue,1);
    mt.addImage(imggreen,2);
   }catch(Exception e){}
   buff=createImage((int)getWidth(),(int)getHeight());
   gb=(Graphics2D)buff.getGraphics();
   redcar=new Car(imgred,80,150);
   for(int i=0;i<10;i++){
    enemy[i]=new Car(imgblue,0,0);
   }
   for(int i=10;i<20;i++){
    enemy[i]=new Car(imggreen,0,0);
   }
   for(int i=0;i<enemy.length;i++){
    setEnemy(i);
   }
   game=new Thread(this);
 }
 public void stop(){
  loop=false;
 }
 void initScreen(){
  screen=new Canvas(){
   public void paint(Graphics g){
    if(gs==null){
     gs=(Graphics2D)screen.getGraphics();
    }
    drawScreen();
   }
  };
  screen.setSize(dim);
  screen.addKeyListener(this);
 }
 public void run(){
  while(loop){
   drawScreen();
   try{Thread.sleep(50);}catch(Exception e){}
  }
 }
 public void setEnemy(int en){
  int x,y;
  next:
  while(true){
   x=rnd.nextInt((int)dim.getWidth()-enemy[en].getWidth());
   y=-rnd.nextInt(5000)-200;
   for(int j=0;j<enemy.length;j++){
    if(j!=en && enemy[j].intersects(x, y))continue next;
   }
   enemy[en].setLocation(x,y);
   break;
  }  
 }
 public void check(Car en){
  if(redcar.intersects(en)){
   if(redcar.getX()>en.getX()){
    en.move(-40,0);
    redcar.move(40,0);
   }
   else{
    en.move(40, 0);
    redcar.move(-40, 0);
   }
  }
 }
 synchronized void drawScreen(){
  gb.clearRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
  gb.setPaint(new Color(100,100,100));
  gb.fillRect(0, 0, (int)dim.getWidth(),(int)dim.getHeight());  
  drawRoad();
  for(int i=0;i<enemy.length;i++){
   enemy[i].move(0, 15);
   enemy[i].draw(gb, screen);  
   if(enemy[i].getY()>dim.getHeight()){
    setEnemy(i);
   check(enemy[i]); 
   }
  }
  redcar.draw(gb, screen);
  gs.drawImage(buff, 0, 0,screen);  
 }
 void drawRoad(){
  road=road+80;
  gb.setPaint(Color.yellow);
  gb.fillRect((int)dim.getWidth()/2,road,10,150);
  if(road>=dim.getHeight()) road=-150;
 }
 
 public void keyPressed(KeyEvent e){
  if(e.getKeyCode()==KeyEvent.VK_LEFT){
   //System.out.println("left");
   redcar.move(-10, 0);
   if(redcar.getX()<0)
    redcar.setX(0);
  }
  else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
   redcar.move(10, 0);
   if(redcar.getX()>dim.getWidth()-redcar.getWidth())
    redcar.setX((int)(dim.getWidth()-redcar.getWidth()));
   //System.out.println("right");
  }
 }
 
 public void keyReleased(KeyEvent e){}  
 public void keyTyped(KeyEvent e){} 
}
//<applet code="CarRace.class" width=250 height=500>   </Applet>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值