十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍java如何实现弹幕小游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、做网站、铁西网络推广、成都小程序开发、铁西网络营销、铁西企业策划、铁西品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供铁西建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
具体内容如下
父类
import java.awt.*; public class GameObject { //游戏物体的父类 Image img; double x,y; int speed = 3; int width,height; public void drawSelf(Graphics g){ g.drawImage(img,(int)x,(int)y,null); } public GameObject(Image img, double x, double y, int X_speed,int Y_speed, int width, int height) { 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) { this.img = img; this.x = x; this.y = y; } public GameObject(){ } //返回物体所在的矩形,便于后续的碰撞检测 public Rectangle getRect(){ return new Rectangle((int)x,(int)y,width,height); } }
弹幕类
import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties; import java.awt.*; public class Shell extends GameObject { double degree; public Shell() { x = 200; y = 200; width = 10; height = 10; speed = 2; speed = 2; //弧度 degree = Math.random()*Math.PI*2; } public void draw(Graphics g){ Color c = g.getColor(); g.setColor(Color.YELLOW); 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<40 || y> Constant.GAME_HEIGHT-height){ degree = -degree; } g.setColor(c); } }
飞机类
import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class Plane extends GameObject{ boolean left,right,up,down; boolean live = true; //飞机是否活着 boolean leftflag,rightflag,upflag,downflag;//这些布尔值判断当碰到上下左右的边框时的状态 //如果活着画出来 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; } } } public Plane(Image img,double x,double y) { this.img = img; this.x = x; this.y = y; this.speed = 3; this.width = img.getWidth(null); this.height = img.getHeight(null); } //按下某个键,增加相应的方向 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; } } //按下某个键,取消相应的方向 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; } } }
爆炸类
import java.awt.*; //爆炸类 public class Explode { double x,y; //爆炸的位置 static Image[] imgs = new Image[16]; static { for(int i=0;i<16;i++){ imgs[i] = PlayGameFrame.GameUtil.getImage("image/explode/e"+(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 class Constant { public static final int GAME_WIDTH = 500; public static final int GAME_HEIGHT = 500; }
主类
import jdk.internal.cmm.SystemResourcePressureImpl; import javax.imageio.ImageIO; import javax.swing.*; 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.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import java.awt.Rectangle; import java.util.Date; /** *飞机游戏的主窗口 */ class PlayGameFrame extends Frame { Image planeImg= GameUtil.getImage("image/plane.png"); Image nightsky = GameUtil.getImage("image/nightsky.jpg"); //定义飞机的坐标 Plane plane = new Plane(planeImg,250,250); //定义炮弹 Shell[] shells = new Shell[50]; Explode boom; Date startTime = new Date(); Date endTime; int period;//游戏持续的时间 /** *画图 */ @Override public void paint(Graphics g) { //自动被调用,g这个变量相当于一个画笔 Color c = g.getColor(); super.paint(g);//防止黑屏 g.drawImage(nightsky,0,0,null); plane.drawSelf(g);//画飞机 if(plane.x <= Constant.GAME_WIDTH-plane.width && plane.x >= 30 && plane.y <= Constant.GAME_HEIGHT-30 && plane.y >= 0) plane.speed = 10; else plane.speed = -plane.speed; //画出所有的炮弹 for(int i=0;i以上是“java如何实现弹幕小游戏”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
当前名称:java如何实现弹幕小游戏
本文地址:http://6mz.cn/article/jcjhji.html