一:简介
这是一个可以通过键盘移动飞机位置 然后可以躲避 炮弹的打击 ,游戏结束为 当炮弹与飞机发生碰撞 则 最后显示出飞机存活的时长 代表游戏结束
二:上码
1:运行游戏的类
package src.src.cn.wyj.飞机;import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;import javax.swing.JFrame;public class MyGame1 extends Frame{//图像的加载Image bg = GameUti.getImage("src/images/bg.jpeg");Image planeimg = GameUti.getImage("src/images/plane.jpeg");//注意图片的路径Plane plane = new Plane(planeimg,250,250,30,30);Shell shell[] = new Shell[50];Explode bao;Date startTime = new Date();Date endTime;int sustain;//游戏持续 的时间//
// //文本的绘制
// public void paint(Graphics g){//自动被调用, g 相当于 一只画笔super.paint(g);//使窗口的背景颜色为白色//g.drawLine(200,12, 200, 200);//加载图片g.drawImage(bg, 0, 0, Constant.game_width, Constant.game_height, null);//这里可以设置背景图片为窗口大小plane.drawself(g);//画飞机for(int i = 0; i < shell.length; i++){ shell[i].draw(g);//画炮弹//飞机和炮弹的碰撞检测boolean peng = shell[i].getRect().intersects(plane.getRect());if(peng){plane.live = false;if( bao == null){bao = new Explode(plane.x,plane.y);endTime = new Date();sustain = (int)((endTime.getTime() - startTime.getTime())/1000);}bao.draw(g);}if(plane.live == false){g.setColor(Color.blue);Font f = new Font("宋体",Font.BOLD,100);g.setFont(f);g.drawString("时间:"+sustain+"秒",200, 350);}}//g.setColor(c);}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);}//创建一个内部类 内部类的好处 可以调用 MyGame1的属性和方法//本项目中 创建一个内部类 主要是为了帮助我们重画窗口class PaintThread extends Thread{public void run(){while(true){repaint();//重画窗口Graphics g=getGraphics();//System.out.println("王永杰");try {Thread.sleep(40);//每 40 毫秒 加载一次 也就是 1s可以加载25次} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//创建一个内部类 键盘控制游戏class KeyMonitor extends KeyAdapter{@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubsuper.keyPressed(e);plane.addDirection(e);}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stubsuper.keyReleased(e);plane.minusDirection(e);}}//初始化窗口public void launchFrame(){this.setTitle("杰哥出品 必是精品");//定义游戏窗口this.setVisible(true);this.setSize(Constant.game_width,Constant.game_height);this.setBackground(Color.white);//点 叉 终止程序this.addWindowListener(new WindowAdapter(){@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});//重画窗口 new PaintThread().start();//启动线程addKeyListener(new KeyMonitor());//给窗口增加键盘的监听//初始化炮弹for(int i = 0; i < shell.length; i++){shell[i] = new Shell();//当碰撞检测 不输出 “相撞时” 则要去 检查 炮弹类和飞机类当中 的构造函数 是否将 图片的尺寸 传进去了}}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubMyGame1 f = new MyGame1();f.launchFrame();}}
2:游戏物体的父类 (炮弹和飞机方便直接用 体现继承的好处)
package src.src.cn.wyj.飞机;import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;//游戏物体的父类public class GameObject {Image img;int x,y;//图片的坐标int speed;//为设定 每次 键盘控制所以移动的距离int width,height;//图片的尺寸 一定赋值图片的尺寸 public void drawself(Graphics g){g.drawImage(img, x, y, width, height, null);}public GameObject(){}public GameObject(Image img, int x, int 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,int x, int y) {super();this.img = img;this.x = x;this.y = y;}// 碰撞检测 (炮弹和飞机)public Rectangle getRect(){return new Rectangle(x, y, width, height);}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub}}
3:窗口的尺寸设置成常量 这样就不用牵一发而动全身了
package src.src.cn.wyj.飞机;public class Constant {public static final int game_width = 700;public static final int game_height = 700;/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub}}
4:飞机类
package src.src.cn.wyj.飞机;import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;public class Plane extends GameObject {boolean left,up,right,down;boolean live = true;public Plane(Image img, int x, int y,int width,int height) {super(img, x, y);this.img = img;this.x = x;this.y = y;this.speed = 3;this.width = width;this.height = height;// TODO Auto-generated constructor stub}public void drawself(Graphics g){if(live){g.drawImage(img, x, y, width, height, null);if(left)x-=speed;if(up)y-=speed;if(right)x+=speed;if(down)y+=speed;} }//增加某个方向的键值 public void addDirection(KeyEvent e){switch (e.getKeyCode()) {case KeyEvent.VK_LEFT:left = true;break;case KeyEvent.VK_UP:up = true;break;case KeyEvent.VK_RIGHT:right = true;break;case KeyEvent.VK_DOWN:down = true;break;default: break;}}//取消某个方向的键值 public void minusDirection(KeyEvent e){switch (e.getKeyCode()) {case KeyEvent.VK_LEFT:left = false;break;case KeyEvent.VK_UP:up = false;break;case KeyEvent.VK_RIGHT:right = false;break;case KeyEvent.VK_DOWN:down = false;break;default: break;}}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub}}
5:炮弹类
package src.src.cn.wyj.飞机;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;public class Shell extends GameObject{double degree;public Shell() {x = 200;y = 200;width = 10;height = 10;speed = 5;degree = Math.random()*Math.PI*2*x;//设置炮弹飞行的弧度// TODO Auto-generated constructor stub}public void draw(Graphics g){Color c = g.getColor();g.setColor(Color.YELLOW);g.fillOval(x, 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;}g.setColor(c);}public Shell(Image img, int x, int y, int speed, int width, int height) {super(img, x, y, speed, width, height);// TODO Auto-generated constructor stub}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub}}
6:爆炸类
package src.src.cn.wyj.飞机;import java.awt.Graphics;
import java.awt.Image;public class Explode {double x,y;static Image[] imgs = new Image[16];static {for(int i=0;i<16;i++){imgs[i] = GameUti.getImage("src/images/"+(i+1)+".gif");//一定要注意名字与图片一致,否则运行时提示空指针imgs[i].getWidth(null);}}int count;public void draw(Graphics g){if(count<=15){g.drawImage(imgs[count], (int)x, (int)y, null);count++;}}public Explode(double x,double y){this.x = x;this.y = y;}public Explode() {}
}
飞机让炮弹炸没了
三:总结
我用的Eclipse 进行编辑的,这是高琪老师讲课当中的一个小项目,这是对前面所学做了复习,通过项目进行复习。最近晚上常在刷算法题,有时候一道题 得用5个多钟头,昨天可能做了道简单的两个多小时做完了,但躺在床上睡不着,一直努力想让自己入睡 ,并一直告诉自己熬夜会让自己变丑,毕竟我还是要靠脸和技术吃饭的boy! 然后我就又看了知乎上的大佬 编程指北的文章 ,对自己 的学习路线也有点了解,可是更加感叹的是人家从大一就开始对编程有了认识,在大二开始用各种变成语言开始写东西,到大三开始确定方向并深究。发自内心的说一句 真好,我的大二编程还没入门,还在想的怎么跟女朋友谈恋爱,浪费了好多时间,最近前女友又来找我了,给我发了很多信息,但我没回,大三太重要了 太重要了,有时候不得不说自己可能太享受舒适的校园环境了,没有感受到不久就要到来的校招危机,对目前的我来说 那必然是危机,因为我还很菜,还有很多东西得去做,大三上和寒假,我感觉我应该把自己定位在 高三 ,不 ,应该比高三更努力,原来抖音也不是命,戒掉他,确实每天多了很多时间,然后我又把手机QQ给卸载了,因为那里面也有短视频,而且我以亲还喜欢浏览别人发的动态 ,一点用都没有,浪费时间,现在也就每天早上打开电脑时,看看有没有重要的信息,有就回复,没有就立马关掉,其实也不是知道说了点啥!但自己可以明确的是 我还需更加努力!