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

关于java飞机躲炮弹的一些对象说明(带源码)的更多相关文章

  1. 点菜网---Java开源生鲜电商平台-系统架构图(源码可下载)

    点菜网---Java开源生鲜电商平台-系统架构图(源码可下载) 1.点菜网-生鲜电商平台的价值与定位. 生鲜电商平台是一家致力于打造全国餐饮行业智能化.便利化.平台化与透明化服务的创新型移动互联网平台 ...

  2. Java中的容器(集合)之HashMap源码解析

    1.HashMap源码解析(JDK8) 基础原理: 对比上一篇<Java中的容器(集合)之ArrayList源码解析>而言,本篇只解析HashMap常用的核心方法的源码. HashMap是 ...

  3. Java并发包中Semaphore的工作原理、源码分析及使用示例

    1. 信号量Semaphore的介绍 我们以一个停车场运作为例来说明信号量的作用.假设停车场只有三个车位,一开始三个车位都是空的.这时如果同时来了三辆车,看门人允许其中它们进入进入,然后放下车拦.以后 ...

  4. 转!!Java学习之自动装箱和自动拆箱源码分析

    自动装箱(boxing)和自动拆箱(unboxing)   首先了解下Java的四类八种基本数据类型   基本类型 占用空间(Byte) 表示范围 包装器类型 boolean 1/8 true|fal ...

  5. java 动态代理深度学习(Proxy,InvocationHandler),含$Proxy0源码

    java 动态代理深度学习, 一.相关类及其方法: java.lang.reflect.Proxy,Proxy 提供用于创建动态代理类和实例的静态方法.newProxyInstance()返回一个指定 ...

  6. Java显式锁学习总结之六:Condition源码分析

    概述 先来回顾一下java中的等待/通知机制 我们有时会遇到这样的场景:线程A执行到某个点的时候,因为某个条件condition不满足,需要线程A暂停:等到线程B修改了条件condition,使con ...

  7. 【Java并发编程】21、线程池ThreadPoolExecutor源码解析

    一.前言 JUC这部分还有线程池这一块没有分析,需要抓紧时间分析,下面开始ThreadPoolExecutor,其是线程池的基础,分析完了这个类会简化之后的分析,线程池可以解决两个不同问题:由于减少了 ...

  8. 【转】java comparator 升序、降序、倒序从源码角度理解

    原文链接:https://blog.csdn.net/u013066244/article/details/78997869 环境jdk:1.7+ 前言之前我写过关于comparator的理解,但是都 ...

  9. java并发编程的艺术(三)---lock源码

    本文来源于翁舒航的博客,点击即可跳转原文观看!!!(被转载或者拷贝走的内容可能缺失图片.视频等原文的内容) 若网站将链接屏蔽,可直接拷贝原文链接到地址栏跳转观看,原文链接:https://www.cn ...

随机推荐

  1. 再说Java集合,subList之于ArrayList

    上一章说了很多ArrayList相关的内容,但还有一块儿内容没说到,那就是subList方法.先看一段代码 public static void testSubList() { List<Str ...

  2. CentOS7.x mini安装OVS

    命令均在root用户下运行: 一.关闭防护墙及selinux sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config setenfor ...

  3. vuex分模块后,如何获取state的值

    问题:vuex分模块后,一个模块如何拿到其他模块的state值,调其他模块的方法? 思路:1.通过命名空间取值--this.$store.state.car.list // OK 2.通过定义该属性的 ...

  4. redis安装与php安装redis模块

    一.安装redis 1.下载 wget https://github.com/antirez/redis/archive/2.8.23.tar.gz 2.解压缩 tar -zxvf 2.8.23.ta ...

  5. php中\r \r\n \t的区别

    \n 软回车:      在Windows 中表示换行且回到下一行的最开始位置.相当于Mac OS 里的 \r 的效果.      在Linux.unix 中只表示换行,但不会回到下一行的开始位置. ...

  6. 并发编程-concurrent指南-ReadWriteLock-ReentrantReadWriteLock(可重入读写锁)

    几个线程都申请读锁,都能获取: import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantRea ...

  7. scrapy基础知识之 关于爬虫部分一些建议:

    1.尽量减少请求次数,能抓列表页就不抓详情页,减轻服务器压力,程序员都是混口饭吃不容易. 2.不要只看 Web 网站,还有手机 App 和 H5,这样的反爬虫措施一般比较少. 3.实际应用时候,一般防 ...

  8. Bzoj 2525 [Poi2011]Dynamite

    2525: [Poi2011]Dynamite Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 240  Solved: 120[Submit][Sta ...

  9. 对于Typora(markdown)的基本使用

    对于刚开始使用该软件,应该在熟悉基本的markdown语法的基础上,再进行快捷键的使用! 标题 (快捷键:ctrl + 数字) 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 ###### ...

  10. MyBatis从入门到精通(2):MyBatis XML方式的基本用法

    本章将通过完成权限管理的常见业务来学习 MyBatis XML方式的基本用法 2.1一个简单的权限控制需求 权限管理的需求: 一个用户拥有若干角色,一个角色拥有若干权限,权限就是对某个模块资源的某种操 ...