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

       

效果如图

其中使用到的技术有: 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;/**
 * @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);}
}
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;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.*;public class Constant {public  static  int Frame_Width=800;//窗口宽度
    public  static  int Frame_heigth=600;//窗口高度


}
敌方飞机:
我方子弹:
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.*;
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;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);}}
游戏常量:






用java swing编写的雷霆战机小游戏相关推荐

  1. Java Swing编写的一个猜拳小游戏

    18年在学校时候写的,整理目录发现的,这个游戏当时是模仿一个微信小程序 效果截图: 里面所用到的四张图片放在百度云: 链接:https://pan.baidu.com/s/1pnbcOCDpHzA-h ...

  2. 使用JAVA开发了一个雷霆战机小游戏^_^

    本程序使用Myeclipse开发,编码为UTF-8. 本程序实现的主要功能有玩家飞机的控制.玩家飞机子弹发射.敌机移动.敌机子弹发射.boss飞机的折线运动.boss飞机的子弹发射.玩家飞机和boss ...

  3. 基于Java swing开发的雷霆战机

    游戏截图 飞机项目的所有类的截图 主窗体类 package com.tarena.shout; import java.awt.Graphics; import java.awt.Image; imp ...

  4. jq开发雷霆战机小游戏

    这是我写的第二个jq小游戏了写了一个多星期,挺不容易的,写篇博客记录一下 游戏界面: 英雄机有能量条:能量条的积攒通过打爆敌机和打boss获得,能量条到达20%英雄机的弹孔可以升级为双管炮火弹孔,能量 ...

  5. java写雷霆战机小游戏

    环境:jdk1.8 idea2017.4 效果图 类图 代码 BallJFrame .java import javax.swing.*; public class BallJFrame extend ...

  6. java雷霆战机小游戏(git 素材+代码)

    git地址 https://gitee.com/wuyue111/thunder-fighters-games.git代码 素材地址 附上效果图 代码结构

  7. 用Python做雷霆战机小游戏【附素材+源码】

    前言 嗨嗨,我是小圆 最近很多朋友都在问我要一些游戏的代码 所以这篇文章就出来了 [想一键获取素材和源码的,点击文章末尾名片] 模块准备 import pygame, os import time i ...

  8. 基于Java Swing 的马踏棋盘小游戏(附源码!免费下载!)

    马踏棋盘游戏小项目 设计主要功能 运用的数据结构 运行流程讲解及录像 项目分类截图及源码链接! 设计主要功能 (1)设计内容:设计一个马踏棋盘游戏,马作为棋子,以马走日字的走法,将整个棋盘一次性走完, ...

  9. 用Python做雷霆战机小游戏【赠素材+源码】

    前言 嗨嗨,我是小圆 最近很多朋友都在问我要一些游戏的代码 所以这篇文章就出来了 [想一键获取素材和源码的,点击文章末尾名片] 模块准备 import pygame, os import time i ...

  10. c语言雷霆战机小游戏

    #include<stdio.h> #include<string.h> #include<conio.h> #include<windows.h> # ...

最新文章

  1. 【Android Gradle 插件】ProductFlavor 配置 ( applicationId 配置 | SdkVersion 相关配置 | version 应用版本配置 )
  2. 连贯的学习黑树(插入节点)
  3. 分布式服务常见问题—分布式事务
  4. iOS Appstore 版本更新
  5. jQuery 1.9+ 移除$.browser方法
  6. Netbeans 中创建数据连接池和数据源步骤(及解决无法ping通问题)
  7. CodeWithMosh--mysql 学习笔记(3)
  8. 手把手玩转win8开发系列课程(20)
  9. 低档显卡无法支持2K显示器
  10. ami主板uefi_AMI Aptio V UEFI 主板手动添加Dell Slic2.5表OEM激活win7一例及过程分解
  11. HTML 网页制作 盒子设计 CSS
  12. 计算机编程php网页源码水果网上销售系统mysql数据库web结构html布局
  13. 【备战春招/秋招系列】美团Java面经总结终结篇 (附详解答案) 1
  14. colab 跑 deformable-detr 记录:
  15. java计算里程_通过角速度计算里程(对于两轮机器人)?
  16. Web组态编辑器赋能智慧石油生产储运2D可视化
  17. 国产操作系统逐步强大,Linux操作系统成为主流
  18. android 货币格式化,Android 国际货币格式化的示例代码
  19. 最全FC交换机基础知识详解
  20. 科创板市价申报订单中拟用价格保护措施,买入申报的成交价不高于保护限价...

热门文章

  1. word页眉前后不一致怎么设置_2007word中,如何在某几页设置与前后不同的页眉?
  2. 计算机排名的985大学排名,最新985大学排名榜名单
  3. Confluence 6 后台中的默认空间模板设置
  4. 7-1 打印倒直角三角形图形 (20 分)
  5. PS第十一课--色阶曲线
  6. android隐藏其他应用图标,【Android】隐藏app图标以及隐式启动其他APP
  7. Excel数据分析从入门到精通(十一)28个图表之结构分析
  8. 热烈祝贺 | 方正璞华喜获“数字贸易专委会年度示范单位”殊荣及“终身荣誉会长”单位
  9. SM2证书的鉴定方法
  10. mov和mp4格式哪个好_Mac版dvd格式转换器哪个好用?Mac上最好用的dvd格式转换器推荐...