内容发布更新时间 : 2024/11/13 21:22:26星期一 下面是文章的全部内容请认真阅读。
package com;
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle;
import java.awt.event.KeyEvent; import java.io.IOException;
import javax.imageio.ImageIO;
public class Plane { Image feijiImage = null; int x = 300; int y = 700; int lifeCount=5; public Plane() { try { feijiImage = ImageIO.read(Plane.class.getClassLoader() .getResourceAsStream(\ } catch (IOException e) { e.printStackTrace(); } } public void draw(Graphics g) { // 画飞机图片 g.drawImage(feijiImage, x, y, null); // 飞机移动 this.move(); // 血条 if(lifeCount>0){ g.setColor(Color.WHITE); g.fillRect(20, 80, 100, 10); g.setColor(Color.red); g.fillRect(20, 80, (100/5)*lifeCount, 10); g.setColor(Color.blue); g.setFont(new Font(\幼圆\ g.drawString(\ } }
//
public void move(){ if(isUP && !isDown && !isLeft && !isRight){ 上 y=y-5; }else if(!isUP && isDown && !isLeft && !isRight){
下
y=y+5;
}else if(!isUP && !isDown && isLeft && !isRight){ 左 x=x-5;
}else if(!isUP && !isDown && !isLeft && isRight){ 右 x=x+5;
}else if(isUP && !isDown && isLeft && !isRight){ 左上 x=x-5; y=y-5;
}else if(!isUP && isDown && isLeft && !isRight){ 左下 x=x-5; y=y+5;
}else if(isUP && !isDown && !isLeft && isRight){ 右上 x=x+5; y=y-5;
}else if(!isUP && isDown && !isLeft && isRight){ 右下 x=x+5; y=y+5; }
// // // // // // //
}
boolean isUP = false; boolean isDown = false; boolean isLeft = false; boolean isRight = false;
// 摁下
public void keyPressed(KeyEvent e) {
}
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) { isRight=true;
} else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) { isLeft=true;
} else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) { isUP=true;
} else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) { isDown=true; }
// 放开 public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) { isRight=false; } else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) { isLeft=false; } else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) { isUP=false; } else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) { isDown=false; } } public Rectangle getRectangle(){ return new Rectangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null)); } }
package com;
public class PlaneStatus { public static int roleNum = 1; public static int playStatus = 0;// 0 游戏开始前 1 第一关 -1 游戏结束 }
package com;
import java.awt.Graphics; import java.awt.Image;