java实现弹幕小游戏代码分享
本篇内容介绍了“java实现弹幕小游戏代码分享”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
成都创新互联-专业网站定制、快速模板网站建设、高性价比广州网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式广州网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖广州地区。费用合理售后完善,10多年实体公司更值得信赖。
父类
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://myzitong.com/article/pspdge.html