Java如何实现飞机小游戏
这篇文章主要讲解了“Java如何实现飞机小游戏”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java如何实现飞机小游戏”吧!
1,Constant;专门放常量
package com.game2; //专门放常量的一个类 public class Constant { public static final int GAME_WIDTH=500;//窗口的宽度 public static final int GAME_HEIDHT=500;//窗口的高度 public static final int GAME_X=300; public static final int GAME_Y=300; }
2,GameObject;工具父类
package com.game2; //写工具父类 import java.awt.*; public class GameObject { //声明6个变量 Image img; double x,y; int speed; int width,height; //重写构造方法 public void drawSelf(Graphics g){ g.drawImage(img,(int)x,(int)y,null); } //6个变量的构造方法 public GameObject(Image img, double x, double 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, double x, double y) { super(); this.img = img; this.x = x; this.y = y; } //写空的方法 public GameObject(){ } /* 返回物体所在的矩形,便于后续的碰撞检测 */ public Rectangle getRect(){ return new Rectangle((int)x,(int)y,width,height); } }
3,GameUtil;工具类
package com.game2; //工具类 import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; public class GameUtil { //工具类私有化 private GameUtil(){ } public static Image getImage(String path){ BufferedImage bi = null; try{ URL u = GameUtil.class.getClassLoader().getResource(path); bi = ImageIO.read(u); }catch(IOException e){ e.printStackTrace(); } return bi; } }
4,Explode;爆炸
package com.game2; //爆炸类 import java.awt.*; public class Explode { //爆炸位置 double x,y; public Explode(double x,double y){ this.x = x; this.y = y; } //设置对象数组 static Image[] imgs =new Image[12]; static { //图片循环 for(int i= 0;i<12;i++){ imgs[i] = GameUtil.getImage("com/game2/images/a"+(i+1)+".png"); imgs[i].getWidth(null); } } //图片计数轮播 int count; public void draw(Graphics g){ if(count <= 11){ g.drawImage(imgs[count],(int)x,(int)y,null ); count++; } } }
5,Plane;飞机类
package com.game2; //飞机类 import java.awt.*; import java.awt.event.KeyEvent; public class Plane extends GameObject { //增加方向 boolean left,up,right,down; boolean live = true; public void drawSelf(Graphics g){ if(live){ g.drawImage(img,(int)x,(int)y,null); if(left){ x -=speed; } if(right) { x +=speed; } if(up){ y -=speed; } if(down){ y +=speed; } }else{ } } public Plane(Image img, double x, double y){ this.img = img; this.x = x; this.y = y; this.speed =5; this.width =30; this.height =40; } //按下键盘的某个键,增加相应的方向 public void addDirection(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_UP: up = true; break; case KeyEvent.VK_DOWN: down = true; break; case KeyEvent.VK_LEFT: left = true; break; case KeyEvent.VK_RIGHT: right = true; break; } } //按下键盘的某个键,取消相应的方向 public void minusDirection(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_UP: up = false; break; case KeyEvent.VK_DOWN: down = false; break; case KeyEvent.VK_LEFT: left = false; break; case KeyEvent.VK_RIGHT: right = false; break; } } }
6,Shell;炮弹类
package com.game2; //炮弹类 import java.awt.*; public class Shell extends GameObject { double degree; public Shell(){ x=200; y=200; width = 5; height = 5; speed = 2; degree = Math.random()*Math.PI *2; } public void draw(Graphics g){ Color c = g.getColor(); g.setColor(Color.green); g.fillOval((int)x,(int)y,width,height); //炮弹沿着任意角度飞 x+= speed*Math.cos(degree); y+= speed*Math.sin(degree); //让炮弹在窗口内反弹 if(x<5||x>Constant.GAME_WIDTH-width-5){ degree = Math.PI-degree; } if(y<30||y>Constant.GAME_HEIDHT-height-5){ degree= -degree; } g.setColor(c); } }
7,image;图片包
8,images;爆炸图片包
9,Plan;主类
package com.game2; //运行类 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 Plan extends Frame { //图片路径 static Image FeiJ = GameUtil.getImage("com\\game2\\image\\11.png"); static Image BeiJ = GameUtil.getImage("com\\game2\\image\\Xk.jpg"); static Image PaoD = GameUtil.getImage("com\\game2\\image\\00.png"); //定义飞机位置 Plane pl = new Plane(FeiJ,250,250);//飞机对象初始化 Shell[] shells = new Shell[50];//多个炮弹数组 Explode bao;// Date startTime = new Date();//开始时间 Date endTime;//结束时间 int period;//游戏持续的时间 //窗口内绘制,自动调用 @Override public void paint(Graphics g) { Color c= g.getColor();//保存原字体颜色 g.drawImage(BeiJ,0,0,null); pl.drawSelf(g);//画飞机 //画出所有炮弹for循环 for(int i= 0;i<shells.length;i++) { shells[i].draw(g); //判断炮弹是否碰撞 boolean peng =shells[i].getRect().intersects(pl.getRect()); if (peng){ pl .live = false;//如果碰撞飞机死亡 if(bao==null){ bao = new Explode(pl.x, pl.y); //计算结束时间 endTime =new Date(); period= (int)((endTime.getTime()-startTime.getTime())/1000); } bao.draw(g);//画出爆炸 } //游戏时间计算 //如果飞机死亡 if(!pl.live){ //改颜色 g.setColor(Color.CYAN); //改字体 Font f = new Font("楷体",Font.BOLD,50); g.setFont(f); //时间计算 g.drawString("时间:"+period+"秒",(int)pl.x,(int)pl.y); } } g.setColor(c);//改回原来字体颜色 } //多线程 class PaintThread extends Thread{ @Override public void run() { //反复画窗口 while(true){ repaint(); //时间间隔 try { Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } } } } //定义键盘监听内部类 class KeyMonitor extends KeyAdapter{ @Override public void keyPressed(KeyEvent e) { pl.addDirection(e); }//按下 @Override public void keyReleased(KeyEvent e) { pl.minusDirection(e); }//释放 } //加入双缓冲,使窗口页面不闪烁 private Image offScreenImage = null; public void update(Graphics g){ if(offScreenImage == null) offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIDHT);//游戏宽度高度 Graphics gOff = offScreenImage.getGraphics(); paint(gOff); g.drawImage(offScreenImage,0,0,null); } /* 初始化窗口 */ public void launchFrame(){ this.setTitle("飞机大战");//窗口标题 this.setVisible(true);//窗口显示可见 this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIDHT);//设置窗口大小 this.setLocation(Constant.GAME_X,Constant.GAME_Y);//设置窗口左上点在电脑屏幕上的位置 this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } });//关闭窗口进程 new PaintThread().start();//启动重画窗口的线程 addKeyListener(new KeyMonitor());//给窗口增加键盘的监听 //初始化生成50个炮弹 for(int i=0;i<shells.length;i++){ shells[i]= new Shell(); } } public static void main(String [] args){ Plan p = new Plan(); p.launchFrame(); } }
运行即可
感谢各位的阅读,以上就是“Java如何实现飞机小游戏”的内容了,经过本文的学习后,相信大家对Java如何实现飞机小游戏这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是蜗牛博客,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论