• 关于java飞机躲炮弹的一些对象说明(带源码)


                                1.飞机躲炮弹的各种实体类都需要一个画笔将他们画出来 
                                          (GameObject)

    import java.awt.*;
    public void drawSelf(Graphics g){
    g.drawImage(image,(int)x,(int)y,null);
    }
    //里边用到了Graphics 的这个对象
    //可以把 g 看成一个画笔,将你需要的飞机等类通过一个画笔画出image ,其中的x,y 设置他们的位置(必须是整形)



    public Rectangle getRect(){
    return new Rectangle((int)x, (int) y,width,height);
    }


        //里边用到了Rectangle 的这个对象
    //返回实体类的位置和大小,想当于一个无形的方块跟着某个实体,在他身边筑起了篱笆,不能发生重叠,一碰就炸



    2.图片单建一个类获取并存储
                                             ( GameUtil)
    public class GameUtil {
    public GameUtil() {
    }
    //获取图片位置
    public static Image getImage(String path){
    BufferedImage bi = null;
    URL u = GameUtil.class.getClassLoader().getResource(path);
    try {
    bi = ImageIO.read(u);
    } catch (IOException e) {
    e.printStackTrace();
    }
    return bi;
    }
    }

    //BufferedImage 对象用来存储图片
    //GameUtil.class.getClassLoader().getResource(path)用来获取url,
    其中GameUtil.class是这个类文件;getClassLoader()是启动类加载器用于启动这个类
    getResource()是从根目录获取URL的,其中的path是不能以“”开头的,(并且getResource()很少单独使用,一般与File对象的getfile()连用)



                           3.关于飞机最重要的是能够根据人机交互移动
    plane
    主要和键盘的按键读取对象KeyEvent有关
    例如定义了一个KeyEvent对象e
    e.getKeyCode(KeyEvent.Vk_键盘的按键)是获取键盘的按键 例如:e.getKeyCode(KeyEvent.VK_W)获取w按键

    4.关于炮弹是能够反弹的算法
    shell

    圆周率==180度
    Math.random()得到0.0到1.0的任意值
    (里边的算法自己思考)比较简单
     
    源码:
    Constant
    package com.company.game;

    public class Constant {
    public static final int Game_WIDTH = 500;
    public static final int Game_HEIGHT = 500;
    }

    Explode
    package com.company.game;

    import java.awt.*;

    public class Explode {
    double x,y;
    static Image[] images = new Image[16];
    static {
    for (int i = 0; i < 16 ; i++) {
    images[i] = GameUtil.getImage("src/images/explode/e" + (i + 1) + ".png");
    images[i].getWidth(null);
    }
    }

    int count;

    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;
    }
    }
                    GameObject 
    package com.company.game;

    import java.awt.*;

    public class GameObject {
    //每个物体都有(宽度、长度、位置的X与Y、速度、图片)

    Image image;
    double x,y;
    int height,width;
    int speed;

    public void drawSelf(Graphics g){
    g.drawImage(image,(int)x,(int)y,null);
    }

    public GameObject(Image image, double x, double y, int height, int width, int speed) {
    // super();
    this.image = image;
    this.x = x;
    this.y = y;
    this.height = height;
    this.width = width;
    this.speed = speed;
    }

    public GameObject(Image image, double x, double y) {
    this.image = image;
    this.x = x;
    this.y = y;
    }

    public GameObject() {
    }

    //返回物体所在的矩形,用于检测是否发生碰撞

    public Rectangle getRect(){
    return new Rectangle((int)x, (int) y,width,height);
    }
    }
    
    
                   GameUtil 
    package com.company.game;

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

    public class GameUtil {
    public GameUtil() {
    }
    //获取图片位置
    public static Image getImage(String path){
    BufferedImage bi = null;
    URL u = GameUtil.class.getClassLoader().getResource(path);
    try {
    bi = ImageIO.read(u);
    } catch (IOException e) {
    e.printStackTrace();
    }
    return bi;
    }
    }
                     Plane 
    package com.company.game;

    import java.awt.*;
    import java.awt.event.KeyEvent;

    public class Plane extends GameObject{
    boolean left,right,up,down;
    boolean live = true;

    //确定飞机移动后的方位
    public void drawSelf(Graphics g){
    if (live){
    g.drawImage(image,(int)x,(int)y,null);
    }
    if(left && x>=0){
    x -= speed;
    }
    if(right && x<Constant.Game_WIDTH-width){
    x += speed;
    }
    if(up && y>=20){
    y = y - speed;
    }
    if(down && y<Constant.Game_HEIGHT-height){
    y += speed;
    }

    }

    public Plane(Image image, double x, double y) {
    super(image, x, y);
    this.x = x;
    this.y = y;
    this.speed = 8;
    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_RIGHT:
    right = true;
    break;
    case KeyEvent.VK_UP:
    up = true;
    break;
    case KeyEvent.VK_DOWN:
    down = true;
    break;

    case KeyEvent.VK_A:
    left = true;
    break;
    case KeyEvent.VK_D:
    right = true;
    break;
    case KeyEvent.VK_W:
    up = true;
    break;
    case KeyEvent.VK_S:
    down = true;
    break;
    }
    }


    public void minusDirection(KeyEvent e){
    switch (e.getKeyCode()){
    case KeyEvent.VK_LEFT:
    left = false;
    break;
    case KeyEvent.VK_RIGHT:
    right = false;
    break;
    case KeyEvent.VK_UP:
    up = false;
    break;
    case KeyEvent.VK_DOWN:
    down = false;
    break;

    case KeyEvent.VK_A:
    left = false;
    break;
    case KeyEvent.VK_D:
    right = false;
    break;
    case KeyEvent.VK_W:
    up = false;
    break;
    case KeyEvent.VK_S:
    down = false;
    break;
    }
    }
    }
    
    
                    Shell 
    package com.company.game;

    import java.awt.*;

    public class Shell extends GameObject{
    double degree; //弧度

    public Shell(){
    x = 200;
    y = 200;
    width = 10;
    height = 10;
    speed = 3;

    degree = Math.random()*Math.PI*2;
    }

    public void draw(Graphics g){
    Color c = g.getColor();
    g.setColor(Color.white);

    g.fillOval((int)x,(int)y,width,height);//填充

    x += speed*Math.cos(degree);
    y += speed*Math.sin(degree);

    if (x<0||x>Constant.Game_WIDTH-width){
    degree = Math.PI - degree;
    }
    if (y<0||y>Constant.Game_HEIGHT -height){
    degree = - degree;
    }


    }
    }
                   Main


    package com.company.game;

    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.Date;

    public class Main extends Frame {

    Image planeImg = GameUtil.getImage("images/plane.png");
    Image bg = GameUtil.getImage("images/bg.png");

    Plane plane = new Plane(planeImg,250,Constant.Game_HEIGHT-50);
    Shell[] shells = new Shell[10];
    Explode explode;
    Date startTime = new Date();
    Date endTime;
    double period;

    @Override
    public void paint(Graphics g) {
    super.paintComponents(g);

    Color c = g.getColor();

    g.drawImage(bg,0,0,null);
    plane.drawSelf(g);

    //花50个炮弹
    for (int i = 0; i < shells.length; i++) {
    shells[i].draw(g);
    boolean peng = shells[i].getRect().intersects(plane.getRect());
    if (peng){
    plane.live = false;
    if (explode == null){
    explode = new Explode(plane.x,plane.y);
    endTime = new Date();
    period = (startTime.getTime()-endTime.getTime());
    }
    explode.draw(g);
    }

    if (!plane.live){
    g.setColor(Color.red);
    Font font = new Font("楷体",Font.ITALIC,50);
    g.setFont(font);
    //g.drawString("时间:" + period + "m",(int)plane.x,(int)plane.y);
    plane.live = true;
    plane = new Plane(planeImg,250,Constant.Game_HEIGHT-50);
    plane.drawSelf(g);
    }
    }

    g.setColor(c);
    }


    private void launchFrame() {
    this.setTitle("飞机躲炮弹");
    this.setVisible(true);
    this.setSize(Constant.Game_WIDTH,Constant.Game_HEIGHT);
    this.setLocation(300,150);
    //this.setBackground(Color.black);

    this.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
    super.windowClosing(e);
    System.exit(0);
    }
    });

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

    for (int i = 0; i < shells.length; i++) {
    shells[i] = new Shell();
    }
    }


    class PaintThread extends Thread {
    @Override
    public void run() {
    super.run();
    while (true){
    repaint();

    try {
    Thread.sleep(40);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    class KeyMonitor extends KeyAdapter{
    @Override
    public void keyPressed(KeyEvent e) {
    super.keyPressed(e);
    plane.addDirection(e);
    }

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

    public static void main(String[] args) {
    // write your code here
    Main f = new Main();
    f.launchFrame();
    }

    private Image offScreenImage = null;

    public void update(Graphics g){
    if (offScreenImage == null){
    offScreenImage = this.createImage(Constant.Game_WIDTH,Constant.Game_HEIGHT);
    }
    Graphics gOff = offScreenImage.getGraphics();
    paint(gOff);
    g.drawImage(offScreenImage,0,0,null);
    }
    }



    炸弹





    参考代码:https://blog.csdn.net/weixin_42148490/article/details/82695381
  • 相关阅读:
    POJ 3304 计算几何 直线与线段相交
    POJ 2653 计算几何 判断线段相交
    POJ 1066 计算几何 线段相交
    关于git的一些有用的链接
    Cloudera是个什么东西
    Data Structure for phone book
    向大家推荐一个C/C++通用Makefile
    关于gcc/g++的include路径和其他一些路径问题
    volatile 和 sig_atomic_t
    分布式文件系统虚拟目录及命名空间的实现方法
  • 原文地址:https://www.cnblogs.com/xww115/p/10497490.html
Copyright © 2020-2023  润新知