文件目录:

工具类:


工具类:
public class Constant {
public static final int GAME_WIDTH=300;//游戏中的常量
public static final int GAME_HEIGHT=500;
public static final int Time_lapse=50;
}
public class GameUtil {
public static Image getImage(String path){
BufferedImage bi=null;
try {
URL u=GameUtil.class.getClassLoader().getResource(path);
bi=javax.imageio.ImageIO.read(u);
} catch (Exception e) {
e.printStackTrace();
}
return bi;
}
}
public class MyFrame extends Frame{
public void launchFrame(){//加载窗口
setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
setLocation(100,100);
setVisible(true);
new PaintThread().start();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class PaintThread extends Thread{
@Override
public void run() {
while(true){
repaint();
try {
Thread.sleep(Constant.Time_lapse);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
实体类:public class Bullet extends GameObject{
double degree;
public Bullet(){
degree=Math.random()*Math.PI*2;
x=Constant.GAME_WIDTH/2;
y=Constant.GAME_HEIGHT/2;
width=10;
height=10;
speed=5;
}
public void draw(Graphics g){
Color c=g.getColor();
g.setColor(Color.yellow);
g.fillOval((int)x,(int)y, width, height);
x+=speed*Math.cos(degree);
y+=speed*Math.sin(degree);
if (y>Constant.GAME_HEIGHT-17||y<30) {
degree=-degree;
}
if (x>Constant.GAME_WIDTH-16||x<7) {
degree=Math.PI-degree;
}
g.setColor(c);
}
}
public class Explode {
double x,y;
static Image[] images=new Image[16];
int count;
static{
for (int i = 0; i < 16; i++) {
images[i]=GameUtil.getImage("Explode/e"+(i+1)+".gif");
images[i].getWidth(null);
}
}
public void draw(Graphics g){
if (count<=15) {
g.drawImage(images[count],(int)x,(int)y, null);
count++;
}
}
public Explode(double x,double y) {
this.x=x;
this.y=y;
}
}
public class GameObject {
Image image;
double x,y;
int speed=10;
int width,height;
public Rectangle getRect(){
return new Rectangle((int)x,(int)y,width,height);
}
public GameObject(Image image, double x, double y, int speed, int width, int height) {
super();
this.image = image;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}
public GameObject(){
}
}
public class plane extends GameObject{
private boolean left,up,right,down;
private boolean live=true;
public void draw(Graphics g){
if (live) {
g.drawImage(image,(int)x,(int)y, null);
move();
}
}
public void move(){
if (left) {
x-=speed;
}
if (right) {
x+=speed;
}
if (up) {
y-=speed;
}
if (down) {
y+=speed;
}
}
public plane(String imagePath, double x, double y) {
this.image =GameUtil.getImage(imagePath);
this.x = x;
this.y = y;
this.width=image.getWidth(null);
this.height=image.getHeight(null);
}
public void addDirection(KeyEvent e){
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left=true;
break;
case KeyEvent.VK_UP:
up=true;
break;
case KeyEvent.VK_RIGHT:
right=true;
break;
case KeyEvent.VK_DOWN:
down=true;
break;
default:
break;
}
}
public void minusDirection(KeyEvent e){
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left=false;
break;
case KeyEvent.VK_UP:
up=false;
break;
case KeyEvent.VK_RIGHT:
right=false;
break;
case KeyEvent.VK_DOWN:
down=false;
break;
default:
break;
}
}
public plane(){
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
}
public class PlaneGameFrame extends MyFrame{
Image bg=GameUtil.getImage("images/bg.jpg");
plane p=new plane("images/plane.png",50,50);
ArrayList bulletList=new ArrayList();
Date startTime;
Date endTime;
Explode bao;
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
p.draw(g);
for (int i = 0; i < bulletList.size(); i++) {
Bullet b =(Bullet)bulletList.get(i);
b.draw(g);
//检测与飞机的碰撞
boolean peng=b.getRect().intersects(p.getRect());
if (peng) {
p.setLive(false);//飞机死掉
if (bao==null) {
bao=new Explode(p.x, p.y);
endTime=new Date();
}
bao.draw(g);
break;
}
}
if (!p.isLive()) {
int period=(int)((endTime.getTime()-startTime.getTime())/1000);
printInfo(g,"时间"+period+"秒",20,20,220, Color.white);
switch (period/10) {
case 0:
case 1:
printInfo(g,"菜鸟",20,20,160, Color.white);
break;
case 2:
printInfo(g,"小鸟",20,20,160, Color.white);
break;
case 3:
printInfo(g,"鸟王子",20,20,160, Color.white);
break;
case 4:
printInfo(g,"鸟人",20,20,160, Color.white);
break;
default:
break;
}
}
}
public void printInfo(Graphics g,String str,int size,int x,int y,Color color){
Color c=g.getColor();
g.setColor(color);
Font f=new Font("宋体",Font.BOLD,size);
g.setFont(f);
g.drawString(str,x,y);
g.setColor(c);
}
//双缓冲
private Image offScreenImage=null;
public void update(Graphics g){
if (offScreenImage==null){
offScreenImage=createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
}
Graphics gOff=offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
public static void main(String[] args) {//主函数在这里
new PlaneGameFrame().launchFrame();
}
public void launchFrame(){
super.launchFrame();
addKeyListener(new KeyMonitor());
for (int i = 0; i <6; i++) {//生成子弹
Bullet b=new Bullet();
bulletList.add(b);//装入容器
}
startTime=new Date();
}
//定义为内部类,可以方便的使用外部属性
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
p.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
p.minusDirection(e);
}
}
}
效果图: