使用java编写的小游戏:雷霆战机

       

效果如图用java swing编写的雷霆战机小游戏

其中使用到的技术有: 1.线程 2.swing 画面的重画技术 3.集合等 都是JavaSE中最基本的

玩法是:按空格发射子弹 当子弹碰到敌方飞机时会消灭敌方飞机 我方飞机能用方向键自由移动  但不能碰到敌机 碰到敌机立刻死亡 

            若碰到敌方敌机发射的子弹则会扣100血        扣完就GG


虽然是一个简单的小游戏   但也有很多地方要注意  .

废话不多说 贴上源代码

游戏物体的夫类:

package com.planegame.util;

import java.awt.*;

/***
 *
 * 创建游戏物体
 *
 *
 *
 */

public class GameObject {
    Image image;
    protected double speed,x,y;//定义物体的坐标和速度
    protected int heigth,width;//图像的长宽

public  GameObject(){

}
    public  GameObject(Image image,double speed, double x,double y,int heigth,int width){

        this.image = image;
        this.speed= speed;
        this.x=x;
        this.y=y;
        this.heigth=heigth;
        this.width=width;

    }


    public  void graw(Graphics g){

        g.drawImage(image,(int)x,(int)y,null);


    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public int getHeigth() {
        return heigth;
    }

    public void setHeigth(int heigth) {
        this.heigth = heigth;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * 包围物体的正方形
     *
     * @return 正方形
     */
    public Rectangle getRectangle(){

        return new Rectangle(width,heigth);

    }

}


游戏画面的夫类:

package com.planegame.main;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.swing.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import static com.planegame.util.Constant.Frame_Width;
import static com.planegame.util.Constant.Frame_heigth;

public class GameFrame extends JFrame {
    public static  String path = System.getProperty("user.dir")+"\\src\\Image";
    public  static HashMap<String,BufferedImage> map= new HashMap<String , BufferedImage>();
    static {
        File file[]= new File(path).listFiles();
        for(int i =0;i<file.length;i++){
            System.out.println(file[i].getName());

            try {
                map.put(file[i].getName(), ImageIO.read(file[i]));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }


    public  void launchFrame(){
        this.setSize(Frame_Width,Frame_heigth);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        System.out.println(path);


    }

    public void paint(Graphics g ){



    }

    public static void main(String[] args) {
        new GameFrame().launchFrame();
    }





}
主界面:
package com.planegame.main;

import com.planegame.util.*;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;

public class FirstStage extends  GameFrame implements KeyListener{
    boolean hit_ememy;
    boolean hit;
    boolean hit_together=false;
    Point point = new Point(0,-646);
    Plane plane = new Plane(map.get("plane.jpg"),1,300,400,44,30);//建立自己的飞机类 传入参数
    ScorePlate scorePlate = new ScorePlate();
    Hp hp= new Hp();
    int re=0;
    int flag=0;


    //Ememy_Plane ememy_plane = new Ememy_Plane(map.get("small_enemy.png"),1000,300,50,50,50);
    ArrayList<Bullet> bullets = new ArrayList<Bullet>();//建立一个存放随机射子弹的list
    ArrayList<MyBullets> mybulletelist = new ArrayList<>();//创建list存放我们自己发射的子弹
    ArrayList<Point> ememy_bulletelist = new ArrayList<>();
    ArrayList<Ememy_Plane> ememy_planes = new ArrayList<>();//创建list存放敌方飞机
    //Bullet bullet = new Bullet(map.get("bullet.png"),5,50,100,20,50);
   // Bullet bullet1 = new Bullet(map.get("bullet.png"),5,500,100,20,50);

    /**
     * 构造一个无参的构造函数
     *
     */
   public  FirstStage(){
       //

       new Thread(new move()).start();//背景移动和子弹飞行的线程
      new Thread(new EmemyBullet()).start();//把bullet放入和敌方飞机移动list的线程
      new Thread(new Ememy_plane()).start();//敌方飞机创建放入list的线程
       new Thread(new ememy_move()).start();
       new Thread(new add_bigememy() ).start();

       this.addKeyListener(this);//键盘监听

          // bullets.add(bullet);
     //  bullets.add(bullet1);












   }





    @Override
    /**
     * 把画面画在窗口
     */
    public void paint(Graphics g) {
       super.paint(g);
       //创建一个画板 参数:宽800 高600  画板颜色类型
      BufferedImage image = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
       Graphics gs= image.createGraphics();//创建画笔
     gs.drawImage(map.get("Background-3.jpg"),point.x,point.y,this);//画背景
        gs.drawString("分数:",600,200);
        gs.drawString(String.valueOf(scorePlate.getScore()) ,640,200);
        gs.drawString("生命值:",600,300);
        gs.drawString(String.valueOf(hp.getHp()) ,640,300);
        if(hp.getHp()==0){
            plane.setAlive(false);

        }
     if(plane.isAlive()) {
         gs.drawImage(map.get("red_plane.png") , (int) plane.getX() , (int) plane.getY() , this);

     }
     else {
         plane.setWidth(0);
         plane.setWidth(0);
         Color c = g.getColor();



         gs.setColor(Color.WHITE);

         //plane.move();
         gs.setFont(new Font("aa",Font.LAYOUT_RIGHT_TO_LEFT,60));
         gs.drawString("Game Over",270,300);
         gs.setColor(c);



     } for (int i=0; i < ememy_planes.size(); i++) {
            if (ememy_planes.get(i).isLive()) {


                 int y=(int) ememy_planes.get(i).getY()+50;
                 ememy_bulletelist.add(new Point((int)ememy_planes.get(i).getX(),y));

                gs.drawImage(ememy_planes.get(i).getImage() , (int) ememy_planes.get(i).getX() , (int) ememy_planes.get(i).getY() , this);



                hit_together = ememy_planes.get(i).getRectangle().intersects(plane.getRectangle());
                if (hit_together) {
                    System.out.println("hit_together");
                    plane.setAlive(false);

                }
            }
        }




      for(int j =0;j<mybulletelist.size();j++){
         for(int q =0;q<ememy_planes.size();q++){

             hit_ememy=ememy_planes.get(q).getRectangle().intersects(mybulletelist.get(j).getRectangle());
             if(hit_ememy){
                 ememy_planes.get(q).dispear();
                 ememy_planes.get(q).setLive(false);

                 scorePlate.addScore();

                 //ememy_plane.setLive(false);
             }
         }

         gs.drawImage(map.get("red_bullet.png"),mybulletelist.get(j).x,mybulletelist.get(j).y,this);

      //  hit_ememy=ememy_planes.get(i).getRectangle().intersects(mybulletelist.get(i).getRectangle());


      }


        for(int i =0;i<bullets.size();i++){
            gs.drawImage(map.get("bullet.png"), (int) bullets.get(i).getX() ,(int)bullets.get(i).getY(),this);
            bullets.get(i).move();
            hit= plane.getRectangle().intersects(bullets.get(i).getRectangle());
            if(hit){
                bullets.get(i).setHeigth(0);
                bullets.get(i).setWidth(0);

                hp.decreaseHp();

            }


        }






     g.drawImage(image,0,0,this);





    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

       if(e.getKeyCode()==KeyEvent.VK_UP){
           plane.setTop(true);
           plane.move();


       }
       else  if (e.getKeyCode()==KeyEvent.VK_SPACE){
           if(plane.isAlive()){
           plane.fire(map.get("red_bullet.png"),(int) plane.getX() , (int) plane.getY() ,mybulletelist);}



       }
     else  if (e.getKeyCode()==KeyEvent.VK_RIGHT){

       plane.setRight(true);
           plane.move();
    }
       else if(e.getKeyCode()==KeyEvent.VK_LEFT){
     plane.setLeft(true);
           plane.move();

       }
       else if(e.getKeyCode()==KeyEvent.VK_DOWN){
           plane.setDown(true);
           plane.move();


       }
    }


    @Override
    public void keyReleased(KeyEvent e) {

    }
    class EmemyBullet implements  Runnable{

        @Override
        public void run() {
            while (true){
               int x_random= (int) ((int)1000*Math.random());

            bullets.add(new Bullet(map.get("bullet.png"),5,x_random,100,48,44));

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

    }}
    class Ememy_plane implements Runnable{

        @Override
        public void run() {
            while (true){
                int ememy_plane_x= (int) (Math.random()*800);
                int rand= (int)(Math.random()*10);


                if(rand>5){
                ememy_planes.add(new Ememy_Plane(map.get("small_enemy.png"),1000,ememy_plane_x,50,50,50));}
                else {
                    ememy_planes.add(new Ememy_Plane(map.get("middle_enemy.png"),1000,ememy_plane_x,50,50,50));


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

        }
    }

    class move implements  Runnable{


        @Override
        public void run() {
            while (true){
                if(point.y==0){

                    point.y=-646;
                }


                point.y+=1;
                for(int i =0;i<mybulletelist.size();i++){
                    mybulletelist.get(i).y-=5;

                }







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


            }

        }
    }
        class add_bigememy implements  Runnable{


            @Override
            public void run() {
                while (true){
                int ememy_plane_x = (int)(Math.random()*800);
                if(scorePlate.getScore()>100){
                    ememy_planes.add(new Ememy_Plane(map.get("big_enemy.png"),1000,ememy_plane_x,50,50,50));
                    ememy_bulletelist.add(new Point(ememy_plane_x,50));



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

            }
        }

    class  ememy_move implements  Runnable{


        @Override
        public void run() {
            while (true){

                for(int i=0;i<ememy_planes.size();i++){
                    ememy_planes.get(i).move();}

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

    }




        }
敌方子弹:

package com.planegame.util;

import java.awt.*;

import static com.planegame.util.Constant.Frame_Width;
import static com.planegame.util.Constant.Frame_heigth;

/**
 *
 * 创建游戏中飞行的子弹
 *
 */

public class Bullet  extends GameObject{
    double x,y,speed;

    @Override
    public Rectangle getRectangle() {
        return  new Rectangle((int)x,(int)y,width,heigth);
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeigth() {
        return heigth;
    }

    public void setHeigth(int heigth) {
        this.heigth = heigth;
    }

    private  double  degree;
    public Bullet(Image image, double speed, double x, double y, int heigth, int width) {


        this.speed=speed;//子弹的运行速度speed 其实是半径
        this.x=x;//子弹飞行的x轴初始坐标
        this.y=y;//子弹飞行的y轴初始坐标
        this.image= image;//子弹图像
        this.heigth=heigth;//子弹宽度
        this.width=width;//子弹高度
         this.degree=Math.PI*2*Math.random();//子弹飞行的角度——随机 由0.01*π到π这区间内


    }


    public void graw(Graphics g){
        g.drawImage(image,(int)x,(int)y,null);//调用Graphics的方法 在窗口中画图像
        move();




    }
    public void  move(){
   this.x+= speed*Math.cos(degree); // x= x+半径*cos随机角度
   this.y+=speed*Math.sin(degree);




    }


}
游戏常量:

package com.planegame.util;

import java.awt.*;

public class Constant {

    public  static  int Frame_Width=800;//窗口宽度
    public  static  int Frame_heigth=600;//窗口高度


}
敌方飞机:

package com.planegame.util;

import java.awt.*;
import java.util.ArrayList;

import static com.planegame.util.Constant.Frame_Width;
import static com.planegame.util.Constant.Frame_heigth;

/**
 * @Author 7aY
 * @Description: TODO()
 * @Date :Create in 21:262017/12/20
 */
public class Ememy_Plane extends GameObject {

     Image image;
    double speed;
    double x;
    double y;
    int heigth;
    int width;
   boolean live=true;

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public int getHeigth() {
        return heigth;
    }

    public void setHeigth(int heigth) {
        this.heigth = heigth;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public Ememy_Plane() {
        super();
    }

    public Ememy_Plane(Image image , double speed , double x , double y , int heigth , int width) {
      this.image=image;
      this.speed=speed;
      this.x=x;
      this.y=y;
      this.heigth=heigth;
      this.width=width;



    }

    @Override
    public Rectangle getRectangle() {
       return  new Rectangle((int)x,(int)y,width,heigth);
    }
    public  void  dispear(){
        this.x=0;
        this.y=0;
        this.width=0;
        this.heigth=0;

    }

    public  void move(){
        int direction= (int) (Math.random()*10);
        if(direction<2){


            this.x=this.x-4;
            if(this.x<0){
                this.x=20;
            }

        }
        if(direction>4&&direction<7){
            this.y=y+5;
            if(this.y>Frame_heigth){
                this.y=Frame_heigth-30;
            }

        }
        if(direction>7){
            this.y=y+5;
            if(this.y>Frame_heigth){
                this.y=Frame_heigth-30;
            }


        }
        if(direction<4&&direction>2){


            this.x=this.x+4;
            if(this.x>Frame_Width-50){
                this.x=Frame_Width;
            }

        }



    }

   // public  void fire(Image image, double plane_x, double plane_y, ArrayList<Ememy_bullete> list){
       // list.add(new Ememy_bullete(image,100,(int)plane_x,(int)plane_y+100,64,64));


    //}
}
HP;

package com.planegame.util;

/**
 * @Author 7aY
 * @Description: TODO()
 * @Date :Create in 21:192017/12/21
 */
public class Hp {
    int hp=1000;

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    /**
     * 减血量
     */
    public void decreaseHp(){
        setHp(getHp()-100);

    }

}

我方子弹:

package com.planegame.util;

import java.awt.*;

/**
 * @Author 7aY
 * @Description: TODO()
 * @Date :Create in 22:172017/12/20
 */
public class MyBullets extends GameObject {
   public int x;
    public  int y;
    int heigth,width;
    Image image;
    public MyBullets() {
        super();
    }
    public  MyBullets(Image image,int x, int y,int heigth,int width){
        this.x=x;
        this.y=y;
        this.heigth=heigth;
        this.width=width;
         this.image=image;
    }



    @Override
    public Rectangle getRectangle() {
        return  new Rectangle(x,y,width,heigth);
    }
}
我方飞机:

package com.planegame.util;

import java.awt.*;
import java.util.ArrayList;

import static com.planegame.util.Constant.Frame_Width;
import static com.planegame.util.Constant.Frame_heigth;

/**
 *
 * 创建游戏飞机主体
 *
 *
 *
 */
public class Plane extends  GameObject {

    boolean top=false,down,left,right;//判断键盘输入方向
    boolean alive=true;//判断飞机是否被击落
double x;
double y;
int bullet_x;
    int bullet_y;
    int width;
    int heigth;

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeigth() {
        return heigth;
    }

    public void setHeigth(int heigth) {
        this.heigth = heigth;
    }

    @Override
    public Rectangle getRectangle() {
        return  new Rectangle((int)x,(int)y,width,heigth);

    }

    public double getX() {
        return x;
    }

    public void setX(int i , double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public Plane() {
        super();
    }

    public Plane(Image image, double speed, double x, double y, int heigth, int width) {

        this.image = image;
        this.speed= speed;
        this.x=x;
        this.y=y;
        this.heigth=heigth;
        this.width=width;

    }

public void fire(Image image,int plane_x,int plane_y, ArrayList<MyBullets> list){
      list.add(new MyBullets(image,plane_x-10,plane_y-50,64,64));





}

    public void move(){
        if(this.top==true){
            if(this.y<0) {
                this.y=60;

            }
            else {
                this.y -= 10;
                setTop(false);

            }

        }
       else if(this.down==true){
            if(this.y>Frame_heigth-60){
                this.y=Frame_heigth-60;

            }
            else{
            this.y+=10;
            setDown(false);
            }


        }
        else  if(this.left==true){
           if(this.x<0){

               this.x=0;
           }
           else {
               this.x -= 10;
               setLeft(false);
           }

        }
        else  if(this.right==true){

            if(this.x>Frame_Width-100){
                this.x=Frame_Width-100;

            }
            else {
                this.x+=10;
                setRight(false);

            }


        }


    }

    public boolean isTop() {
        return top;
    }

    public void setTop(boolean top) {
        this.top = top;
    }

    public boolean isDown() {
        return down;
    }

    public void setDown(boolean down) {
        this.down = down;
    }

    public boolean isLeft() {
        return left;
    }

    public void setLeft(boolean left) {
        this.left = left;
    }

    public boolean isRight() {
        return right;
    }

    public void setRight(boolean right) {
        this.right = right;
    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }
}
总分数:

package com.planegame.util;

/**
 * @Author 7aY
 * @Description: TODO()
 * @Date :Create in 12:412017/12/21
 */
public class ScorePlate {

    int score=0;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
    public  void addScore(){
        setScore(getScore()+100);


    }
}







相关文章: