栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA飞机小游戏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA飞机小游戏

该小游戏使用java语言实现,使用工具idea。

共写9个类

Constant;Explode;GameObject;GameUtil;Plane;Shell;image;images;Plan; 

1,Constant;专门放常量
package com.game2;
//专门放常量的一个类
public class Constant {
    public static final int GAME_WIDTH=500;//窗口的宽度
    public static final int GAME_HEIDHT=500;//窗口的高度
    public static final int GAME_X=300;
    public static final int GAME_Y=300;
}
2,GameObject;工具父类
package com.game2;
//写工具父类
import java.awt.*;

public class GameObject {
    //声明6个变量
    Image img;
    double x,y;
    int speed;
    int width,height;
    //重写构造方法
    public void drawSelf(Graphics g){
        g.drawImage(img,(int)x,(int)y,null);
    }
    //6个变量的构造方法
    public GameObject(Image img, double x, double y, int speed, int width, int height) {
        super();
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.width = width;
        this.height = height;
    }
    //重写构造方法
    public GameObject(Image img, double x, double y) {
        super();
        this.img = img;
        this.x = x;
        this.y = y;
    }
    //写空的方法
    public GameObject(){
    }
    
    public Rectangle getRect(){
        return new Rectangle((int)x,(int)y,width,height);
    }
}
3,GameUtil;工具类
package com.game2;
//工具类
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;


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;
    }
}
4,Explode;爆炸
package com.game2;
//爆炸类
import java.awt.*;

public class Explode {
    //爆炸位置
    double x,y;
    public Explode(double x,double y){
        this.x = x;
        this.y = y;
    }
    //设置对象数组
    static Image[] imgs =new Image[12];
    static {
        //图片循环
        for(int i= 0;i<12;i++){
            imgs[i] = GameUtil.getImage("com/game2/images/a"+(i+1)+".png");
            imgs[i].getWidth(null);
        }
    }
    //图片计数轮播
    int count;
    public void draw(Graphics g){
        if(count <= 11){
            g.drawImage(imgs[count],(int)x,(int)y,null );
            count++;
        }
    }

}
5,Plane;飞机类
package com.game2;
//飞机类
import java.awt.*;
import java.awt.event.KeyEvent;

public class Plane extends GameObject {
    //增加方向
    boolean left,up,right,down;
    boolean live = true;

    public void drawSelf(Graphics g){
        if(live){
            g.drawImage(img,(int)x,(int)y,null);
            if(left){
                x -=speed;
            }
            if(right) {
                x +=speed;
            }
            if(up){
                y -=speed;
            }
            if(down){
                y +=speed;
            }
        }else{

        }



    }
    public Plane(Image img, double x, double y){
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed =5;
        this.width =30;
        this.height =40;
    }
    //按下键盘的某个键,增加相应的方向
    public void addDirection(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_UP:
                up = true;
                break;
            case KeyEvent.VK_DOWN:
                down = true;
                break;
            case KeyEvent.VK_LEFT:
                left = true;
                break;
            case KeyEvent.VK_RIGHT:
                right = true;
                break;
        }
    }
    //按下键盘的某个键,取消相应的方向
    public void minusDirection(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_UP:
                up = false;
                break;
            case KeyEvent.VK_DOWN:
                down = false;
                break;
            case KeyEvent.VK_LEFT:
                left = false;
                break;
            case KeyEvent.VK_RIGHT:
                right = false;
                break;
        }
    }
}
6,Shell;炮弹类
package com.game2;
//炮弹类
import java.awt.*;
public class Shell extends GameObject {
    double degree;

    public Shell(){
        x=200;
        y=200;
        width = 5;
        height = 5;
        speed = 2;
        degree = Math.random()*Math.PI *2;
    }
    public void draw(Graphics g){
        Color c = g.getColor();
        g.setColor(Color.green);
        g.fillOval((int)x,(int)y,width,height);

        //炮弹沿着任意角度飞
        x+= speed*Math.cos(degree);
        y+= speed*Math.sin(degree);

        //让炮弹在窗口内反弹
        if(x<5||x>Constant.GAME_WIDTH-width-5){
            degree = Math.PI-degree;
        }
        if(y<30||y>Constant.GAME_HEIDHT-height-5){
            degree= -degree;
        }
        g.setColor(c);
    }
}
7,image;图片包

 

8,images;爆炸图片包

 

9,Plan;主类
package com.game2;
//运行类
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 Plan extends frame {
    //图片路径
    static Image FeiJ = GameUtil.getImage("com\game2\image\11.png");
    static Image BeiJ = GameUtil.getImage("com\game2\image\xk.jpg");
    static Image PaoD = GameUtil.getImage("com\game2\image\00.png");
    //定义飞机位置
    Plane pl = new Plane(FeiJ,250,250);//飞机对象初始化
    Shell[] shells = new Shell[50];//多个炮弹数组
    Explode bao;//
    Date startTime = new Date();//开始时间
    Date endTime;//结束时间
    int period;//游戏持续的时间


    //窗口内绘制,自动调用
    @Override
    public void paint(Graphics g) {
        Color c= g.getColor();//保存原字体颜色
        g.drawImage(BeiJ,0,0,null);
        pl.drawSelf(g);//画飞机
        //画出所有炮弹for循环
        for(int i= 0;i 
运行即可 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677897.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号