java写的雷霆战机,我想让子弹一直出来怎么办呢,大神们来帮忙啊,最好给出代码+注释

package com.simpre;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class PlaneWar extends JFrame{
BackGround backGround = new BackGround();
Plane plane = new Plane();

public PlaneWar(){
    this.setTitle("PlaneWar");
    this.setBounds(300,150,450,800);
    this.setVisible(true);
    this.setResizable(false);
    this.addKeyListener(new KeyControl());
    Thread thread = new Thread(new PaintThread());
    thread.start();
}

public void paint(Graphics g){
    backGround.draw(g);
    plane.draw(g);
}

class PaintThread implements Runnable{
    public void run(){
        while(true){
            repaint();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class KeyControl extends KeyAdapter{
    public void keyPressed(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_UP){
            plane.up =true;
        }else if(e.getKeyCode() == KeyEvent.VK_LEFT){
            plane.left =true;
        }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            plane.right =true;
        }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
            plane.down =true;
        }
    }
    public void keyReleased(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_UP){
            plane.up =false;
        }else if(e.getKeyCode() == KeyEvent.VK_LEFT){
            plane.left =false;
        }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            plane.right =false;
        }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
            plane.down =false;
        }
    }
}

public static void main(String args[]){
    new PlaneWar();
}

}

```package com.simpre;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Plane {
boolean left,up,right,down;
int planeSpeed =10;
int x=200,y=700;
Image imgPlane = GameUtil.getImage("Images/MyPlane.png");

public void draw(Graphics g){
    g.drawImage(imgPlane,x,y,80,60,null);
    if(left & x>0){
        x-=planeSpeed;
    }
    if(up & y>40){
        y-=planeSpeed;
    }
    if(right & x<380){
        x+=planeSpeed;
    }
    if(down & y<750){
        y+=planeSpeed;
    }
}

public Plane() {

}

}

```package com.simpre;

import java.awt.Graphics;
import java.awt.Image;

public class BackGround {
int y;
int BgSpeed=2;
Image imgBg = GameUtil.getImage("Images/BG.jpg");

public void draw(Graphics g){
    g.drawImage(imgBg,0,y,450,800,null);
    g.drawImage(imgBg,0,y-800,450,800,null);
    y+=BgSpeed;
    if(y>800){
        y=0;
    }
}

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐