2019-06-06 java飞机游戏项目

image.png

image.png

package cn.sxt.game;

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

public class Explode {

    double x ,y;
    static Image[] imgs=new Image[16];
    static {
        for(int i=0;i<16;i++) {
            imgs[i]=GameUtil.getImage("images/explode/e"+(i+1)+".gif");
            imgs[i].getWidth(null);
        }
    }
    int count;
    public void draw(Graphics g) {
        if(count<=15) {
            g.drawImage(imgs[count], (int)x, (int)y, null);
            count++;
        }
    }
    public Explode(double x,double y) {
        this.x=x;
        this.y=y;
    }
    
}
package cn.sxt.game;

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

public class GameObject {
    Image img;
    double x,y;
    int speed;
    int width,height;
    public void drawMySelf(Graphics g) {
        g.drawImage(img, (int)x, (int)y, null);
        
    }
    
    public GameObject(Image img,double x,double y) {
        this.img=img;
        this.x=x;
        this.y=y;
        if(img!=null) {
            this.width=img.getWidth(null);
            this.height=img.getHeight(null);
        }
        
    }
    public GameObject(Image img,double x,double y,int speed ,int width,int height) {
        
        this.img=img;
        this.x=x;
        this.y=y;
        this.speed=speed;
        this.width=width;
        this.height=height;
        
    }
    public GameObject() {
        
    }
    //返回物体对应的矩形区域,便于后续在碰撞检测中使用
    public Rectangle getRect() {
        return new Rectangle((int)x,(int)y,width,height);
    }

}
package cn.sxt.game;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;


public class GameUtil {

    //工具类最好将构造器私有化
    private GameUtil() {
        
    }
    public static Image getImage(String path) {
        BufferedImage bi=null;
        try {
            URL u=GameUtil.class.getClassLoader().getResource(path);
            bi=ImageIO.read(u);
        }catch(IOException e) {
            e.printStackTrace();
        }
        return bi;
    }
}

package cn.sxt.game;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowEvent;

import java.util.ArrayList;
import java.util.Date;

public class MyGameFrame extends Frame {
// 1.0增加计时功能
Date startTime = new Date();
Date endTime;

public void launchFrame() {
    // 游戏窗口打印标题
    setTitle("GIP作品");
    // 设置窗口默认可见
    setVisible(true);
    // 窗口大小高度500,宽度500
    setSize(500, 500);
    // 窗口左上角顶点坐标位置
    setLocation(300, 300);
    // 初始化,生成一堆炮弹
    for (int i = 0; i < 50; i++) {
        Shell b = new Shell();
        shellList.add(b);
    }

    // 增加关闭窗口监听,这样当用户单击右上角关闭按钮时候,可以关闭游戏程序
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);

        }

    });

    new PaintThread().start();
    addKeyListener(new KeyMonitor());

}

// 0.2版本paint方法画图形
/*
 * public void paint(Graphics g) { //从坐标点(100,50)到(400,400)画出直线 g.drawLine(100,
 * 50, 400, 400); //画出矩形 矩形左上角顶点坐标(100,500)宽度300 ,高度300 g.drawRect(100, 50, 300,
 * 300); //画出椭圆 ,椭圆外切矩形为左上角顶点坐标(100,50),宽度300,高度300 g.drawOval(100, 50, 300,300
 * ); }
 */
// 0.5版本前画画飞机方法
/*
 * Image bgImg=GameUtil.getImage("images/bg.jpg"); Image
 * planeImg=GameUtil.getImage("images/plane.png");
 * 
 * int planeX=200; int planeY=200; static int count =0;
 * 
 * public void paint(Graphics g) { g.drawImage(bgImg, 0, 0, null);
 * System.out.println("调用paint,重画窗口,次数:"+(count++)); g.drawImage(planeImg,
 * planeX, planeY, null); planeX+=3;
 * 
 * }
 */
// 飞机类封装后 画飞机方法
Image bgImg = GameUtil.getImage("images/bg.jpg");
Image planeImg = GameUtil.getImage("images/plane.png");
Plane plane = new Plane(planeImg, 300, 300, 5);

ArrayList shellList = new ArrayList();
// 创建爆炸对象
Explode bao;

public void paint(Graphics g) {
    g.drawImage(bgImg, 0, 0, null);
    // 画飞机本身
    plane.drawMyself(g);
    // 画出容器内所有子弹
    for (int i = 0; i < shellList.size(); i++) {
        Shell b = shellList.get(i);
        b.draw(g);
        // 0.8版本增加碰撞检测
        // 飞机和所有炮弹对象进行矩形检测
        boolean peng = b.getRect().intersects(plane.getRect());
        if (peng) {
            plane.live = false;
            endTime = new Date();
            if (bao == null) {
                bao = new Explode(plane.x, plane.y);
            }
            bao.draw(g);
        }
    }
    if (!plane.live) {
        if (endTime == null) {
            endTime = new Date();
        }
        int period = (int) ((endTime.getTime() -startTime.getTime()) / 1000);
        printInfo(g, "时间:" + period + "秒", 50, 120, 260, Color.white);
    }
}

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 = this.createImage(500, 500);// 游戏窗口的宽度和高度
    Graphics goff = offScreenImage.getGraphics();
    paint(goff);
    g.drawImage(offScreenImage, 0, 0, null);

}

// 0.4版本 增加内部类 线程类
class PaintThread extends Thread {
    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(40);// 1s=1000
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

// 增加键盘监听内部类
class KeyMonitor extends KeyAdapter {
    public void keyPressed(KeyEvent e) {
        plane.addDirection(e);
    }

    public void keyReleased(KeyEvent e) {
        plane.minusDirection(e);
    }
}

public static void main(String[] args) {
    MyGameFrame f = new MyGameFrame();
    f.launchFrame();

}

}

package cn.sxt.game;

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

public class Plane extends GameObject {

    boolean left, up, right, down;
    boolean live=true;

    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;

        }
    }

    // 松开上下左右建,则改变方向值
    // 例如,松开上建,则e.getKeyCode()的值就是VK_up 那么置up=false
    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;
        }
    }

    public void drawMyself(Graphics g) {
        if (live) {
            super.drawMySelf(g);
            if (left) {
                x -= speed;
            }
            if (right) {
                x += speed;
            }
            if (up) {
                y -= speed;
            }
            if (down) {
                y += speed;
            }
        }

    }

    public Plane(Image img, double x, double y, int speed) {
        super(img, x, y);
        this.speed = speed;
    }
}
package cn.sxt.game;

import java.awt.Color;
import java.awt.Graphics;

public class Shell extends GameObject {
    double degree;
    public Shell() {
        degree=Math.random()*Math.PI*2;
        x=200;
        y=200;
        width=10;
        height=10;
        speed=3;
        
    }
    public void draw(Graphics g) {
        //将外部传入对象g的状态保存好
        Color c=g.getColor();
        g.setColor(Color.red);
        g.fillOval((int)x, (int)y, width, height);
        //炮弹沿任意角度飞行
        x+=speed*Math.cos(degree);
        y+=speed*Math.sin(degree);
        //如下代码用来实现碰到边界,炮弹反弹回来的效果
        if(y>500-height||y<30) {
            degree=-degree;
        }
        if(x<0||x>500-width) {
            degree=Math.PI-degree;
        }
        g.setColor(c);
    }

}

你可能感兴趣的:(2019-06-06 java飞机游戏项目)