GameFame.java

package cn. tx;import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;class GameFrame extends JFrame {HeroPlane heroPlane;//定义子弹的集合Vector<Bullet> bullets = new Vector<>();//敌机集合Vector<EnemyPlane> enemys = new Vector<>();GameFrame  frame;public GameFrame () {frame = this;//创建英雄机heroPlane =new HeroPlane();heroPlane.start();//设置窗体的宽高this.setSize(450, 730);//标题this.setTitle("雷霆战机");this.setResizable(false);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setLocationRelativeTo(null);//窗口可见this.setVisible(true);new Thread(new Runnable() {@Overridepublic void run() {while (true) {repaint();try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}}}).start();//产生敌机的线程new Thread(new Runnable() {//创建随机数的对象Random r = new Random();@Overridepublic void run() {while (true){//启动敌机EnemyPlane enemyPlane = new EnemyPlane(r.nextInt(500), 0, frame);enemyPlane.start();//添加敌机的时候,让x轴随机enemys.add(enemyPlane);try{Thread.sleep(500);} catch (InterruptedException e){e.printStackTrace();}}}}).start();}//        *在窗口上画,内容,paint这 个画笔的方法在窗口初始化的时候会默认的执行
//        @param gpublic void paint (Graphics g) {//System.out.println("绘制画板");//两背景BufferedImage image = (BufferedImage) this.createImage(this.getSize().width, this.getSize().height);//高效缓存的画笔Graphics bi = image.getGraphics();//画背景bi.drawImage(new ImageIcon("img/MAP02_01.png").getImage(),0,0,null);//画战斗机bi.drawImage(heroPlane.img, heroPlane.x,heroPlane.y, heroPlane.width,heroPlane.heigth,null);//飞机发射炮弹for (int i = 0; i < bullets.size(); i++) {System.out.println(bullets);Bullet bullet = bullets.get(i);if(bullet.y > 0)bi.drawImage(bullet.image, bullet.x,bullet.y -= bullet.speed, bullet.width,bullet.height, null);elsebullets.remove(bullet);}//画敌机for (int i = 0; i < enemys.size(); i++) {System.out.println(enemys);EnemyPlane ep = enemys.get(i);if(ep.y < 730 )bi.drawImage(ep.img, ep.x,ep.y += ep.speed, ep.width,ep.heigth,null);elseenemys.remove(ep);}//生效g.drawImage(image,0,0,null);}public static void main (String[]args){GameFrame frame = new GameFrame();Player player = new Player(frame);frame.addKeyListener(player);}}

HeroPlane

package cn.tx;
import javax.swing.*;
import java.awt.*;public class HeroPlane extends Thread{//英雄机在画板上的位置int x=200, y=600;int width = 50, heigth = 50;//飞机的速度int speed = 10;Image img = new ImageIcon("img/10011.png").getImage();//定义方向键的标志boolean up,down,left,right;public HeroPlane() {}public HeroPlane(int x, int y, int width, int heigth, Image img) {this.x = x;this.y = y;this.width = width;this.heigth = heigth;}@Overridepublic void run() {while (true){if (up){y -= speed;}if (down){y += speed;}if (left){x -= speed;}if (right){x += speed;}try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}}
}

Player

package cn.tx;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//定义一个玩家
public class Player extends KeyAdapter {GameFrame frame;HeroPlane heroPlane;public Player(GameFrame frame) {this.frame=frame;}public void keyPressed(KeyEvent e) {int keyCode = e.getKeyCode();//38、40、37、39switch (keyCode){case 38:frame.heroPlane.up = true;break;case 40:frame. heroPlane.down = true;break;case 37:frame. heroPlane.left = true;break;case 39:frame. heroPlane.right = true;break;case 66:addBullut();break;}}@Overridepublic void keyReleased(KeyEvent e) {int keyCode = e.getKeyCode();//38、40、37、39switch (keyCode){case 38:frame.heroPlane.up = false;break;case 40:frame. heroPlane.down = false;break;case 37:frame.  heroPlane.left = false;break;case 39:frame. heroPlane.right = false;break;}}public void addBullut(){frame.bullets.add(new Bullet( frame.heroPlane.x+5,  frame.heroPlane.y - 20));}
}

EnemyPlane

package cn.tx;import javax.swing.*;
import java.awt.*;public class EnemyPlane extends Thread {public GameFrame gf;//子弹的坐标,大小速度public int x, y;public int width = 50;public int heigth = 50;public int speed = 2;public Image img = new ImageIcon("img/10021.png").getImage();public EnemyPlane(int x, int y, GameFrame gf) {super();this.x = x;this.y = y;this.gf = gf;}public EnemyPlane(int x, int y, int width, int heigth, GameFrame gf) {super();this.x = x;this.y = y;this.width = width;this.heigth = heigth;this.gf = gf;}//玛丽飞翔的逻辑;移动的逻辑都在这里。public void run() {while (true) {//向左走if (hit()) {System.out.println("hit................");this.speed = 0;this.img = new ImageIcon("img/300350.png").getImage();try {this.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}gf.enemys.remove(this);break;}if (this.y >= 760) {break;}try {this.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}}//检测碰撞public boolean hit() {// swing在水中,人家已经提供了Rectangle myrect = new Rectangle(this.x, this.y, this.width, this.heigth);Rectangle rect = null;for (int i = 0; 1 < gf.bullets.size(); i++) {Bullet bullet = gf.bullets.get(i);System.out.println("test hit");rect = new Rectangle(bullet.x, bullet.y - 1, bullet.width, bullet.height);//碰撞检测if (myrect.intersects(rect)) {return true;}}return false;}@Overridepublic String toString() {return "EnemyPlane{" +"x=" + x +", y=" + y +", width=" + width +", height=" + heigth +'}';}
}

Bullet

package cn.tx;
import javax.swing.*;
import java.awt.*;public class Bullet {//在面板上的坐标int x, y;int width= 50,height = 50;//定义飞机默认速度int speed = 5;Image image = new ImageIcon("img/30022.png").getImage();public Bullet(int x, int y) {this.x = x;this.y = y;}public Bullet(int x, int y, int width, int height) {this.x = x;this.y = y;this.width = width;this.height = height;}
}

java练手小项目雷霆战机相关推荐

  1. Java练手小项目——BMI计算器

    最近有一个减肥的朋友想要知道自己的BMI是多少,问我能不能给他做一个计算BMI的工具(无中生友).于是用Java简单写了一个.一起来看看吧. 身体质量指数是BMI指数(身体质量指数,简称体质指数),是 ...

  2. 练手小项目(2)-生活小助手--星座运势查询

    上一篇内容 练手小项目(2)-生活小助手 今天星期一.趁着中午的歇息时间把 第二个写出来 星座运势,近期看看极客学院 用聚合数据做了天气预报的视频教程,不好评价他.看他在后面的代码变更那么大,我就知道 ...

  3. 练手小项目(5)安全卫士_程序锁

    最近想做的小新工具箱,一直想做一个程序锁,其实原理,很简单,先注册一个服务,检测手机所有进程,如果发现被加锁的app启动,马上弹出一个输入程序锁界面,但是这样子bug很多.我先做一个基本后面慢慢把bu ...

  4. springboot+vue练手小项目[前台搭建+后台编写](非常详细)

    [ springboot+vue练手小项目 ] 技术栈: springboot+vue3+element-plus +Mybaties-plus+hutool +mysql8 项目介绍 :最近刚学了s ...

  5. ssm练手小项目_20 个 JavaScript+Html+CSS 练手的小项目

    前言: 最近在 GitHub 上发现了一个 vanillawebprojects[1] 开源仓库,里面收集了 20 个 JavaScript+Html+CSS的练手项目,没有使用任何框架,可以让你从基 ...

  6. 台式小风扇(HTML+CSS+JS练手小项目)

    台式小风扇(HTML+CSS+JS练手小项目) 功能介绍 外观展示 HTML代码 CSS代码 JS代码 总结 功能介绍 前段时间看到这样的风扇特效,感觉还挺好玩,就自己也写一个练练手. 风扇有四个档位 ...

  7. 爬虫练手小项目:豆瓣高分图书TOP100

    爬虫练手小项目:豆瓣高分图书TOP100 import requests import re from requests.exceptions import RequestException impo ...

  8. 数据结构练手小项目(AVL树、哈希表、循环链表、MySQL数据库)

    文章目录 前言 正文(无删减) 我的想法(删减修改版) 数据导入与数据存储 功能实现 数据结构 用户结构 SIM卡结构 AVL树数据结构 哈希表结构 数据表 用户表 SIM卡表 时间安排 前言 本月主 ...

  9. html+css+js之20个练手小项目(一)

    html+css+js之20个练手小项目(一)--Hangman 前言 一.HTML 二.CSS 三.JS 前言 前端新手练习,记录不迷失. 主要练习html和CSS布局以及JS. 来源github, ...

  10. 练手小项目,爬取3DM图片

    博客原文:https://weweweha.com 1. 概述 ​ 爬取3DM指定网页的游戏壁纸,并且通过多线程来加速爬取图片的速度. 2.使用库 ​ request库用来1解析指定网页,re库用来搜 ...

最新文章

  1. 数据分析IJCAI 2020:录用率12.6%,华人占据半壁江山,表征学习、GNN成热点 | AI日报...
  2. docker java镜像_Docker JDK镜像
  3. EF 学习 实用脚本
  4. createbitmap导致的内存泄漏如何处理_C++ 如何避免内存泄漏,一篇就够
  5. RabbitMQ 处理过慢,原来是一个 SQL 缓存框架导致的 GC 频繁触发
  6. SQL分组取每组前一(或几)条记录(排名)
  7. Linux 命令 之查看程序占用内存
  8. Redis再入门 codis 对比 Memcached
  9. github 代理_GitHub访问提速方法
  10. wr885n虚拟服务器设置,动态IP设置:选择动态IP(以太网宽带
  11. mac下cocos2dx(带jsoncpp第三方库)编译为android项目心得
  12. HTML5的File API
  13. Kali linux 全部版本镜像下载
  14. XRD进行定性分析时可以得到哪些有用信息
  15. Python3 爬虫教程 - 新兴网页解析利器 parsel
  16. XML语言的基本语法-Java Web
  17. 【前沿技术RPA】 一文学会用UiPath实现自动发送电子邮件(Email Automation)
  18. 看了这篇Docker指令详解,网友直呼:我收藏了你呢?
  19. 抖音测试的软件,抖音app测试版
  20. vlookup使用步骤_vlookup函数怎么使用_vlookup函数的使用方法及实例 - 系统家园

热门文章

  1. 字符串的交叉合并c语言,C语言 两字符串的合并
  2. [C++刷题笔记]——区间分解质数
  3. 打印机的系统是linux吗,linux下打印机的配置和使用
  4. WordPress 安装时常见的数据库的错误
  5. windows查看系统运行时间和cmd命令大全
  6. Revealing the predictability of intrinsic structure in complex networks
  7. git 添加远程仓库并将第一次本地库的已有所有内容推送到远程库上遇到的一个错误...
  8. [漏洞挖掘]SRC-泛微OA文件上传
  9. 前后端通信:WebSocket之实时监控
  10. java calendar时间计算_Java Calendar计算所在时区的时间偏移量