Java-飞机小游戏

今天放假第二天,一个人在家很无聊,跟着b站写了一个飞机游戏的小项目。

1.画一个窗体继承Frame类,因为JFrame闪烁特别严重,加一段双缓冲技术,解决闪烁问题

2.加入背景和飞机的图片,写一个GameUitl类使用

URL u=GameUitl.class.getClassLoader().getResource(path);
  bi=ImageIO.read(u);方法显示图片

3.写一个GameObject父类,飞机Plane炮弹Shell都继承此类,在类中右击-Source-Generate getters and setters to create 来写构造器。

4.在飞机类中增加两个获得和减少键盘的方法,在方法中使用getKeyCode()方法获得键盘数据。并重写一个改变飞机位置的方法。

5.在弹药类中用Graphics g画笔自己画炮弹g.fillOval((int)x, (int)y, width, height);画实体椭圆。并用数学方法实现任意角度飞及碰撞反弹。

 //任意角度去飞
  x+=speed*Math.cos(degree);
  y+=speed*Math.sin(degree);
  
  //碰撞反弹
  if(x<0||x>Constant.Game_width-width){
   degree=Math.PI-degree;

 }
  if(y<30||y>Constant.Game_heigth-height){
   degree=-degree;

}

6.写一个爆炸类,实现飞机爆炸时图片的轮流播放

扫描二维码关注公众号,回复: 4958970 查看本文章

public class Explore {
 double x,y;
 static Image[] imgs=new Image[16];
 static{
 for(int i=0;i<16;i++){
  
  
  imgs[i]=GameUitl.getImages("images/explode/e"+(i+1)+".gif");
  imgs[i].getWidth(null);
 
 }
  
 }
 int count;
 //轮播实现爆炸图片
 public void draw(Graphics g){
  if(count<16){
   g.drawImage(imgs[count], (int)x,(int) y, null);
   count++;
  }
 }
 public Explore(double x,double y){
  this.x=x;
  this.y=y;
 }
}

7.检验碰撞时使用获得图片矩形相撞的方法getRect().intersects(plane.getRect())

8.碰撞时实现一个计时功能


 

猜你喜欢

转载自www.cnblogs.com/stt-ac/p/10284868.html
今日推荐