- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.event.KeyEvent;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import javax.imageio.ImageIO;
- public class Plane extends MoveObject implements Fireable{
- private int x;
- private int y;
- private int width = 50;
- private int height = 50;
- private static Image image;
- private static String path = "src/image/plane.gif";
- private boolean isRDown;
- private boolean isLDown;
- private boolean isUDown;
- private boolean isDDown;
- private DIR dir;
- static{
- try {
- image = ImageIO.read(new File(path));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public Plane(){
- this.width = 50;
- this.height = 50;
- this.x = (PlaneFrame.WINDOW_WIDTH - this.width) / 2;
- this.y = (PlaneFrame.WINDOW_HEIGHT - this.height);
- }
- public void paint(Graphics g){
- g.drawImage(image, x , y , width , height,null);
- }
- public void keyPressed(int e) {
- switch (e) {
- case KeyEvent.VK_RIGHT:
- isRDown = true;
- break;
- case KeyEvent.VK_LEFT:
- isLDown = true;
- break;
- case KeyEvent.VK_UP:
- isUDown = true;
- break;
- case KeyEvent.VK_DOWN:
- isDDown = true;
- break;
- }
- makeDir();
- }
- public void keyRelease(int e){
- switch (e) {
- case KeyEvent.VK_RIGHT:
- isRDown = false;
- break;
- case KeyEvent.VK_LEFT:
- isLDown = false;
- break;
- case KeyEvent.VK_UP:
- isUDown = false;
- break;
- case KeyEvent.VK_DOWN:
- isDDown = false;
- break;
- }
- makeDir();
- }
- private void makeDir(){
- if(isLDown && !isRDown && !isUDown && !isDDown){
- dir = DIR.LT;
- }
- if(isLDown && isUDown && !isRDown && !isDDown){
- dir = DIR.LU;
- }
- if(isLDown && !isUDown && !isRDown && isDDown){
- dir = DIR.LD;
- }
- if(!isLDown && isUDown && !isRDown && !isDDown){
- dir = DIR.UP;
- }
- if(!isLDown && isUDown && isRDown && !isDDown){
- dir = DIR.RU;
- }
- if(!isLDown && !isUDown && isRDown && !isDDown){
- dir = DIR.RT;
- }
- if(!isLDown && !isUDown && isRDown && isDDown){
- dir = DIR.RD;
- }
- if(!isLDown && !isUDown && !isRDown && isDDown){
- dir = DIR.DN;
- }
- move(dir);
- }
- public void move(DIR dir){
- DirStep ds = hmDir.get(dir);
- int xx = x + 7 * ds.getXstep();
- int yy = y + 7 * ds.getYstep();
- if(xx >= 0 && xx <= PlaneFrame.WINDOW_WIDTH - this.width && yy >= 25 && yy <= PlaneFrame.WINDOW_HEIGHT - this.height){
- x = xx;
- y = yy;
- }
- }
- public void fire() {
- int sx = this.x + (this.width)/2 - 15;
- int sy = this.y - 25;
- Shell shell = new Shell(sx,sy,DIR.UP);
- PlaneFrame.shell.add(shell);
- }
- }